Integrate Spring with Struts2 (next, from Chuanzhi podcast video, including all source code)

Source: Internet
Author: User
Tags format definition

Integrate Spring with Struts2 (next, from Chuanzhi podcast video, including all source code)

 

1. Here is the configuration content of web. xml. Next we will introduce in detail all its configurations.

 

 
   
    
      
   
    
Index. jsp
     
        
          
   
    
ContextConfigLocation
           
   
    
Classpath: beans. xml
       
     
    
    
   
    
Org. springframework. web. context. ContextLoaderListener
     
      
  
   
    
Struts
   
   
    
Org. apache. struts. action. ActionServlet
   
   
   
    
     
Config
    
    
     
/WEB-INF/struts-config.xml
    
   
   
   
    
0
   
  
  
   
    
Struts
   
   
    
*. Do
   
  
 
1First, we can see Tags:

 

Format definition:

 

 
  
   contextConfigLocation
  
  
   contextConfigLocationValue
  
 
If this label is not configured, The WEB-INF/applicationContext. xml file is loaded by default. However, you can configure Label Label to change the default properties of the configuration file.

 

As shown above, we modified Attribute to load the configuration file classpath: beans. xml.

(PS: For details about this label, refer to another blog: web. xml configuration. Details)

2. Then, we configure the ContextLoaderListener listener to enable the IOC container to automatically load the applicationContext configuration at startup,

Because it implements the ServletContextListener interface, the container will automatically execute its implementation method at startup.

 

     
    
     org.springframework.web.context.ContextLoaderListener
      
   
3. Configure the core interceptor of struts2 and initialize the struts2 configuration file.

 

 

Ii. Then we will configure the struts-config.xml file mentioned in web. xml

 

   
   
    
    
     
    
    
1. In the struts-config.xml, there is an anction whose path is "/person/list ". If you want to give this action to Spring management, we can use dependency injection to inject the bean at the business layer into the action.

 

However, make sure that the path attribute value of the action is the same as the bean name in Spring.

For example, in the previous blog post integrating Hibernate with Spring, there is a bean in beans. Its code is as follows:

 

   
2. It is not enough to define this. Tags:

 

 

   
    
   
Use DelegatingRequestProcessor to forward the processing to the bean in the Spring container. DelegatingRequestProcessor inherits from RequestProcessor.

 

This code tells Struts to use DelegatingRequestProcessor to replace the original RequestProcessor. After this setting is complete, Struts forwards the intercepted user requests to the bean under Spring context and matches them according to the bean name attribute.

3. After receiving the action passed by struts in beans. xml, match the corresponding bean.

 

Then we need to implement the bean entity class:

PersonAction. java

 

Package cn. itcast. web. action; import javax. annotation. resource; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. apache. struts. action. action; import org. apache. struts. action. actionForm; import org. apache. struts. action. actionForward; import org. apache. struts. action. actionMapping; import org. springframework. web. context. webApplicationContext; import org. sp Ringframework. web. context. support. webApplicationContextUtils; import cn. itcast. service. personService; public class PersonAction extends Action {@ Resource PersonService personService; @ Overridepublic ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {/* The comment is not sent to SpringIOC for management. * WebApplicationContext ctx = WebApplicationContextUtils. getWebApplicationContext (this. getServlet (). getServletContext (); PersonService personService = (PersonService) ctx. getBean (personService); */request. setAttribute (persons, personService. getPersons (); return mapping. findForward (list); // jump directly to the name = list page in struts-config .}}

 

4. Next we will implement the view page, that is, WEB-INF/page/personList. jsp

 

 

<%@ page language=java import=java.util.* pageEncoding=UTF-8%><%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=c %>
ID =$ {person. id}, name =$ {person. name}
The jstl label is used for direct output.

 

Note that the three-tier SSH framework project has been set up, and you only need to enter the address: http: // localhost: 8080/SSH2/person/list. do to access it.

However, because our jsp page is under the WEB-INF, is secure, so not through the browser address bar input results, only through the console

The Web Browser output result.


  <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybhq9 "" src = "http://www.bkjia.com/uploads/allimg/140814/0424042301-0.png"/>

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.