zoukankan      html  css  js  c++  java
  • Spring 3.x MVC 入门31 使用内容协商来实现多视图 示例

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

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

    配置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

    xml

    jsp

     

     

     

  • 相关阅读:
    观察者模式(学习笔记17)
    web前端安全编码(模版篇)
    如何获取元素最终使用的css值
    Range在各浏览器下的问题和常见处理办法
    总结cookie的一些问题
    js 设计模式单例模式
    web端不能登录问题汇总
    多域名登录方案思考
    深入分析js中的constructor 和prototype
    由一次代码优化想到的Js 数据类型
  • 原文地址:https://www.cnblogs.com/zhaoyang/p/2315432.html
Copyright © 2011-2022 走看看