[Struts] Learning diary 1-A simple example

Source: Internet
Author: User

In this example, a logon interface is provided. If the user enters the user ID and password, the logon is successful. It is easy to use the Eclipse plug-in of easy struts. The steps are as follows:

1. Create a new project and add easy struts support for the project in the project properties.

2. In the eclipse menu File> New> Other, select easy action assosiated with a form. This opens a wizard, which consists of three steps, which are described below.

3. The first step is to create a form. Leave the module blank (use the default value). Use Case to fill in logon. Easy struts will automatically change the form name and form type fields based on the name, it is best to change the package name of the latter as needed. Click the Add button on the Right of form properties to add the userid and password attributes. The types are Java. Lang. String, And the JSP input types are text and password respectively. Click Next to go to the next step.

4. The second step is to create an action. Use the default values. Click Next to go to the next step.

5. Step 3 is to create forwards and exceptions. Add a forward named success for the created action, and the path is/form/main.htm. This page is displayed after successful logon. Of course, you still need to create this page. Otherwise, the 404 error will occur after successful logon. Click Finish to complete the wizard.

6. In the execute method of the logonaction. Java file generated by the wizard, write downCodeAs follows:


Public actionforward execute (
Actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response)
Throws exception {

Logonform = (logonform) form;
Actionerrors errors = new actionerrors ();

If (! Logonform. getuserid (). Equals (logonform. GetPassword ()))
Errors. Add (actionerrors. global_error, new actionerror ("error. Password. mismatch "));

If (! Errors. isempty ()){
Saveerrors (request, errors );
Return Mapping. getinputforward ();

Return (mapping. findforward ("success "));


7. Add the project to a tomcat context by adding the following sentence to Tomcat server. xml:

<Context Path = "/Struts-test" reloadable = "true" docbase = "C: \ eclipse \ workspace \ Struts-test"/>

8. start Tomcat and Use http: // localhost: 8080/Struts-test/form/logon. jsp to access tomcat. the logon screen is displayed.

Note: I found that although easy struts will help you copy the required jar to the WEB-INF/lib, but this is only a part, you have to manually put other struts lib /*. copy the JAR file. Otherwise, the system prompts "cannot find actionmappings or actionformbeans Collection"

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.