maven工程web层的spring

时间:2022-09-15 20:09:44

1、避免IE浏览器发送ajax请求时,出现下载的情况

2、采用springmvc自带的JSON转换工具,支持@ResponseBody注解

3、对模型视图添加前后缀

4、controller层注入

下面是例子:

<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc
="http://www.springframework.org/schema/mvc"
xsi:schemaLocation
="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"
>

<!-- 避免IE在ajax请求时,返回json出现下载 -->
<bean id="mappingJackson2HttpMessageConverter"
class
="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>text/json;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 采用SpringMVC自带的JSON转换工具,支持@ResponseBody注解 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJackson2HttpMessageConverter" />
</list>
</property>
</bean>

<!-- 对模型视图添加前后缀 -->
<!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/view/" p:suffix=".jsp" />
-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value
="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

<!-- 扫描controller(controller层注入) -->
<mvc:annotation-driven />
<context:component-scan base-package="com.mmc.d4alc.controller" />

<mvc:resources location="/images/" mapping="/images/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/file/" mapping="/file/**" />
<mvc:resources location="/" mapping="/*.txt" />


</beans>