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(); ListcatList =catService.listCats(); request.setAttribute("carList",catList); return mapping.find("list"); } }
Spring's configuration in web.xml
contextConfigLocation /WEB-INF/classes/applicationContext.xml org.springframework.web.context.ContextLoaderListener CharacterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true CharacterEncodingFilter /*
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
hibernateFilter org.springframework.orm.hibernate3.support. OpenSessionInViewFilter hibernateFilter *.do
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(); ListcatList =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
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
catBeforeInterceptor catAfterInterceptor
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
{1} /list.jsp /list.jsp
Spring Configuration
web.xml configuration
contextConfigLocation /WEB-INF/classes/applicationContext.xml org.springframework.web.context.ContextLoaderListener Struts2 org.apache.struts2.dispatcher.FilterDispatcher Struts2 /*
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!