First, Struts2 Download
Login http://struts.apache.org/download.cgi Download Struts2 Latest Version
Recommended Download Full distribution full version, this option includes Struts2 the sample application ( Apps folder), empty sample apps, core libraries ( Lib folder), source code, and documentation ( Docs folder), etc., src folder contains Struts2 the entire source code of the framework.
Second, Struts2 installation
copy these several required class libraries to Web of the application Web-inf/lib the path. If you need more features of Struts2 in your Web app , you'll need to copy the appropriate JAR file to Web of the application Web-inf/lib the path.
Third, the development process
1. In Web. XML The file defines the core Filter to intercept user requests.
<?xml version= "1.0" encoding= "GBK"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id= "Webapp_ ID "version=" 3.0 "><!--defines the core of the STRUTS2 filter--><filter><filter-name>struts2</filter-name ><filter-class>org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class ></filter><!--let Struts2 's core filter intercept all requests--><filter-mapping><filter-name>struts2</ Filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>
2. If you need to POST method to submit a request, you define the data that contains the form JSP page. It is not necessary to go through this step if you simply send the request in a GET mode.
3. Define the Action class that handles user requests . For example, we do a login demo needs a loginaction
Import Com.opensymphony.xwork2.actioncontext;import Com.opensymphony.xwork2.actionsupport;public class LoginAction Extends actionsupport{//defines username and password properties that encapsulate request parameters 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;} Defines the Execute method that handles user requests public String execute () throws exception{//when username is Crazyit.org,password for Leegang, the login succeeds if ( GetUserName (). Equals ("struts") && GetPassword (). Equals ("struts")) {Actioncontext.getcontext (). GetSession ( ). Put ("User", GetUserName ()); return SUCCESS;} Else{return ERROR;}}}
4. ConfigurationActionand the correspondence between the processing result and the physical view resource.
<struts><!--Specify global internationalized resource file--><constant name= "struts.custom.i18n.resources" value= "mess"/><!-- Specifies the character set used by the internationalized encoding--><constant name= "struts.i18n.encoding" value= "GBK"/><!--all action definitions should be placed under the package-- ><package name= "Lee" extends= "Struts-default" ><action name= "Login" class= " Org.crazyit.app.action.LoginAction "><!--define mappings 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>
5. Writing View Resources
login.jsp
<%@ page language= "java" contenttype= "text/html; CHARSET=GBK "pageencoding=" GBK "%><% @taglib prefix=" s "uri="/struts-tags "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
error.jsp
<%@ page language= "java" contenttype= "text/html; CHARSET=GBK "pageencoding=" GBK "%><% @taglib prefix=" s "uri="/struts-tags "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
welcome.jsp
<%@ page language= "java" contenttype= "text/html; CHARSET=GBK "pageencoding=" GBK "%><% @taglib prefix=" s "uri="/struts-tags "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
6. Writing internationalized Resource files
Use Native2ascii Conversion Encoding
loginpage=\u767b\u5f55\u9875\u9762
errorpage=\u9519\u8bef\u9875\u9762
succpage=\u6210\u529f\u9875\u9762
Failtip=\u5bf9\u4e0d\u8d77\uff0c\u60a8\u4e0d\u80fd\u767b\u5f55\uff01
Succtip=\u6b22\u8fce\uff0c{0},\u60a8\u5df2\u7ecf\u767b\u5f55\uff01
user=\u7528\u6237\u540d
Pass=\u5bc6 \u7801
Login=\u767b\u5f55
That
loginpage= Login Page
errorpage= error Page
succpage= Success Page
failtip= Sorry, you can't login!
succtip= Welcome, {0}, you are already logged in!
user= User name
pass= Dense Code
login= Login
Iv. Results of operation
Struts2 download, installation and development process