Use MyEclipse to develop struts2 framework structure detailed tutorial -- Take logon as an example, myeclipsestruts2

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.