Spring 3.x MVC 入门3-1 -- 使用内容协商来实现多视图 示例

使用内容协商实现多视图例

根据前篇文件的介绍,这里直接给出例子

 

配置xml

<context:component-scan base-package="com.controls" />

   

    <context:annotation-config />

   

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">

        <property name="order" value="1" />

        <property name="favorParameter" value="false" />

        <property name="ignoreAcceptHeader" value="true" />

       

        <property name="mediaTypes">

            <map>

                <entry key="json" value="application/json" />

                <entry key="xml" value="application/xml" />        

            </map>

        </property>

       

        <property name="defaultViews">

            <list>

                <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"></bean>

                <bean class="org.springframework.web.servlet.view.xml.MarshallingView">

                    <constructor-arg>

                        <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">

                             <property name="classesToBeBound">

                                <list>

                                   <value>com.model.User</value>

                                </list>

                             </property>

                        </bean>

                    </constructor-arg>

                </bean>

            </list>

        </property>

    </bean>

    <!-- 上面没匹配到则会使用这个视图解析器 -->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="order" value="2" />

        <property name="prefix" value="/WEB-INF/views/" />

        <property name="suffix" value=".jsp" />

        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />

    </bean>

 

 

Model

 

@XmlRootElement

public class User {

   

    private long userID;

    private String userName;

    private Date birth;

 

    public String getUserName() {

       return userName;

    }

    public void setUserName(String userName) {

       this.userName = userName;

    }

    public Date getBirth() {

       return birth;

    }

    public void setBirth(Date birth) {

       this.birth = birth;

    }

    public long getUserID() {

       return userID;

    }

    public void setUserID(long userID) {

       this.userID = userID;

    }

}

 

 

 

 

Contoller

@RequestMapping(value="/user/{userid}")

public String queryUser(@PathVariable("userid") long userID, ModelMap model)

{

      

       User u = new User();

       u.setUserID(userID);

       u.setUserName("zhaoyang");

       model.addAttribute("User", u);

      

       return "User";

}

 

 

如果是返回text/html,还需要建立个jsp文件

<body>

    UserName: ${requestScope.User.userID } <br />

    Age: ${requestScope.User.userName }

  </body>

 

测试结果

json

Spring 3.x MVC 入门3-1 -- 使用内容协商来实现多视图 示例

 

xml

 

Spring 3.x MVC 入门3-1 -- 使用内容协商来实现多视图 示例

 

 

jsp

 

Spring 3.x MVC 入门3-1 -- 使用内容协商来实现多视图 示例

 

 

 

你可能感兴趣的:(Spring 3)