Spring Integration Struts2 (next, from the Wisdom Podcast video, including all source code)

Source: Internet
Author: User
Tags format definition

Next: Spring Integration hibernate:http://blog.csdn.net/u010800530/article/details/38470321

First, if you want to consolidate Struts2, the core interceptor is essential, and then you configure the Contextloaderlistener listener.


First, here is the configuration of Web. Xml , followed by a detailed description of all of its configuration

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "3.0" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "> <display-name></display-name> < welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-p Aram> <param-name>contextConfigLocation</param-name> &LT;PARAM-VALUE&GT;CLASSPATH:BEANS.XML&L T;/param-value> </context-param> <!--instantiate a spring container--<listener> <listener-class>org. Springframework.web.context.contextloaderlistener</listener-class> </listener> <servlet>< servlet-name>struts</servlet-name><servlet-class>org.apache.struts.action.actionservlet</ servlet-class><!--Specifies the configuration file to initialize the Struts2--><init-param><param-name>config</param-name><param-value>/web-inf/struts-config.xml</param-value></init-param><!- -Specify pre-loaded--><load-on-startup>0</load-on-startup></servlet><servlet-mapping>< Servlet-name>struts</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping> </web-app>
1, first, we see the <context-param></context-param> tag:

Format definition:

<context-param><param-name>contextConfigLocation</param-name><param-value> Contextconfiglocationvalue</param-value></context-param>
If you do not configure this tag, the Web-inf/applicationcontext.xml file will be loaded by default. However, we can change the default properties of the configuration file by configuring the contents of the <param-value> tag in the <context-param> tab.

As shown above, we have modified the <param-value> property so that he can load classpath:beans.xml this configuration file

(PS: For a detailed explanation of this tag, refer to my other blog post: Web. XML configuration <context-param> details)

2. Then we configure the Contextloaderlistener listener so that the IOC container automatically loads the ApplicationContext configuration when it is started,

Because it implements the Servletcontextlistener interface, the container will automatically execute the method it implements when it is started.

<listener>  <listener-class>org.springframework.web.context.contextloaderlistener</ Listener-class>  </listener>
3. Finally, configure the STRUTS2 core interceptor and initialize the struts2 configuration file


Second, we will then configure the Struts-config.xml file mentioned in Web. XML

1, in the Struts-config.xml, there is a anction, its path is "/person/list". If you want to give this action to spring management, we can use dependency injection to inject the business layer's beans into the action.

However, you must make sure that the value of the Path property of the action is the same as the name of the bean inside spring .

For example, we have a beans.xml in the last spring integrated Hibernate blog with a bean in it, with the following code:

<span style= "FONT-SIZE:14PX;" ><bean name= "/person/list" class= "Cn.itcast.web.action.PersonAction"/></span>
2, only these are not enough, you have to define <controller> Tags:

<controller><set-property property= "Processorclass" value= " Org.springframework.web.struts.DelegatingRequestProcessor "/></controller>
with delegatingrequestprocessor , the processing is forwarded to the bean in the spring container,delegatingrequestprocessor inherit from Requestprocessor.

The purpose of this code is to tellStrutswithDelegatingrequestprocessorto replace the original requestprocessor. When you are done setting this setting,StrutsThe intercepted user request is forwarded to the bean under spring context, matching the bean's Name property

thirdly, after receiving the action from struts in Beans.xml, match to the corresponding bean.

Then we need to implement this bean's 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.springframework.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 annotated content here is when the action is not handed over to SPRINGIOC 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. }}

Four, next we will implement the View page, 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 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
The Jstl label is used here to output it directly.

In particular, it is important to note that the project for the SSH three-tier framework is set up, and only the address must be entered: http://localhost:8080/SSH2/person/list.do can be accessed.

However, since our JSP page is under Web-inf and is security, it is not possible to enter results through the browser address bar, only through the console

Where the Web browser output results.







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.