代码之家  ›  专栏  ›  技术社区  ›  Adi Sembiring

Spring2.5和Json(SpringJSON视图)

  •  2
  • Adi Sembiring  · 技术社区  · 14 年前

    Spring Json View 辅导的?

    hello.json .

    我对用Spring编写Java应用程序还比较陌生,我的配置文件可能有错误吗?

    我的项目名是 jsonapp

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        id="WebApp_ID" version="2.5">
        <display-name>jsonapp</display-name>
        <servlet>
            <servlet-name>jsonapp</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>jsonapp</servlet-name>
            <url-pattern>*.htm</url-pattern>
        </servlet-mapping>
    
    </web-app>
    

    杰索纳普-servlet.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
        <context:component-scan base-package="com.tutorial.json.web" />
    
        <bean
            class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
        <bean
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    
        <bean name="viewResolver" 
            class="org.springframework.web.servlet.view.XmlViewResolver" />
    
    </beans>
    

    查看.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
        <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"/>
    </beans>
    

    package com.tutorial.json.web;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    @RequestMapping("/hello.json")
    public class HelloController {
        @RequestMapping(method = RequestMethod.GET)
        public ModelAndView hello() {
            Map<String, String> model = new HashMap<String, String>();
            model.put("firstname", "Peter");
            model.put("secondname", "Schmitt");
    
            return new ModelAndView("jsonView", model);
        }
    }
    

    谢谢你的帮助

    1 回复  |  直到 12 年前
        1
  •  2
  •   Raghuram    14 年前

    <url-pattern> 在你的网站.xml已映射到 *.htm 而不是 *.json