Create a Struts project under MyEclipse (illustration)

Source: Internet
Author: User
Today, a simple Struts project is created using MyEclipse:
A logon page. If both the user name and password are correct, the user name is displayed on the new page,
Otherwise, return to the logon page.

1) File-> New-> Project
Create a project

2) In the displayed dialog box, select "Tomcat Project"

3) enter the Project name: mytest
Other options are available by default.

4) configure the context name. The default value is enough.

5) the directory structure is as follows:

6) Right-click the project name "mytest" and a menu is displayed,
"MyEclpise-> Add WebProject Capabilities ",
Add a web container for the project

7) configure the web Container. All options are set by default.

The current directory structure of the project is as follows:

9) Right-click the project name "mytest" and a menu is displayed,
"MyEclpise-> Add Struts Capabilities ",
Add Struts components for the project

10) Configure Struts information. All options are available by default.

11-1) the current directory structure of the project is as follows:

11-2) double-click the struts-config.xml file, the struts design panel will appear:

12) in the design panel of struts-config.xml, click the "J" icon on the left (representing the creation of JSP files ),
Click the left button in the blank area of the design panel. The following dialog box is displayed:
Enter the JSP name welcom. jsp and select the template,
Note: by default, the first template is a JSP without Struts labels.

13) the code generated by the newly created welcome. jsp is as follows:
The content of this file will be modified later.

14) in the design panel of "struts-config.xml", right-click the blank space, a menu is displayed,
Select "New-> Form, Action and JSP ",
Create this file to generate the three files together.

15) The first step is to create a FormBean,
Enter "login" in the "Use case" Column (the Use case name can be any one here ),
After the input, the system automatically adds the corresponding Name to the "Name" and "Form type" columns.
Add attributes for FormBean first, and click "Add,
Add userName and passowrd.

16) on the "Method" tab, deselect the error Method and reset Method,
Because we just want to make a simple small example, we don't need these two methods.

17) on the "JSP" tab, create a JSP file. The JSP file here is the logon page,
Select "Create JSP form ",
Enter "/login. jsp" in the path bar. The default value is "/form/login. jsp ",
I do not like the default path and it is difficult to access, so I removed the "/form" in the middle ",
You can click "Next ".

18) Step 2: Create "Action ",
On the "Form" tab, add the input page path "/login. jsp ",
The path name is the path name entered,
Then, cancel the option for verification.

19) in the "Forwards" tab, enter two Forwards,
One is a legitimate user's turn to welcome. jsp,
The other is the conversion of illegal users to login. jsp,
Note that login. jsp needs to set redirect to true.


20) after the creation is complete, you can see their relationships in the struts-config design panel.

21) the content of login. jsp is as follows:
<Html: form action = "/login">
<Html: text property = "usreName"/>
<Html: password property = "password"/>
<Html: submit/> </Html: form>

22) the content of LoginAction. java is as follows. here we need to verify whether it is a legal user,
If the username is coris and the password is 999, it is a legal user, you can go to the welcome. jsp page,
Otherwise, it is returned to the login. jsp page.
LoginForm loginForm = (LoginForm) form;
If (loginForm. getUsreName (). equals ("coris ")&&
LoginForm. getPassword (). equals ("999 ")){
Request. setAttribute ("userName "),
LoginForm. getUsreName ());
Return mapping. findForward ("success ");

Return mapping. findForward ("failure ");

23) The content of welcome. jsp has also been changed. on this page, you need to get the login Name:
Hello, <bean: write name = "userName" scope = "request"/>

24) content in the struts-config.xml file is automatically generated at creation and does not need to be changed manually

25) Now you need to deploy the server, right-click the project name "mytest ",
Select "MyEclipse-> Add and Remove Project Deployment"

26) in the pop-up box, select the server and press "Add" to bring up the "New Deployment" dialog box,
Select "Tomcat 5" and click Finish

27) If Tomcat 5 is successfully configured, the following status is displayed:

28) after completing all the configurations, start the tomcat service now.

29) When tomcat is started normally, the console displays the following information,
I use the Japanese operating system, so the Japanese information is displayed.

30) Now you can access the page for testing,
Enter http: // localhost: 999/mytest/login. jsp in the address bar.
My tomcat port is 999. If my friend hasn't changed the tomcat port, the default port is 8080,
Enter the coris username and password 999. After submission, go to the welcome. jsp page,
Valid user.

31) If you enter an incorrect user name and password, you will be redirected to the login. jsp page,
Note that here I am using the word "redirection" because in the struts-config.xml file,
<Forward name = "failure" path = "/login. jsp" redirect = "true"/>,
Redirect = "true" is set. If it is set to false (false by default), the page will not be refreshed,
The entered value is still on the page.

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: 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.