Myeclipse struts tutorial

Source: Internet
Author: User

1. Order

This document is applicable to the Environment Sun JDK 1.4.2, eclipse 3.0.1, and myeclipse 3.8.2. All screens are based on the default user interface settings of Eclipse, myeclipse enterprise workbench, and Windows XP. If you encounter any difficulties when using this document, please refer to the user feedback section to feedback to myeclipse documentation team.

2. Description

In this user guide, we will use myeclipse enterprise Workbench to develop and test a simple struts sample application. This does not require us to have the preparation knowledge of struts or myeclipse.

3. required tools

JDK 1.4 + (sun or IBM)

Http://java.sun.com/j2se/downloads/index.html

Eclipse 3.0.x SDK

Http://www.eclipse.org/downloads/index.php

Myeclipse 3.8.2

Http://www.myeclipseide.com/ContentExpress-display-ceid-10.html

Tomcat 5.0.x (preferred 5.0.28 P referred, or other Servlet/EJB containers)

Http://apache.xtelli.net/jakarta/tomcat-5/v5.0.28/bin/jakarta-tomcat-5.0.28.exe

4. construct a new project

To effectively organize our development, we need to create a newWeb module project .You can create a web project through the menuFile> New> Other> Project> J2EE> Web project, As shown in

Figure 1 --- NewWEB ProjectDialog Box

Enter all information about the new project, such:

Figure 2 ---- settingsWEB Project

Once a web project is created, we need to add struts capabilities for myeclipse. This can be done by right-clickingPackage ExplorerView, and selectMyeclipse> Add struts capabilities. As shown in the following figure:

Figure 3:WEB ProjectAddStruts capabilities

For this application, the default value of the struts dialog box is very suitable. However, you can change the base package for new classes to suit your situation. As shown in figure 4, we simply keep these default values.

Figure 4 ---- configure struts capabilities

After completing the preceding steps, the project structure is shown in Figure 5.

Figure 5 ---- project structure view after Configuration

Now we have successfully created a project, and then we can start to build Web content. Now we are going to the next step.

5. Start a struts Project

In this section, we will focus on creating a struts application example. This strength is imitating a simple website login screen. Therefore, only two JSP pages are required, one prompting the user to log on, and the other briefly indicating that the logon is successful. We name the two JSP pagesUserlogin. jspAnd userl oginsuccess. jsp. If a user attempts to log on with an authorization error, the user is instructed to return loginuser. jsp directly and the error message is displayed.

Generally, it is useful to design the application process when you start a struts project. In this way, the development team will have a better understanding of the combination of all elements in the project. The easiest way to create a process is to use the struts graphical design mode. The struts Editor provides a drag-and-drop tool that allows you to quickly copy pages to construct applications. The login process of this application example is shown in Figure 6.

Figure 6 ----- application process

We can see from the design view that this application is displayed on the userlogin. jsp page. This logon page will call the userlogin action to perform the user's logon operation. If a verification error or problem occurs, userlogin Action directs the user to return to the userlogin. jsp page. However, if the logon succeeds, the application will go to the userloginsuccess. jsp page.

5.1 struts project components

A typical struts project includes the following components:

JSPs

Actions

Actionforwards *

Actionforms **

Struts deployment file: struts-config.xml

In myelcipse, you can create all or one of the components (except struts-config.xml) in three different ways)

Method 1: Use the menu File> New> other...> J2EE> Struts 1.1 (or 1.0), for example:

Figure 7

The dialog box directs you directly and prompts you all the values supported by struts components. Some parts are more complex than others. For example, in the dialog box shown in figure 8, you can see that all the features supported by struts action have been assigned a value that is easy to understand.

Figure 8-Dialog Box: Create a NewStruts action

Method 2: Use the Outline View. From this view, you can right-click any component type pop-up menu to create a new component of this type. Figure 9 is shown below:

Figure 9 ---- Outline View

Method 3: The struts Graphical Editor (such as 10) is also a convenient way to create struts components. Open the struts-config.xml file and click the design tab below the editor to open the struts Graphical Editor

Figure 10 ----- open the struts Graphical Editor:

Go to the editing mode page shown in 11:

Figure 11 ----- struts graphical design mode View

Now we know how to create different components of the struts component. We can create different parts of the login application. Next

5.2 build an application

We can create a JSP page in a centralized manner to build an example application. Since our application simulates a website login interface, we only need two JSP pages: userlogin. jsp and userloginsuccess. jsp. Like most struts applications, if an error occurs during the login process, the user is guided back to userlogin. jsp and an error is prompted (we do not need a special error page userloginfailure. jsp ).

First, create userloginsuccess. jsp. It looks like creating the final page first. We can use new form, action and JSP to create userlogin. jsp, which is associated with action and actionform.

12. Create userlogin. jsp in struts design mode.

Figure 12 ---- create a JSP page

After clicking, the dialog box shown in Figure 13 is displayed.

Note: You must select standard JSP using Struts 1.1.

Figure 13 ---- configure userloginsuccess. jsp

After you click Finish, the design attempt displays the newly created page. Example 14

Figure 14

To complete this JSP page, only information indicating that the user has successfully logged on is output! The source code of userloginsuccess. jsp is given below:

Userloginsuccess. jsp

<% @ Page Language = "Java" %>

<% @ Taglib uri = "http://jakarta.apache.org/struts/tags-bean" prefix = "Bean" %>
<% @ Taglib uri = "http://jakarta.apache.org/struts/tags-html" prefix = "html" %>
<% @ Taglib uri = "http://jakarta.apache.org/struts/tags-logic" prefix = "logic" %>
<% @ Taglib uri = "http://jakarta.apache.org/struts/tags-tiles" prefix = "tiles" %>
<% @ Taglib uri = "http://jakarta.apache.org/struts/tags-template" prefix = "template" %>
<% @ Taglib uri = "http://jakarta.apache.org/struts/tags-nested" prefix = "nested" %>

<! Doctype HTML public "-// W 3C // dtd html 4.01 transitional // en">
<HTML: HTML locale = "true">
<Head>
<Title> my struts 'userloginsuccess. jsp 'ending page </title>
</Head>

<Body>
Hello <Bean: write name = "username" Scope = "request"/>, you successfully logged in!
</Body>
</Html: HTML>

This page is very simple. The only thing we need to do is the content of the <body> label: output the value of the variable username, which is stored in the application request process. Therefore, in the action to be created later, we need to place a username attribute in the request process.

At this moment, we also need to create userlogin. jsp, actionform, and action. It seems a lot of work, but myeclipse can easily create them through the new form and new form, action and JSP dialog box.

Continue to build this application and right-click the blank area of the struts designer canvas. Select New, and then select new form and action and JSP, as shown in Figure 15:

Figure 15 ------- startNew Form, action, JSP

The new form dialog box appears first. Be sure to enter the User case name. 16 shows the value of the User case name you want to enter

Figure 16

We will add two form attributes: username and password. When adding a password field, select JSP input type as password. 17:

Figure 17 ----- add attributes to a form

Figure 18 ----- form attributes

Before clicking Next, you must click the JSP tag and perform the same operations as 19.

Figure 19

Finally, click the methods label. Do not select any method. For example, 20

Figure 20

After you click Next, the new action dialog box appears. You can see that most of the values have been filled in for you. Figure 21 shows the dialog box. As follows:

Figure 21

You can click the forwards tab to specify actionforwards. Example 22

Figure 22

After adding forwards for this action, click Finish. Myeclipse creates resources and updates the struts-config.xml file. Figure 23 shows the modified structure.

Figure 23

Now the system process has been determined, and we need to add logic for this action to control the login process. You can quickly jump to any resource by double-clicking. As shown in 24

Figure 24

When you open userloginaction. Java for the first time, the code automatically generated for the execute () method is as follows:

Userloginaction. Java

Public actionforward execute (
Actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response ){
Userloginform = (userloginform) form;
Throw new unsupportedoperationexception (
"Generated method 'execute (...) 'not implemented .");
}

Modify the execute () method of userloginaction. Java as follows:

Userloginaction. Java

Public actionforward execute (
Actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response ){
Userloginform = (userloginform) form;

If (userloginform. GetUserName (). Equals ("myeclipse") & userloginform. GetPassword (). Equals ("myeclipse "))
{
Request. setattribute ("username", userloginform. GetUserName ());
Return Mapping. findforward ("success ");
}

Return Mapping. findforward ("failure ");
}

Here we have done a very simple thing, just to check whether username and password are both myeclipse. If yes, the username is stored in the request and switched to: success, so userloginsucces. jsp will display personalized information. Otherwise, the system will switch to failse after an error occurs. In practice, you should add actionmessages or actionerrors to explain what happened.

6. Run

Now that the application has been completed, we need to deploy it to Tomcat 5. Open the package explorer view, right-click the project, select myeclipse> add and remove project deployments, or click the deploy J2EE project to server button in the toolbar. For example, 25

Figure 25

After deployment, check the deployment status to ensure proper deployment. You can follow figure 26

Figure 26

Finally, start Tomcat to use the application server as shown in Figure 27.

Figure 27

When the server is started, the eclipse console view displays the startup status. 28

Figure 28

When Tomcat 5 runs, we can use the browser view of myeclipse to test the sample application. Select menuWindow> show View> other...As shown in 29

Figure 29

Enter the URL in the address bar, as shown in

Figure 30

It indicates that the program can run normally. Enter username and passwod according to 31

Figure 31

After logon, we are guided to the successful logon interface. 32

Figure 32

7. Summary

In this example, we developed a simple struts application using the struts tool provided by myeclipse enterprise workbench.

We can see the benefits of myeclipse: efficiency and convenience

 

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.