Spring Integrated STRUTS2

Source: Internet
Author: User

1: Use spring's own listener

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http        Java.sun.com/xml/ns/javaee/web-app_2_5.xsd "id=" webapp_id "version=" 2.5 "> <!--Configure the Spring configuration file name and location-- <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpa Th:applicationcontext.xml</param-value> </context-param> <!--launch Servletcontextlistener of the IOC container- <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!--configuration Struts2 Filter--<filter> <filter-name>struts2&lt ;/filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pat Tern>/*</url-pattern> </filter-mapping></web-app>

2: Add the jar package. Get the name and location of the spring configuration file by: Classpath: That's it. Spring automatically helps us to load it. and put it in the ServletContext.

3: Writing class

 package   Com.struts.spring.beans;  public  class   person { private   String username;  public  void   Setusername (String username) { this . Usern    Ame = username;  public  void   Hello () {System.out.println (" My name is "+ username); }    }
 Package com.struts.spring.services;  Public class Personservice {    publicvoid  Save () {        System.out.println ("Test service layer" );    }}
 Packagecom.struts.spring.actions;ImportCom.struts.spring.services.PersonService; Public classpersonaction {PrivatePersonservice Personservice;  Public voidSetpersonservice (Personservice personservice) { This. Personservice =Personservice; }     PublicString Execute () {System.out.println ("Action Layer");                Personservice.save (); return"Success"; }}

4: Configure Applicationcontext.xml

class= "Com.struts.spring.beans.Person" >            <property name= "username" value= "is Lu Wei" ></property >        </bean>        class= "Com.struts.spring.services.PersonService"/>        class= " Com.struts.spring.actions.PersonAction "scope=" prototype ">            <property name=" Personservice "ref=" Personservice "></property>        </bean>

Knowledge Point: Note: When configuring the Struts2 Action in an IOC container, the scope property needs to be configured with a value of prototype. each time a new object is regenerated to the requester .

5: Write Struts.xml

<?xml version= "1.0" encoding= "UTF-8"? ><!DOCTYPE Struts public"-//apache software foundation//dtd Struts Configuration 2.3//en" "Http://struts.apache.org/dtds/struts-2.3.dtd" > <struts> <constant name= "Struts.enable.DynamicMethodInvocation" value= "false"/> <constant name= "Stru Ts.devmode "value=" true "/> < PackageName= "Default" namespace= "/"extends= "Struts-default" > <!--when spring consolidates Struts2, the spring Action that is configured in Struts2classneed to point to the ID of the bean in the IOC container--<action name= "Person-save"class= "Personaction" > <result>/success.jsp</result> </action> </ Package></struts>

Spring Integrated STRUTS2

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.