1、Spring框架的搭建
这个很简朴,只需要web容器中注册org.springframework.web.context.ContextLoaderListener,并指定spring加载设置文件,那么spring容器搭建完成。(虽然org.springframework的焦点jar包需要引入)
虽然为了越发易用支持J2EE应用,一般我们还会加上如下:
Spring监听HTTP请求事件:org.springframework.web.context.request.RequestContextListener
<!-- spring设置文件开始 --> <context-param> <param-name>contextConfigLocation</param-name><!-- spring设置文件,请按照需要选取 --> <param-value>classpath*:webconfig/service-all.xml</param-value> </context-param> <listener><!-- Spring认真监听web容器启动和封锁的事件 --><!-- Spring ApplicationContext载入 --> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener><!-- Spring监听HTTP请求事件 --> <!-- 使spring支持request与session的scope,如: --> <!-- <bean id="loginAction" class="com.foo.LoginAction" scope="request"/> --> <!-- 利用: --> <!-- 1、注解获取:@Autowired HttpServletRequest request; --> <!-- 2、java代码:HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); --> <!-- 3、直接在参数中通报:public String sayHi(HttpServletRequest request) --> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <listener><!-- Spring 刷新Introspector防备内存泄露 --> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <filter> <filter-name>encodingFilter</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>false</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- spring设置文件竣事 -->
2、Spring MVC的搭建
首先我们知道Spring MVC的焦点是org.springframework.web.servlet.DispatcherServlet,所以web容器中少不了它的注册。(虽然org.springframework的web、mvc包及其依赖jar包需要引入)
<!-- spring mvc设置开始 --> <servlet> <servlet-name>Spring-MVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/spring-mvc.xml</param-value><!-- spring mvc设置文件 --> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Spring-MVC</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- spring mvc设置竣事 -->
同时为了更好利用MVC,spring-mvc.xml需要设置以下:
1)(可选)多部门请求理会器(MultipartResolver)设置,软件开发,与上传文件有关 需要类库commons-io、commons-fileupload
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"></property><!-- 默认编码--> <property name="maxUploadSize" value="104857600"></property><!-- 文件巨细最大值--> <property name="maxInMemorySize" value="40960"></property><!-- 内存中的最大值--> </bean>
2)(可选)当地化(LocaleResolver)设置
3)(可选)主题理会器(ThemeResolver)设置
4)(必选)处理惩罚器映射器(HandlerMapping)设置,可以设置多个,一般回收RequestMappingHandlerMapping可能自界说
这里我们自界说了一个处理惩罚器映射器,担任重写RequestMappingHandlerMapping,支持@RequestMapping无需任何path参数自动装载类名或要领作为url路径匹配。
<bean id="handlerMapping" class="io.flysium.framework.web.servlet.mvc.method.annotation.CustomHandlerMapping"> <property name="order" value="-1" /> </bean>