Reference: http://blog.csdn.net/hntyzgn2010/article/details/5547753
http://chenlh.iteye.com/blog/464341
Struts is open source software. Struts is used to help us reduce the time it takes to develop Web applications using the MVC design model. Struts is a good choice if we want to mix the benefits of Servlets and JSPs to build scalable applications.
Struts 2 is the next generation of struts and is the new Struts 2 framework that was combined with struts 1 and webwork technology. Its new Struts 2 architecture differs greatly from the architecture of struts 1. Struts 2, with webwork as its core, uses interceptors to handle user requests, and the design allows the business logic controller to completely disengage from SERVLETAPI, so struts 2 can be understood as WebWork's updated product. Although there are too many changes from struts 1 to struts 2, the changes are small with respect to the Webwork,struts 2.
1. Create a project
Tool: MyEclipse
First step: Create a new Web project
Step Two: Add the Struts 2.0 jar package to the project
Official: Http://struts.apache.
Download the compressed package:
Add the file to Lib in the project directory
=======
You can also do this:
New Web project--> Project right-->properties-->myeclipse-->project facets-->install Apache struts (2.x)
2. Configure STRUTS2 Forwarding Filter
Edit Web. XML content
<?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"version= "3.1"> <Display-name>StrutsTest2</Display-name> <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></Web-app>
NOTE: "/*" indicates that all browser-side requests involving this project have been processed by the STRUTS2 filter.
3. Create input page login.jsp, results page welcome.jsp and error.jsp
login.jsp
<%@ Page Language="Java"Import="java.util.*"pageencoding="GB2312"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML> <Head><title>Login interface</title></Head> <Body> <formAction= "Loginaction.action">User name:<inputname= "username"><BR>Password:<inputtype= "Password"name= "Userpass"><BR> <inputtype= "Submit"value= "Submit"> <inputtype= "Reset"value= "Cancel"> </form> </Body></HTML>
welcome.jsp
<%@ Page Language="Java"Import="java.util.*"pageencoding="GB2312"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML> <Head> <title>Welcome</title> </Head> <Body> <FontColor= "Red"size= "Ten">Login Successful!</Font> </Body></HTML>
error.jsp
<%@ Page Language="Java"Import="java.util.*"pageencoding="GB2312"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML> <Head> <title></title> </Head> <Body> <FontColor= "Red"size= "Ten">User or password Error!</Font> </Body></HTML>
4. Create the action file Loginaction and Struts2.xml files
Loginaction.java
Packagecom;ImportCom.opensymphony.xwork2.ActionSupport; Public classLoginactionextendsactionsupport{PrivateString username; PrivateString Userpass; PublicString Execute () {if("Asdf". Equals (username) && "123". Equals (Userpass))returnSUCCESS; Else returnERROR; } PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } PublicString Getuserpass () {returnUserpass; } Public voidSetuserpass (String userpass) { This. Userpass =Userpass; }}
Note: the Execute () method is executed in the default configuration, and the configuration is often changed in the actual application. The following will be explained in depth.
Note that the username and Userpass in this class must match the name attribute of the Web page file.
Struts.xml
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.1//en" "http://struts.apache.org/ Dtds/struts-2.1.dtd "><Struts>< Packagename= "StrutsTest2"extends= "Struts-default"> <Actionname= "Loginaction"class= "com. Loginaction "> <resultname= "Success">/welcome.jsp</result> <resultname= "Error">/error.jsp</result> </Action> </ Package> </Struts>
5. Publish the program to Tomcat and start Tomcat.
Mine is: http://localhost:8088/StrutsTest2/login.jsp
STRUTS2 Study Notes--Introduction to the first struts example