springmvc对多视图的支持

来源:互联网 发布:mac如何改管理员名字 编辑:程序博客网 时间:2024/05/01 23:24

导入对xml视图支持的jar包

springmvc.xml配置多视图的支持

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd         http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-3.2.xsd         http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd         http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">        <!-- 开启扫描 com.study下的包,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean -->        <context:component-scan base-package="com.study"></context:component-scan>        <!-- annotation-driven:默认创建了多个对象:RequestMappingHandlerMapping,RequestMappingHandlerAdapter        也提供对json格式支持-->        <mvc:annotation-driven/>        <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">            <!-- 配置媒体类型 -->            <property name="contentNegotiationManager">                <bean class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">                    <property name="mediaTypes">                        <map>                            <!-- 如有其他视图可以在下面再加 -->                            <entry key="json" value="application/json"></entry>                            <entry key="xml" value="application/xml"></entry>                        </map>                    </property>                </bean>            </property>            <!-- 指定默认视图 -->            <property name="defaultViews">                <list>                    <!-- 对json格式视图支持 -->                    <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"></bean>                    <!-- 对xml格式视图支持 -->                    <bean class="org.springframework.web.servlet.view.xml.MarshallingView" >                        <constructor-arg>                            <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">                                <property name="classesToBeBound">                                    <list>                                        <!-- 配置需要解析的类 -->                                        <value>com.study.domain.User</value>                                    </list>                                </property>                            </bean>                        </constructor-arg>                    </bean>                </list>            </property>        </bean>        <!-- 配置sprigmvc视图解析器:解析逻辑试图              后台返回逻辑试图:index            视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/jsps/index.jsp        -->        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">            <property name="prefix" value="/WEB-INF/jsps/"></property>            <property name="suffix" value=".jsp"></property>        </bean></beans>

需要转换的javaBean加上注解
@XmlRootElement,我使用的是jdk1.7,1.51版本好像对此注解不支持


后台方法

    /**     * 对多视图的支持     */    @RequestMapping("multView")    public User multView(){        User user3 = new User();        user3.setId(1);        user3.setUserName("孙尚香");        user3.setAge(18);        user3.setBirthday(new Date());        return user3;    }

约定rest目录下所有以json和xml扩展名都支持相应的视图
访问需要在rest目下,后缀为.json或.xml对应的格式
这里写图片描述
这里写图片描述
否则
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述