Use MyEclipse to develop struts2 framework structure detailed tutorial -- Take logon as an example, myeclipsestruts2
1. Create a Web Project named struts2, select Java EE6.0, and click Finish.
2. Right-click "struts" and choose "MyEclipse"> "Add Struts Capabilities". The following pop-up window is displayed, and then select "Struts 2.1. Click Finish.
3. The project directory is shown in:
4. Create a Login class that inherits the ActionSupport class (click "Browse" of the Superclass and select "Search ActionSupport)
5. Define the username and password variables, right-click and select Source-> Generate Getters and Setters to quickly import get and set functions.
6. Select Source-> Override/Implement Methods, select execute (), and click Finish.
7. Enter the following code:
public String execute() throws Exception{String u,p;u=getUsername();p=getPassword();if(u.equals("lang")&&p.equals("lang")){return "Success";}else
{return "Error";}
}
8. Create the Success. jsp interface and Error. jsp interface, and display "Logon successful" and "Logon Failed"
Add the following code to Success. jsp:
<% @ Taglib prefix = "s" uri = "/struts-tags" %> <s: property value = "username"/> logon successful !!! <Br>
You can obtain the logon username.
9. On the struts. xml Flow interface:
Select package, name "default", workspace "/action", inherit "struts-default", and add "Action" and name "Login ", class is "com. langguojie. struts2.Login "; add two" Results "with names Success and Error respectively. The corresponding JSP is Success. jsp and Error. jsp.
The code corresponding to Struts. xml is:
<struts><package name="default" namespace="/action" extends="struts-default"><action name="Login" class="com.langguojie.struts2.Login"><result name="Success">../Success.jsp</result><result name="Error">../Error.jsp</result></action></package></struts>
10. Running result:
11. Conclusion
After a preliminary understanding of Struts2, The Struts2 framework is very convenient for data transmission and calling, and is also very useful in website development. It is easy to use and can directly call functions. Some of the Code is also automatically generated.