springmvc配置
2015-08-31 15:24
423 查看
<?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 自动扫描且只扫描@Controller -->
<context:component-scan base-package="com.coamctech.sample.demo"
use-default-filters="false">//默认情况下它是true的,即不仅扫描@controller,还要扫描@content的子注解@service,@repository
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>
<!-- 当返回值为字符串时, 为了避免乱码问题, 将默认字符集设置为UTF-8(默认是ISO-8859-1) -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter" c:defaultCharset="UTF-8" />//spring默认的字符集是ISO-8859-1
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 定义JSP文件的位置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/" //controller中会返回一个字符串,而我们需要将其解析为视图的路径,因此通过对前缀和后缀的定义来定义视图的位置
p:suffix=".jsp" />
<!-- 容器默认的DefaultServletHandler处理 所有静态内容与无RequestMapping处理的URL -->
<mvc:default-se
4000
rvlet-handler /> //web.xml中定义了拦截所有请求,包括.js,.jpn等静态文件,因此我们需要定义一个默认的servlet来处理静态文件的访问
<!-- 定义无需Controller的url<->view直接映射 -->
<mvc:view-controller path="/" view-name="redirect:/index" />
<!-- 处理异常 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"
p:exceptionMappings-ref="exceptionMappings" />
<util:properties id="exceptionMappings">
<prop key="java.lang.Throwable">error/500</prop>
</util:properties>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 自动扫描且只扫描@Controller -->
<context:component-scan base-package="com.coamctech.sample.demo"
use-default-filters="false">//默认情况下它是true的,即不仅扫描@controller,还要扫描@content的子注解@service,@repository
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>
<!-- 当返回值为字符串时, 为了避免乱码问题, 将默认字符集设置为UTF-8(默认是ISO-8859-1) -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter" c:defaultCharset="UTF-8" />//spring默认的字符集是ISO-8859-1
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 定义JSP文件的位置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/" //controller中会返回一个字符串,而我们需要将其解析为视图的路径,因此通过对前缀和后缀的定义来定义视图的位置
p:suffix=".jsp" />
<!-- 容器默认的DefaultServletHandler处理 所有静态内容与无RequestMapping处理的URL -->
<mvc:default-se
4000
rvlet-handler /> //web.xml中定义了拦截所有请求,包括.js,.jpn等静态文件,因此我们需要定义一个默认的servlet来处理静态文件的访问
<!-- 定义无需Controller的url<->view直接映射 -->
<mvc:view-controller path="/" view-name="redirect:/index" />
<!-- 处理异常 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"
p:exceptionMappings-ref="exceptionMappings" />
<util:properties id="exceptionMappings">
<prop key="java.lang.Throwable">error/500</prop>
</util:properties>
</beans>
相关文章推荐
- Eclipse导出Android应用程序以及在真机和仿真机的安装
- springMVC下无法加载静态资源文件(js、css、image)
- spring mvc Controller间跳转 重定向 传参
- eclipse 版本区别
- Java锁的种类以及辨析——阻塞锁
- Spring MVC Custom Validator Example
- Java 内部类
- Spring整合Hibernate,不用HibernateDaoSupport与HibernateTemplate而用Hibernate自己的api分析说明
- Java锁的种类以及辨析——自旋锁的其他种类
- 华为上机测试题(数字字符串转二进制-java)
- Java可变参数使用方法
- JAVA类集
- 分治:合并排序的java程序实现
- java锁的种类以及辨析——自旋锁
- java类型转换易犯的错误
- JAVA使用POI操作excel
- 深入浅出RxJava三-响应式的好处
- Java内存管理原理及内存区域详解
- java 编码习惯整理
- 深入浅出RxJava二--操作符