Spring is an open source design-level framework that solves the problem of loose coupling between the business logic layer and other layers. Therefore, it integrates interface-oriented programming ideas throughout the entire system application. Spring is a lightweight Java development framework that emerged in 2003 and was created by Rod Johnson. Simply put, Spring is a layered JavaSE/EEfull-stack (one-stop) lightweight open source framework. This article mainly introduces the detailed explanation of Spring's WEB module configuration, briefly introduces its inheritance method, proxy method, and related detailed configuration code. It has certain reference value and friends in need can learn about it.
A brief introduction to the seven major modules of the Spring framework
Detailed explanation of the MVC module code in Spring
Spring's WEB module is used for integration Web frameworks, such as Struts1, Struts2, JSF, etc.
Integrate Struts1
Inheritance method
The Spring framework provides the ActionSupport class to support the Action of Struts1. After inheriting ActionSupport, you can obtain Spring's BeanFactory and obtain various resources in various Spring containers
import org.springframework.web.struts.ActionSupport; public class CatAction extends ActionSupport{ public ICatService getCarService(){ return (ICatService) getWebApplicationContext().getBean("catService"); } public ActionForward execute(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){ CatForm catForm = (CatForm) form; if("list".equals(catForm.getAction())){ returnthis.list(mapping,form,request,response); } } public ActionForward list(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){ CatForm catForm = (CatForm) form; ICatService catService =getCatService(); List<Cat> catList =catService.listCats(); request.setAttribute("carList",catList); return mapping.find("list"); } }
Spring's configuration in web.xml
<context-param><!-- Spring配置文件的位置--> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </context-param> <listener><!-- 使用Listener加载Spring配置文件--> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <filter><!-- 使用Spring自带的字符过滤器--> <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
If used in conjunction with Hibernate, you need to add the OpenSessionInViewFilter filter in web.xml to expand the session scope to the JSP layer to prevent lazy loading exceptions from being thrown
<filter> <filter-name>hibernateFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support. OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name> hibernateFilter</filter-name> <url-pattern>*.do</url-pattern><!-- 对Struts 1的Action启用--> </filter-mapping>
Agent method
The inheritance method is very simple to integrate into Spring, but the disadvantage is that the code is coupled with Spring, and Action is not managed by Spring, so you cannot use Spring's AOP and IoC features. You can avoid these defects by using the proxy method
public class CatAction extends Action{ //此处继承的Struts 1的Action private ICatService catService; //setter、getter略 public ActionForward execute(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){ CatForm catForm = (CatForm) form; if("list".equals(catForm.getAction())){ returnthis.list(mapping,form,request,response); } } public ActionForward list(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){ CatForm catForm = (CatForm) form; ICatService catService =getCatService(); List<Cat> catList =catService.listCats(); request.setAttribute("carList",catList); return mapping.find("list"); } }
This Action is not related to Spring When coupling occurs, only an ICatService attribute is defined, and then Spring is responsible for injecting
struts-congfig.xml configuration
<form-beans> <form-bean name="catForm" type="com.clf.spring.CatForm"> </form-beans> <action-mappings> <action name=" catForm" path="/cat" type="com.clf.spring.CatAction"> <forward name="list" path="/jsp/listCat.jsp"></forward> </action> </action-mappings> <!-- 最核心的配置,该配置把Struts的Action交给Spring代理--> <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" /> <!-- controller配置生效后,Action的type属性就是去作用了,Struts不会用type属性指定的类来创建CatAction,而是到Spring配置中寻找,因此Spring中必须配置CatAction --> <!-- Spring中配置Action使用的是name属性而不是id,Spring会截获"/cat.do"的请求,将catService通过setter方法注入到CatAction中,并调用execute()方法--> <bean name="/cat" class=" com.clf.spring.CatAction"> <property name="catService" ref="catService" /> </bean>
web.xml The configuration is the same as the inheritance method above
Action using proxy mode can configure Spring features such as interceptors, for example, configuring CatAction with a pre-method interceptor and a post-return interceptor
<bean id="catBeforeInterceptor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvodor"> <property name="advice"> <bean class="com.clf.spring.MethodBeforeInterceptor" /> </property> <property name="mappedName" value="*"></property> </bean> <bean id="catAfterInterceptor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvodor"> <property name="advice"> <bean class="com.clf.spring.MethodAfterInterceptor" /> </property> <property name="mappedName" value="*"></property> </bean> <bean name="/cat" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="interceptorNames"> <list> <value> catBeforeInterceptor</value> <value> catAfterInterceptor</value> </list> </property> <property name="target"> <bean class="com.clf.spring.CatAction"> <property name="catService" ref="catService"></property> </bean> </property> </bean>
Integrating Struts 2
Spring integrating Struts 2 requires the struts2-spring-2.011.jar package
public class CatAction{ private ICatService catService; private Cat cat; //setter、getter略 public String list(){ catService.listCats(); return "list"; } public String add(){ catService.createCat(cat); return list(); } }
struts.xml configuration
In addition to the normal configuration, you also need to
<constant name=" struts.objectFactory" value="spring" /> <packagenamepackagename="cat" extends="struts-default"> <action name="*_cat" method="{1}" class="catAction"> <param name="action" >{1}</param> <result>/list.jsp</result> <result name="list">/list.jsp</result> </action> </package>
Spring Configuration
<bean id="catAction" scope="prototype" class="com.clf.spring.CatAction"> <property name="catService" ref="catService"></property> </bean>
web.xml configuration
<context-param><!-- Spring配置文件的位置--> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </context-param> <listener><!-- 使用Listener加载Spring配置文件--> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <filter> <filter-name>Struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name> Struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
The above content is a detailed explanation of Spring's WEB module configuration. I hope it can help everyone.
Related recommendations:
Advantages of spring framework in Java framework
Detailed explanation of SpringAop in Java
How to use Spring boot to operate mysql database
The above is the detailed content of Detailed explanation of Spring's WEB module configuration. For more information, please follow other related articles on the PHP Chinese website!