springmvc配置文件配置说明
2017-01-14 17:24
375 查看
最近在自学springmvc,通过阅读Paul Deck的Spring MVC学习指南,边看书边做书里贯穿的项目,同时在博客中记录下来,一为加深记忆,深入理解,二来记性差,方便日后翻看。项目github地址:https://github.com/huajianduzhuo/testSpringMVC
1、<context:annotation-config />
启用spring基于annotation的DI(依赖注入),如@Autowired、@Resource等。controller需要调用service等服务来完成业务逻辑,通过@Autowired注入service。
2、<context:component-scan base-package="controller" /><context:annotation-config />
自动扫描配置包下所有标注@Controller的bean。只需要扫描controller,不需要扫描service、dao,service、dao在spring的配置文件里配置扫描。
3、<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
配置viewResolver试图解析器,如上配置有前缀和后缀两个属性,可使controller中返回的view视图路径缩短。如WEB-INF/jsp/index.jsp,只需提供index,试图解析器会自动增加前后缀。
4、<mvc:annotation-driven/>
<mvc:resources location="/css/" mapping="/css/**"/>
如果在web.xml中,配置DispatcherServlet的url-pattern为“/”,那么所有请求都会映射到DispatcherServlet,包括静态资源文件(如js、css、图片等),此时需要在springmvc的配置文件中配置如上resource元素,使静态资源单独处理,不通过DispatcherServlet。
5、 <!-- 从请求和响应读取/编写字符串 -->
<bean id="stringHttpMessage" class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 用于将对象转化为JSON -->
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringHttpMessage"/>
<ref bean="jsonConverter"/>
</list>
</property>
</bean>controller需要返回JSON字符串时,需要进行以上配置
1、<context:annotation-config />
启用spring基于annotation的DI(依赖注入),如@Autowired、@Resource等。controller需要调用service等服务来完成业务逻辑,通过@Autowired注入service。
2、<context:component-scan base-package="controller" /><context:annotation-config />
自动扫描配置包下所有标注@Controller的bean。只需要扫描controller,不需要扫描service、dao,service、dao在spring的配置文件里配置扫描。
3、<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
配置viewResolver试图解析器,如上配置有前缀和后缀两个属性,可使controller中返回的view视图路径缩短。如WEB-INF/jsp/index.jsp,只需提供index,试图解析器会自动增加前后缀。
4、<mvc:annotation-driven/>
<mvc:resources location="/css/" mapping="/css/**"/>
如果在web.xml中,配置DispatcherServlet的url-pattern为“/”,那么所有请求都会映射到DispatcherServlet,包括静态资源文件(如js、css、图片等),此时需要在springmvc的配置文件中配置如上resource元素,使静态资源单独处理,不通过DispatcherServlet。
5、 <!-- 从请求和响应读取/编写字符串 -->
<bean id="stringHttpMessage" class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 用于将对象转化为JSON -->
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringHttpMessage"/>
<ref bean="jsonConverter"/>
</list>
</property>
</bean>controller需要返回JSON字符串时,需要进行以上配置
相关文章推荐
- Shiro学习之路 Hello Shiro(一)
- 基于Spring Boot 的Blog开发
- Spring整合Dwr,Cannot locate BeanDefinitionParser for element [annotation-scan]
- SpringMVC两种接收GET参数的方法
- spring3 MVC手工搭建Spring3项目demo
- spring mvc 开发需要的jar包的用途详解
- 基于注解的 Spring MVC 简单入门-网摘
- [MVC] 深入浅出Spring MVC
- SpringMVC 整合 ehcache. 注解应用
- Circular view path [index]: would dispatch back to the current handler URL [/index] again.
- Mock 编写一个Spring Controller 测试类
- Spring MVC 配置 multipart 解析器 上传文件
- spring mvc @ModelAttribute注解应用分析
- 初学者对Spring MVC的认识
- 让 SpringMVC 接收多个对象的4种方法
- 使用POI、Spring MVC导出EXCEL
- spring mvc的web.xml的配置
- SpringMVC工作原理
- Spring MVC静态资源处理<mvc:resources><mvc:default-servlet-handler>
- spring拦截器配置