struts1.3--using MyEclipse integration tools to develop struts

Source: Internet
Author: User

The first two articles by hand writing code, manually configured Struts-config.xml file method to develop a simple struts project, through the manual way to learn the underlying principle of detail. When we are familiar with it, we don't need to develop struts in a purely manual way, MyEclipse provides us with a convenient integrated development tool.
The steps to automatically develop struts through MyEclipse are described below. The login verification system is still used here to illustrate.

1. Build a Web Project

The first step is to create a Web project, which is the first of all projects. Here I new project Strutsloginbytool

2. Introduction of the Struts development kit

(1) Right-click on the project, then tap Myeclipse->project facets[capabilities]->install Apache Struts (1.x) Facet (This step for MyEclipse2015 Stable version 2.0, different versions of the steps may differ slightly.

(2) Select the Struts 1.3 version in the Pop-up dialog box.

(3) Select the path of the Struts-config.xml file in the next dialog box, select some configuration such as URL pattern.

(4) Clicking Finish will automatically introduce the Struts development package and automatically generate the Struts-config.xml file, and complete some configuration. The current project directory is as follows:

As you can see, the struts development package that it automatically introduces is 1.3.8 version and generates a Struts-config.xml file.

After this step, MyEclipse helped us automatically configure the Struts development environment, which we can already develop using struts in the project.
We can still develop actionform, Action, and manually configure the Struts-config.xml file in the process later. But MyEclipse's strength lies in the fact that it can be done automatically, simplifying the hassle of our manual work.

The following is a purely automated way to develop.

3. Development login.jsp

Develop a simple login page: login.jsp, the JSP is still placed in the Web-inf directory, forwarded to the page by index.jsp. As in the previous article, the core code is as follows:

<body>    <form Action="??" Method="POST">UserName:<input type="text" name="username"/><br><br>Password:<input type="password" name="password"/><br><br>        <input type="Submit" value="Login"/>        </form>  </body>

The address of the action submitted here is not yet known, so let's put it here for the time being. The two form components are named username and password, and these two names are critical in struts, which are used when populating the form.

4. Development of welcome.jsp and error.jsp

Conveniently put welcome.jsp and error.jsp also write, for after landing after the verification of the jump target page.

5. Developing Actionform and action

(1) We open the Struts-config.xml file, the file can Design be Source written in two ways, the Design pattern is we automatically develop Actionform and Action window, and Source mode let us directly edit the source code.

(2) in the design mode of the blank right click, select New, you can see it can create new action, you can create a new form, etc.

(3) We create a new form, creating a UserForm, such as:

(4) Create and UserForm The associated action with the name Loginaction, such as:

(5) After completing the above steps, we can see what MyEclipse has done for us:

<1> look at the project directory, which automatically generates the code for UserForm and Loginaction:

<2> look at the Struts-config.xml file, in the Design mode, you can see that it has more than one loginaction view, in the Source mode can see the source code, It has helped us to configure UserForm and Loginaction and their correlation.

6. Design Configure the jump relationship in Struts-config.xml mode

(1) Drag login.jsp, welcome.jsp, and error.jsp to the Struts-config.xml view, such as:

(2) Select the Add Connection tool on the left, configure the login.jsp to loginaction input relationship by wiring, and configure the forwarding relationship loginaction to welcome.jsp and error.jsp. For the welcome.jsp, the forwarded judgment name is ok , while the forwarding to error.jsp is judged by the nameerror

(3) The final form of the diagram is as follows:

(4) We can take a look at the source code of Struts-config.xml, which in loginaction gives us the automatic configuration of the forward relationship:

7. Add business logic in the Excute method of Loginaction

Determine if the user authentication passed, as follows:

publicexecute(ActionMapping mapping, ActionForm form,        HttpServletRequest request, HttpServletResponse response) {    UserForm userForm = (UserForm) form;// TODO Auto-generated method stub    if("123".equals(userForm.getPassword())){        return mapping.findForward("ok");    }else {        return mapping.findForward("error");    }}

8. Don't forget to add the action address of the form in login.jsp, our loginaction path is /login , so we should write/StrutsLoginByTool/login.do

<body>    <form Action="/strutsloginbytool/login.do" method="POST"> UserName:<input type="text" name="username"/><br><br>Password:<input type="password" name="password"/><br><br>        <input type="Submit" value="Login"/>        </form>  </body>

9. Testing

Run the project and find that it works as expected with manual write code configuration.

You can see that we've only written 3 simple JSP pages, and a little bit of simple business logic code. And all of the others are myeclipse help us do, generate action and actionform, configure Struts-config.xml, and so on.

So, struts simplifies the hassle of writing and configuring jump relationships, and lets us focus on the writing of business logic. Of course such a simple login verification is only a very small example, and the real complexity of the project is the business logic.

This example is intended to introduce the general process of developing struts with integrated tools.

Why is struts a web framework?

Struts traverses the web layer because struts takes over the components of the Web layer, including JSPs, forms, actions, and so on, so that struts is called the Web layer framework, but struts does not affect the following business logic layer, DAO layer, and Database layer. These levels will have other frameworks to take over in the future.

struts1.3--using MyEclipse integration tools to develop struts

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.