Use Eclipse + MyEclipse to develop a classic struts instance

Source: Internet
Author: User
Tags tld

Introduction:
This document describes how to use struts designer (graphical development environment) of myeclipse to develop a simple user login program segment.
It mainly includes two jsp files, one ActionForm, one Action, and other
UserLogin. jsp (user logon and error prompt page) userLoginSuccess. jsp (logon success prompt page)
UserLoginForm. java (ActionForm, which stores user submitted information)
UserLoginAction. java (Action, simple processing of user logon events)

 
Let's get started.

First, create a j2ee web project.1:

Click next, enter LoginDemo for Project name, and keep the remaining items by default. Click finish.
Under package explorer, we can see our project, and then add necessary files for the Struts framework to this project. right-click our project name and choose mydomaines --> Add Struts Capabilities... dialog Box 2 is displayed:

Here, Struts config path is our struts configuration file, and URL pattern is selected *. do, Default application resource is our Default resource file location. You can choose its storage location, where we keep the Default. After you click Finish, the project structure is similar to Figure 3:

Let's start with our example. First, modify the/WEB-INF/web. xml file to add a tag library for it (found in use that it can be successful without adding. However, to keep the consistency in the book, add security. Hoho ~~) Add the following code to </webapp>:

<Jsp-config>
<Taglib>
<Taglib-uri>/tags/struts-bean </taglib-uri>
<Taglib-location>/WEB-INF/struts-bean.tld </taglib-location>
</Taglib>
<Taglib>
<Taglib-uri>/tags/struts-html </taglib-uri>
<Taglib-location>/WEB-INF/struts-html.tld </taglib-location>
</Taglib>
<Taglib>
<Taglib-uri>/tags/struts-logic </taglib-uri>
<Taglib-location>/WEB-INF/struts-logic.tld </taglib-location>
</Taglib>
<Taglib>
<Taglib-uri>/tags/struts-nested </taglib-uri>
<Taglib-location>/WEB-INF/struts-nested.tld </taglib-location>
</Taglib>
</Jsp-config>
After that, open the struts-config.xml file and click Design in the lower left corner of the interface to enter the visual Design interface. Have you noticed Palette on the right? :) click it and let's start our jsp page design. Let's create the userLoginSuccess. jsp file first. Why should we create this file first? In myeclipse, you can create the Action, ActionForm, and Jsp files at one time (create three associated files ).
Click the create JSP file icon on the Palette panel to bring up the create JSP file panel. Figure 4:

Enter userloginsuccess. jsp in file name, select 2] standard JSP using Struts 1.1 as template to use, and click Finish.
After the completion, the struts-config.xml file is automatically updated, and the visualization community also appeared on the newly created JSP module. The newly created JSP file is also opened.
Override all the <% @ taglib... for what we started defining in/WEB-INF/Web. xml:

<% @ Taglib uri = "/tags/Struts-html" prefix = "html" %>
<% @ Taglib uri = "/tags/Struts-bean" prefix = "Bean" %>
<% @ Taglib uri = "/tags/Struts-logic" prefix = "logic" %>

Then add the following in <body> </body>:
Hello <Bean: write name = "username" Scope = "request"/>.
Here, the username attribute in the request is output on the page, so wait for us to log on to userloginaction and set a relevant attribute after logon.

OK. Let's start designing the last three files. In the Design mode of the Struts-config.xml, right-click the blank area of the drawing, choose New --> New Form, Action and JSP to bring up the option Panel of the ActionForm, we enter the relevant value according to the diagram, figure 5:

On the Form Properties tab of Optional Details, click add to add values for this ActionForm. In this logon example, two attributes userName and password are added, as shown in Figure 6:

When adding a password, select password from the JSP input type drop-down box.
After completing this step, we will complete the ActionForm design.
Next, select the JSP tab of Optional Details. Let's select Create JSP form? In this step, myeclipse will create a simple login page for us to interact with users. Keep the content as shown in figure 7. Figure 7:

Because this is just a simple demonstration of a login segment, you do not need to verify that the user information is valid, so remove the new method of the Option Details method tab, 8:

Click Next To Go To The Action Option panel. deselect the Validate Form Option in the Form tab of Option Details. 9:

Then, click add on the Forwards tab to add the page for Success and Failure return. 10:

Click Finish. In the Design of the Struts-config.xml, You can see Figure 11:

Finally, modify userLogin. jsp and replace all <% @ taglib... %>:
<% @ Taglib uri = "/tags/struts-html" prefix = "html" %>
<% @ Taglib uri = "/tags/struts-bean" prefix = "bean" %>
Modify the execute segment in UserLoginAction as follows, as shown in Figure 12:

OK, complete... Next we will test the heat project...

Like adding a Struts framework for a Project, right-click the Project name and choose MyEclipse --> Add and Remove Project development.
In the dialog box that appears, click add. In the New Deployment dialog box that appears, select Tomcat5 as the Server and click Finish to complete the Deployment:

Open http: // 127.0.0.1: 8080/LoginDemo/userLogin. jsp in a browser
Enter Password 123456, username EricHe. Displayed successfully:


If you enter an error or do not enter an error, the system returns to the current logon page (because the error message is not set ).
Related Article

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.