zl程序教程

您现在的位置是:首页 >  后端

当前栏目

springmvc 支持对象与json 自动转换的配置

2023-09-27 14:24:39 时间

基于maven的工程,

需要在pom.xml中添加如下依赖

       <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

在springmvc的配置文件中添加如下代码

http://www.springframework.org/schema/mvc 

 

http://www.springframework.org/schema/mvc/spring-mvc.xsd

 

 

<!--支持对象与json的自动转换-->
    <mvc:annotation-driven />    

 

 

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:flow="http://www.springframework.org/schema/webflow-config"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:jms="http://www.springframework.org/schema/jms"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xmlns:osgi="http://www.springframework.org/schema/osgi"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:mvc="http://www.springframework.org/schema/mvc" 

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
          http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
          http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.3.xsd
          http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
          http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
 
    <!--关于扫描包的问题https://blog.csdn.net/lhpnba/article/details/77988616-->      
    <context:component-scan
        base-package="com.hs" >
        <!--只扫描控制器。 -->
        <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
    <!--支持对象与json的自动转换-->
    <mvc:annotation-driven />    
    <!--对静态资源的处理-->
    <mvc:resources mapping="/static/**" location="/static/" />  
    <!-- 视图解析器 -->  
    <!-- <bean class="org.springframework.web.servlet.HandlerInterceptor"></bean> -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/"/>  
        <property name="suffix" value=".jsp"/> 
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
    </bean>  
</beans>
springmvc的配置用以支持json和对象的自动转换