The first step: Download struts2 corresponding jar package, can be downloaded to Struts official website: http://struts.apache.org/download.cgi#struts252
For the purpose of learning, the entire compressed file can be downloaded.
It includes: 1 Apps: sample application, useful for learning; 2 docs: Related learning documents, API documents, etc. 3 LIB: Core Class library, dependency package; 4:SRC: Source code
Step Two: Create a new Dynamic WEB project type project in Eclipse, always click Next and check Generate Web. XML deployment descriptor to have Web. xml
Step three: Import the jar package
Struts2 has a few of the necessary jar packages, you can go to the official website to download Struts-min-lib.zip, inside the package is the necessary jar package.
Fourth step: write the corresponding action
Create a new package under SRC com.struts.action (Note: The name needs to be in com.XXX.action form), and then create a new Java file below: Loginaction.java
Packagecom.struts.action;ImportCom.opensymphony.xwork2.ActionSupport; Public classLoginactionextendsActionsupport {PrivateString username; PrivateString password; PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } PublicString Execute ()throwsException {if("username". Equals ( This. GetUserName ()) && "password". Equals ( This. GetPassword ())) { return"Success"; } Else { return"Fail"; } }}
Fifth step: New results page
Create the following two files in Web-inf:
success.jsp
<%@ page language= "java" contenttype= "text/html; Charset=iso-8859-1 " pageencoding=" Iso-8859-1 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > Success!</ Body>
fail.jsp
<%@ page language= "java" contenttype= "text/html; Charset=iso-8859-1 " pageencoding=" Iso-8859-1 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > fail!</body >
Sixth step: Create a new initial page under Web-inf (there are two parameters according to the form Loginaction know need to submit)
Login.html
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "UTF-8"><title>Insert Title here</title></Head><Body> <formAction= "Login"Method= "POST">User name:<inputtype= "text"name= "username">Password:<inputtype= "Password"name= "Password"> <inputtype= "Submit"value= "Submit"> </form> </Body></HTML>
Seventh Step: Configure the Web.xml:1 under Web-inf, join the Struts Filter 2, configure the home page
<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://xmlns.jcp.org/xml/ns/javaee"xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"ID= "webapp_id"version= "3.1"> <Display-name>Struts2project</Display-name> <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> <welcome-file-list> <Welcome-file>/web-inf/login.html</Welcome-file> </welcome-file-list></Web-app>
Eighth step: Configure the Struts2 configuration file, create a new struts.xml in the SRC directory, do the action configuration
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.5//en" "Http://struts.apache.or G/dtds/struts-2.5.dtd "><!--START snippet:xworksample -<Struts> < Packagename= "Com.struts.action"extends= "Struts-default"> <Actionname= "Login"class= "Com.struts.action.LoginAction"> <resultname= "Success">/web-inf/success.jsp</result> <resultname= "Fail">/web-inf/fail.jsp</result> </Action> </ Package></Struts>
End: This completes the landing of the simple struts2 frame, put into Tomcat and start, can be accessed through http://localhost:8080/struts2Project/.
Build a simple struts2 framework for landing