Details of Spring WEB module configuration

  • 2020-12-09 00:52:21
  • OfStack

7 modules of Spring framework are briefly introduced

MVC module code detail in Spring

WEB module of Spring is used to integrate Web framework, such as Struts1, Struts2, JSF, etc

Integrated Struts1

Inheritance way

The Spring framework provides Action for ActionSupport classes to support Struts1. Once you inherit from ActionSupport, you get the BeanFactory of Spring, which gives you access to a variety of resources within the 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 configuration in ES33en.xml


<context-param><!-- Spring The location of the configuration file --> 
  <param-name>contextConfigLocation</param-name> 
  <param-value>/WEB-INF/classes/applicationContext.xml</param-value> 
</context-param> 
 
<listener><!--  use Listener loading Spring The configuration file --> 
  <listener-class> 
    org.springframework.web.context.ContextLoaderListener 
  </listener-class> 
</listener> 
 
<filter><!--  use Spring Built-in 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> 
    <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> 

When used in conjunction with Hibernate, you need to add the OpenSessionInViewFilter filter to ES39en.xml to extend the session layer to prevent lazy loading exceptions


<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><!--  right Struts 1 the Action To enable the --> 
</filter-mapping> 

Proxy mode

Inheritance into Spring is very simple, but the disadvantage is that the code is coupled to Spring, and Action is not handed over to Spring management, so Spring's AOP, IoC features can not be used, using proxy approach can avoid these defects


public class CatAction extends Action{ // Inherited here Struts 1 the Action 
  private ICatService catService ;  
  //setter , getter slightly  
 
  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 coupled to Spring, but just defines an ICatService attribute, 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 that the configuration will put Struts the Action to Spring The agent --> 
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" /> 
 
<!-- controller Once the configuration takes effect, Action the type The attribute is just going to function, Struts Don't use type Property to create the class specified by the CatAction , but rather to Spring Find in the configuration, therefore Spring Must be configured in CatAction --> 
<!-- Spring In the configuration Action Using the name Properties instead of id . Spring Be intercepted "/cat.do" The request of will catService through setter Method injection into CatAction , and call execute() methods --> 
<bean name="/cat" class=" com.clf.spring.CatAction"> 
  <property name="catService" ref="catService" /> 
</bean> 

web.xml is configured in the same way as inheritance above

Action using proxy mode can configure Spring features such as interceptors, such as pre-interceptors and post-interceptors for CatAction methods


<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> 

Integrated Struts 2

Spring Integration of Struts 2 requires es86EN2-ES87en-2.011.ES88en package


public class CatAction{ 
  private ICatService catService; 
  private Cat cat; 
  //setter , getter slightly  
 
  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 need to < contstant/ > Add a constant named struts.objectFactory and set the value to spring to indicate that the Action was generated by Spring. Then put the < action/ > The class attribute is changed to catAction, and Struts2 will look for bean named catAction in Spring


<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 The location of the configuration file --> 
  <param-name>contextConfigLocation</param-name> 
  <param-value>/WEB-INF/classes/applicationContext.xml</param-value> 
</context-param> 
 
<listener><!--  use Listener loading Spring The configuration file --> 
  <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> 

conclusion

This is the WEB module configuration details of Spring, I hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out.

Reference:

Discussion on page jump in Springmvc

Spring AOP introduction Demo Share

Spring Framework web project practice full code sharing


Related articles: