We know that the integration of STRUTS1 and spring is achieved by Org.springframework.web.struts.DelegatingActionProxy, the following is a specific user login implementation to illustrate the STRUTS2 integration of spring related content .
First, the preparatory work
1. Example analysis we do not deal with the database, all is when the login to determine whether the user name is a specified value, whether the password is a specified value, and the associated exception handling,
2. Why do we say STRUTS2 integrates spring? I believe I know it at home, I don't have to say more ....
3. Download the Struts2 jar package, source code, API documentation in HTTP://STRUTS.APACHE.ORG/DOWNLOAD.CGI#STRUTS212.
4. Download different versions of Spring's jar package, source code, API documentation in http://static.springframework.org/downloads/nightly/release-download.php.
5. Configure the development environment: Myeclipse6.0+eclipse3.3+jdk6.0+tomcat6.0+struts 2.0.11+spring2.0.
6. Create a new Web project and import the appropriate jar package as shown below:
A. Since IDE development tools are not yet well supported for struts2.0, all we need is a hand-work configuration, first of all we have just down the struts2.0 lib inside the Commons-logging-1.0.4.jar, Ognl-2.6.11.jar, Xwork-2.0.4.jar, Freemarker-2.3.8.jar, Struts2-core-2.0.11.1.jar and struts2.0 inside the required plug-in package Struts2-spring-plugin-2.0.11.1.jar added web-inf/lib below
B. Support for spring2.0 through the IDE development tools Project
7. Create a Struts.xml file under SRC (specific notation is presented later)
8. Configure Web. Xml as follows:
<web-app version= "2.5" 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_2_5.xsd ">
<!--load Struts2 core--
<filter>
<filter-name>struts2</filter-name>
<filter-class>
Org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--indicate where the spring configuration file is--
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<!--load Spring configuration file Applicationcontext.xml--
<listener>
<listener-class>
Org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
Second, the establishment of the front page
1. User login must have a user login page login.jsp, as follows:
<%@ taglib prefix= "s" uri= "/struts-tags"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 TRANSITIONAL//CN" >
<title>login2</title>
<body>
<s:form name= "Login" action= "login" method= "POST" >
<s:textfield name= "username" label= "account" ></s:textfield>
<s:password name= "password" label= "password" ></s:password>
<s:submit></s:submit>
</s:form>
</body>
2. If the login is successful, the index.jsp file is as follows:
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 TRANSITIONAL//CN" >
<title>login2</title>
<body>
<div>
<br/>
</div>
</body>
3, the User name illegal prompt page. usernameinvalid.jsp (The exception information is obtained by El Expression)
<%@ page language= "java" contenttype= "text/html; charset=gb18030 "pageencoding=" GB18030 "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb18030 ">
<title>insert title here</title>
<body>
User name illegal: <font color= "Red" >${exception.message}</font>
</body>
4, password illegal prompt page. passwordinvalid.jsp (struts2 tag gets exception information);
<%@ page language= "java" contenttype= "text/html; charset=gb18030 "
pageencoding= "GB18030"%>
<%@ taglib prefix= "s" uri= "/struts-tags"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb18030 ">
<title>insert title here</title>
<body>
Password illegal: <font color= "Red" ><s:property value= "Exception.Message"/></font>
</body>
Third, establish the corresponding action
1. Loginservice class to provide user request service
Package org.topCSA.s2s.service;
Import org.topCSA.s2s.exception.PasswordException;
Import org.topCSA.s2s.exception.UsernameException;
public class Loginservice
{
/*
* We this is just a small example, not with the database to hit the
* If there is a database operation, then in this loginservice is the operation of the specific DAO class implementation of login-related operations
*/
public Boolean validate (String username,string password) throws Exception
{
Boolean v = false;
if (! " XCP ". Equals (username)//If the user name is not equal to XCP, an exception is thrown
{
throw new Usernameexception ("Incorrect user name");
}
else if (! " 123 ". Equals (password))))//If the password is not equal to 123, an exception is thrown
{
throw new Passwordexception ("Incorrect password");
}
Else
{
v = true;
}
return v;
}
}
2. Loginaction class to receive user requests
Package org.topCSA.s2s.action;
Import Org.topCSA.s2s.service.LoginService;
Import Com.opensymphony.xwork2.ActionSupport;
public class Loginaction extends Actionsupport
{
/**
*
*/
Private static final long serialversionuid = 1L;
Private String username;
private String password;
/*
* We inject loginservice through the spring IOC container, thus reducing the dependency between classes
*/
Private Loginservice Loginservice;
Public Loginservice Getloginservice ()
{
return loginservice;
}
public void Setloginservice (Loginservice loginservice)
{
This.loginservice = Loginservice;
}
Public String GetUserName ()
{
return username;
}
public void Setusername (String username)
{
This.username = Username;
}
Public String GetPassword ()
{
return password;
}
public void SetPassword (String password)
{
This.password = password;
}
@Override
public void Validate ()
{
/*
* We can add some input validation to this method class, but in order to reflect the business logic method we wrote later, this does not validate
*/
}
@Override
Public String Execute () throws Exception
{
Boolean result = Loginservice.validate (username, password);
if (result = = True)
{
return SUCCESS;
}
Else
{
return INPUT;
}
}
}
Iv. configuration Struts.xml and Applicationcontext.xml
1. Configuration struts.properties, in order to solve the Chinese problem, the use of the reference struts2 is as follows: Inside plus struts.i18n.encoding = gb2312, of course, can also be added directly to the struts.xml in the wording of the < Constant Name= "struts.i18n.encoding" value= "GBK" ></constant>
2. Configure Struts.xml
<! DOCTYPE Struts Public
"-//apache software foundation//dtd Struts Configuration 2.0//en"
"Http://struts.apache.org/dtds/struts-2.0.dtd" >
<struts>
<package name= "struts2" extends= "Struts-default" >
<action name= "Login" class= "Loginaction" >
<exception-mapping result= "Usernameinvalid" exception= "Org.topCSA.s2s.exception.UsernameException"/>
<exception-mapping result= "Passwordinvalid" exception= "Org.topCSA.s2s.exception.PasswordException"/>
<result name= "Success" >/index.jsp</result>
<result name= "Input" >/login.jsp</result>
<result name= "Usernameinvalid" >/usernameInvalid.jsp</result>
<result name= "Passwordinvalid" >/passwordInvalid.jsp</result>
</action>
</package>
</struts>
3. Configure Applicationcontext.xml
<beans
Xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.0.xsd ">
<bean name= "Loginservice" class= "Org.topCSA.s2s.service.LoginService"/>
<bean name= "loginaction" class= "Org.topCSA.s2s.action.LoginAction" >
<property name= "Loginservice" >
<ref bean= "Loginservice"/>
</property>
</bean>
</beans>
V. Testing (all success)
STRUTS2 Integrated Spring Application Example