Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure Development Struts in Eclipse

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to configure the development Struts in Eclipse. I hope you will get something after reading this article. Let's discuss it together.

Demand

The Struts application example to be combined in this article is user login (logon), and the requirements for user login (logon) are:

1 receive the user name and password, jump to the success page and prompt "Welcome to login!" .

2 if the form is submitted if the user name is empty, prompt "Please enter the user name!" .

3 if the password is empty, submit the form, then prompt "Please enter the password!" .

4 if you enter an illegal user name, you will prompt "Please enter the correct user name!" .

5 save the login information through the model component.

Introduction to the framework of logging in to an application instance

The user login (logon) application consists of the following modules:

1 Model (M): the model is a JAVABEAN component-SaveLogon, which contains two properties: username (user name) and userpassword (password), provides a method to read and set properties get/set, and a method to save login information saveToDb (). The task of saveToDb is to save login information to a persistent storage system (such as a database).

2 View (V): the view consists of two JSP files and an ActionForm Bean-logonForm. The JSP files are: logon.jsp and success.jsp. Logon.jsp is responsible for accepting the user name and password entered by the user and displaying error messages, while success.jsp is responsible for displaying login success information.

3 Controller (C): the controller is Action class logonAction. Carry on the business logic verification to the data, call the model component to save the login information, select the view component to return to the user.

The working Mechanism of Struts

The user enters the user name and password in the view component (logon.jsp) and submits the HTML form. The Struts framework first reads the configuration of servlet in WEB.XML, and then automatically encapsulates the form data into ActionForm Bean, in this case logonForm. LogonForm validates the submitted data (for example, the minimum length, whether it contains illegal characters, etc.). If the ActionForm Bean validation fails, the logonForm returns the ActionErrors object, and the Struts framework saves the ActionErrors object in the request scope, forwards the request to the view component, and displays the error message contained in the ActionErrors object in the request scope through the tag in the view component. If validated by ActionForm Bean, the ActionErrors object returned by logonForm submits the form data to the controller component logonAction for the NULL,Struts framework, and the controller validates the form data with business logic. If the logonAction verification fails, the ActionMessages object containing the error message is created, and the ActionMessages object is saved to the request scope through the saveErrors method, and the request is forwarded to the view component through the tag in the view component. Show the error message contained in the ActionErrors object within the scope of request If the logonAction authentication passes, the Struts framework accesses the model component, and then the Struts framework returns the ActionFoward object and decides which view component to pass the forwarding information contained in the ActionFoward object to, depending on the configuration of the Struts-config.xml file.

Deploy the Struts framework

Now that you have some understanding of how Struts works, let's introduce how to deploy the Struts framework in Eclipse.

The environment of this article has been mentioned earlier, and the installation of Eclipse and its plug-ins MyEclipse and TomcatPlugin is not covered in this article.

1 create a new project

1) Select Java > > Tomcat Project, and the next step is shown in figure 1

(figure 1)

2) fill in the project name. If you do not use the default value of the directory where the project is located, please modify it and click finish. Figure 2

(figure 2)

2 create a Struts framework

1) right-select Project > > New > > other > > MyEclipse > > Web-Struts > > Struts 1.2 > > Struts 1.2 Form,Action & JSP. As shown in figure 3. If there is an error in this step, which is not described in this article, please refer to my other article-the solution to the error in creating a Struts in MyEclipse.

(figure 3)

2) configure Struts ActionForm

Because the Struts application example in this article is a user login, Use case chooses logon. The Superclass selection is shown in the figure, and the other defaults. Figure 4

(figure 4)

Next, fill in the Optional Details and enter the Form form item, in this case, the user name (username) and password (userpassword). Click Add, please refer to figure 5 and figure 6 for input.

(figure 5)

(figure 6)

After the input, it will be displayed in the Form Properties of Optional Details. Figure 7

(figure 7)

We use the default settings for the Methods tab and do not modify it.

Click the JSP tab and check the "Create JSP form" option with the default setting "/ form/logon.jsp". Figure 8

(figure 8)

3) configure Struts Action

The above steps have completed the configuration of Form, and the following is to configure Action.

Click next in figure 8 and use the default. As shown in figure 9

(figure 9)

The Parameter and Methods options are also default.

Click the Forwards tab and click Add to configure the success page information. In this paper, the success.jsp of the root directory is selected as the success page, and Name is logon_sc. As shown in figure 10

(figure 10)

After the input, it will be displayed in the Forwards of Optional Details. As shown in figure 11

(figure 11)

Click finish.

Struts has been configured. Take a look at Eclipse, there are a few more things in the project, such as struts-related jar, as well as struts-config.xml and tld files. The code for struts-config.xml is as follows:

Listing 1 (struts-config.xml)

< struts-config >

< data-sources / >

< form-beans >

Share To

Development

© 2024 shulou.com SLNews company. All rights reserved.

12
Report