A brief introduction to the seven major modules of Spring framework
Detailed explanation of the MVC module code in Spring
Spring's WEB module is used to integrate Web frameworks, such as Struts1, Struts2, JSF, etc.
Integrate Struts1
Inheritance method
The Spring framework provides ActionSupport class to support Struts1's Action. After inheriting ActionSupport, you can obtain Spring's BeanFactory, thereby obtaining 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,HttpServletResponsesponse){ CatForm catForm = (CatForm) form; if("list".equals(catForm.getAction())){ returnthis.list(mapping,form,request,response); } } public ActionForward list(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponsesponse){ CatForm catForm = (CatForm) form; ICatService catService =getCatService(); List<Cat> catList =catService.listCats(); request.setAttribute("carList",catList); return mapping.find("list"); } }Spring configuration in web.xml
<context-param><!-- Location of Spring configuration file --> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </context-param> <listener><!-- Loading Spring configuration file using Listener--> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <filter><!-- Using Spring's own character filter --> <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> <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 an OpenSessionInViewFilter filter in web.xml to expand the session scope to the JSP layer to prevent delayed 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><!-- Enable Action for Struts 1--> </filter-mapping>
Agent method
Integrating the inheritance method into Spring is very simple, but the disadvantage is that the code is coupled with Spring, and the Action is not handed over to Spring for management, so Spring's AOP and IoC features cannot be used. Using the proxy method can avoid these defects.
public class CatAction extends Action{ //The Action private ICatService catService inherited here is catService; //setter and getter slightly public ActionForward execute(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponsponse){ CatForm catForm = (CatForm) form; if("list".equals(catForm.getAction())){ returnthis.list(mapping,form,request,response); } } public ActionForward list(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponsponse){ CatForm catForm = (CatForm) form; ICatService catService =getCatService(); List<Cat> catList =catService.listCats(); request.setAttribute("carList",catList); return mapping.find("list"); } }This Action is not coupled with Spring, but just defines an ICatService property, which is then injected by Spring
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> <!-- The most core configuration, which handes the Action of Struts to the Spring Agent--> <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" /> <!-- After the controller configuration takes effect, the type property of the Action is used. Struts will not use the class specified by the type property to create a CatAction, but search in the Spring configuration. Therefore, CatAction must be configured in Spring --> <!-- The Action in Spring uses the name property instead of id. Spring will intercept the "/cat.do" request, inject catService into the CatAction through the setter method, and call the execute() method --> <bean name="/cat"> <property name="catService" ref="catService" /> </bean>
The configuration of web.xml is the same as the inheritance method above.
Using the proxy method Action can configure Spring features such as interceptors, such as configuring the CatAction before and after interceptors after method
<bean id="catBeforeInterceptor"> <property name="advice"> <bean /> </property> <property name="mappedName" value="*"></property> </bean> <bean id="catAfterInterceptor"> <property name="advice"> <bean /> </property> <property name="mappedName" value="*"></property> </bean> <bean name="/cat"> <property name="interceptorNames"> <list> <value> catBeforeInterceptor</value> <value> catAfterInterceptor</value> </list> </property> <property name="target"> <bean> <property name="catService" ref="catService"></property> </bean> </property> </bean> </property> </bean>
Integrate Struts 2
Spring integrates Struts 2 requires struts2-spring-2.011.jar package
public class CatAction{ private ICatService catService; private Cat cat; //setter and getter public String list(){ catService.listCats(); return "list"; } public String add(){ catService.createCat(cat); return list(); } }struts.xml configuration
In addition to normal configuration, you also need to add a constant named struts.objectFactory and set the value to spring to indicate that the Action is generated by Spring. Then change the class attribute of <action/> to catAction, Struts2 will search for a bean named catAction in Spring.
<constant name=" struts.objectFactory" value="spring" /> <packagenamepackagename="cat" extends="struts-default"> <action name="*_cat" method="{1}"> <param name="action" >{1}</param> <result>/list.jsp</result> <result name="list">/list.jsp</result> </action> </package>Spring configuration
<bean id="catAction" scope="prototype"> <property name="catService" ref="catService"></property> </bean>
web.xml configuration
<context-param><!-- Location of Spring configuration file --> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </context-param> <listener><!-- Loading Spring configuration file using Listener--> <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>
Summarize
The above is all the detailed explanation of Spring's WEB module configuration, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out.
refer to:
A brief discussion on the page jump problem in Springmvc
Spring AOP Introduction Demo Sharing
Spring framework web project practical code sharing