Spring + Struts2 Integration

Source: Internet
Author: User

1. Integrate Struts2
A) import the struts jar package
Struts2-core-2.1.6.jar xwork-2.1.2.jar ognl-2.6.11.jar freemarker-2.3.13.jar commons-logging-1.1.jar
B) copy the struts. xml file to the src directory.
C) configure the struts core filter in web. xml.

<Filter>
<Filter-name> struts2 </filter-name>
<Filter-class> org. apache. struts2.dispatcher. ng. filter. StrutsPrepareAndExecuteFilter </filter-class>
</Filter>
<Filter-mapping>
<Filter-name> struts2 </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>

2. The entire Spring
A) import the jar package Spring. jar commons-logging.jar of spring
B) copy the applicationContext. xml file to the WEB-INF directory
C) configure the spring listener in web. xml.
 

<! -- Configure the spring listener -->
<Listener>
<Listener-class> org. springframework. web. context. ContextLoaderListener </listener-class>
</Listener>

D) add plug-in struts2-spring-plugin-2.1.6.jar for spring and struts2 Integration
 
3. write code for testing
Login. jsp
 

<Body>
<Form action = "login" method = "post">
USERNAME: <input type = "text" name = "username"/> <br>
PASSWORD: <input type = "password" name = "password"/> <br>
<Input type = "submit" value = "login">
</Form>
</Body>

Login successful page success. jsp

<Body>
Login successful!
</Body>

LoginDao business interface

Package com. itmyhome. actioin;
 
Interface LoginDao {
Public boolean login (String username, String password );
}

 
LoginDaoImpl business interface implementation

Package com. itmyhome. actioin;
 
Public class LoginDaoImpl implements LoginDao {
 
Public boolean login (String username, String password ){
// TODO Auto-generated method stub
If ("zhangsan". equals (username) & "123". equals (password )){
Return true;
} Else {
Return false;
}
}
 
}

LoginAction

Package com. itmyhome. actioin;
 
Import com. opensymphony. xwork2.ActionSupport;
 
Public class LoginAction extends ActionSupport {
Private String username;
Private String password;
Private LoginDao loginDao; // The DAO is handed to spring for processing.
Public String execute (){

If (loginDao. login (username, password )){
Return "success ";
} Else {
Return "input ";
}
}
 
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;
}
 
Public LoginDao getLoginDao (){
Return loginDao;
}
 
Public void setLoginDao (LoginDao loginDao ){
This. loginDao = loginDao;
}

}

Struts. xml configuration file

<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE struts PUBLIC
"-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN"
Http://struts.apache.org/dtds/struts-2.0.dtd>
 
<Struts>
 
<Package name = "default" namespace = "/" extends = "struts-default">
<Action name = "login" class = "com. itmyhome. actioin. LoginAction">
<Result name = "success">/success. jsp </result>
<Result name = "input">/login. jsp </result>
</Action>
</Package>
 
<! -- Add packages here -->
 
</Struts>

ApplicationContext. xml configuration file

<Bean id = "loginDao" class = "com. itmyhome. actioin. LoginDaoImpl"/>
 
<! -- Annotated -->
<Bean id = "loginAction" class = "com. itmyhome. actioin. LoginAction">
<Property name = "loginDao" ref = "loginDao"/>
</Bean>

Deployment: start tomcat and enter "zhangsan" and "123" to jump to success. jsp integration successful

From the itmyhome Column

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.