Home> Java> javaTutorial> body text

Detailed explanation of Spring's WEB module configuration

小云云
Release: 2017-12-05 11:34:35
Original
2025 people have browsed it

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 catList =catService.listCats(); request.setAttribute("carList",catList); return mapping.find("list"); } }
Copy after login

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 /* 
Copy after login

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 
Copy after login

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 catList =catService.listCats(); request.setAttribute("carList",catList); return mapping.find("list"); } }
Copy after login

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

              
Copy after login

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        
Copy after login

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(); } }
Copy after login

struts.xml configuration

In addition to the normal configuration, you also need to add a constant named struts.objectFactory and set the value to spring , indicating that the Action is generated by Spring. Then change the class attribute of to catAction. Struts2 will look for the bean named catAction in Spring

   {1} /list.jsp /list.jsp  
Copy after login

Spring Configuration

  
Copy after login

web.xml configuration

 contextConfigLocation /WEB-INF/classes/applicationContext.xml    org.springframework.web.context.ContextLoaderListener    Struts2 org.apache.struts2.dispatcher.FilterDispatcher    Struts2 /* 
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!