This article turns from http://blog.csdn.net/liaisuo/article/details/9064527
Today, I built the STRUTS2 framework in Eclipse and completed a very simple example program.
The overall diagram is as follows:
The first step : Download the full distribution in http://struts.apache.org/download.cgi, the latest version of Struts2, which is the complete version of STRUTS2
The second step : Unzip the downloaded compressed package. Copy the 8 jar packages shown under the struts-2.3.14.3-a Lib path into the Lib directory under Java Engineering.
Step three : In the Web. XML configuration file, configure the core filter for STRUTS2. The code is as follows:
<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>
Fourth Step : In order for struts to run, create the Struts.xml configuration file in the SRC directory. The code is as follows:
<?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>
<package name= "test" extends= "Struts-default" >
<action name= "Login" class= "Com.rankexam.control.LoginAction" >
<!--define a mapping between three logical views and physical resources--
<result name= "Input" >/Login.jsp</result>
<result name= "Error" >/Error.jsp</result>
<result name= "Success" >/Welcome.jsp</result>
</action>
</package>
</struts>
Fifth Step : Create the action class that handles the user request, which inherits from the Actionsupport class. The code is as follows:
Package Com.rankexam.control;
Import Javax.servlet.http.HttpServletRequest;
Import Org.apache.struts2.ServletActionContext;
Import Com.opensymphony.xwork2.ActionContext;
Import Com.opensymphony.xwork2.ActionSupport;
public class Loginaction extends actionsupport{
/**
* Default version serial number
*/
Private static final long serialversionuid = 1L;
Private String username;
private String password;
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 String Execute () throws Exception {
if (GetUserName (). Equals ("Baidu") && GetPassword () equals ("Baidu")) {
Actioncontext.getcontext (). GetSession (). Put ("User", GetUserName ());
Return "Success";
}else{
return "error";
}
}
}
Sixth Step : Complete the Welcome, Error, login page writing. The code is as follows:
1.login.jsp page Code
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<% @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=utf-8 ">
<title> Login Page </title>
<body>
<s:form action= "Login" method= "POST" >
<s:textfield name= "username" label= "username" ></S:textfield>
<s:password name= "password" label= "password" ></S:password>
<s:submit text= "Login" ></S:submit>
</S:form>
</body>
2.welcome.jsp page Code
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<% @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=utf-8 ">
<title> Login Success Page </title>
<body>
${sessionscope.user} Hello, you are already logged in!
</body>
3.error.jsp page Code
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<! 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=utf-8 ">
<title> Login Failure Page </title>
<body>
Sorry, Login failed!
</body>
Through the above process, we have completed the STRUTS2 framework and its simple use in eclipse.
Using Eclipse to build the STRUTS2 framework