说来惭愧,用SpringMVC做了2个web项目了,但是第一次纯属赶鸭子上架,连spring、jsp、servlet是啥都不知道的小家伙突然间要做一个web项目了。至今想来,还是很操蛋的。
不过用过之后,基础的web知识,springMVC+mybatis+mysql+dubbo的尝试就算有了。
第二次用就熟练多了,这次勇敢的用上了springSecurity,好吧,又被自己坑了。各种约定,各种名词让一个初出茅庐的小朋友很是伤感。
现在,对之前的2个web项目中用到的springMVC做一个总结,算是笔记吧。
从web.xml开始。
<!-- Spring的log4j监听器 -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>WEB-INF/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<!-- Spring上下文监听器 -->
<context-param>
<param-name>
contextConfigLocation
</param-name>
<param-value>
/WEB-INF/spring/*.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- SpringSecurity安全链 -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter>
<filter-name>CharacterFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>ForceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CharacterFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>Spring-Dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Spring-Dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 默认servlet处理静态资源 -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.ico</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.eot</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.svg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.ttf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.woff</url-pattern>
</servlet-mapping>
<!-- c标签引用 -->
<jsp-config>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/tld/c.tld</taglib-location>
</taglib>
</jsp-config>
上述代码就是一个比较完毕的web项目中spring的一些配置项。包含了:
log4j;
spring上下文监听初始化;
springSecurity开启并拦截所有请求url;
静态资源映射;
jstl标签支持;
请求url的编码处理;
springMVC拦截所有请求url并且交由Spring-Dispatcher-servlet处理。
然后到MVC关键部分Spring-Dispatcher-servlet。
<?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:security="http://www.springframework.org/schema/security"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.customer.note.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Json -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<util:list id="beanList">
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<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>
</util:list>
</property>
</bean>
<security:global-method-security
jsr250-annotations="enabled" secured-annotations="enabled" />
</beans>
这里包含4个部分:
第一部分:注解扫描配置;
这是扫描controller注解的配置,用于执行注解扫描并且将请求的url与具体的类与方法关联起来。
第二部分:视图解析引擎配置;
声明具体页面的解析器,比如上述使用的解析器是支持jstl的,同时描述页面的本地路径与后缀。
第三部分:消息转换器装配;
针对方法级注解的数据转换适配器,目前支持的是用jackson来做json到对象的自动转换,媒体类型支持多种json格式的以及通用格式。
第四部分:安全相关(不在讨论范围,忽略)
到这,springMVC的基础配置就差不多了,现在来看看controller的配置。
@Controller
public class ViewController {
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login() {
return "login";
}
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index() {
return "index";
}
@RequestMapping(value = "/show", method = RequestMethod.GET)
public String show() {
return "show";
}
@RequestMapping(value = "/caseManager", method = RequestMethod.GET)
public String caseManager() {
return "caseManager";
}
@RequestMapping(value = "/policy", method = RequestMethod.GET)
public String policy() {
return "policy";
}
}
结合spring 对于RESTful的支持,这里的url都向REST的规范靠拢,同时限定接入请求的类型。
基本的使用就到这里结束了,下一篇说一些看springMVC源码后的一些体会。