Getting started with struts2, the "Add User" applet is interpreted and analyzed as a program.

Source: Internet
Author: User

During the recent Chinese New Year, I was so confident that I had brought two jquery books back to check out. I found that most of the basic applications in the book would be useful, and I didn't want to read them too hard. I had no atmosphere !!!

Okay, let's get started.

The previous article solved the configuration in struts2. This time, let's start a small program.

First of all, the struts2 structure must be familiar with first. It is a typical MVC Framework. My understanding of business processes is as follows:

Well, a request page. JSP-> send the request to your servlet container, that is, your server. The request goes through the "filter" (in the web. <filter-class> Org. apache. struts2.dispatcher. ng. filter. strutsprepareandexecutefilter </filter-class>) to determine whether the request needs to call the action. If necessary, read struts. the corresponding action configuration in XML, such as where this action class exists; after being passed to the action, the result is returned after the action method is executed. This result is generally "success" "input ", the returned results are still identified in struts. in the xml configuration information, find what needs to be done for the corresponding returned results.

Then write the program according to the above idea:

First, write a user class. There is no need to explain this. You will know what to do with all kinds of information.


The next step is to write the action class and create a class createuseraction. Java

Here, the struts action package is required.

The execute method here is the default method executed when the action is called. It will be explained later. The crowdsourced Security Testing Platform calls action to execute this method.

In this method, determine whether the user name is null. If it is null, input is returned. If it is not null, success is returned. These are two common return values of action.

The implements here indicates that this class is required to implement the action interface.


The next step is a preliminary understanding of struts principles:

Create a page that is used to enter the user name and password createuser. jsp


The value of the action attribute of the form tag is createuser. Action, which means this action is executed when the form is submitted. But isn't the action a class just now related to it? Do not worry. When submitting a form and executing an action, go to the Struts. xml configuration file to find the createuser. Action:

Therefore, you need to write in struts. xml:

<Package name = "tutorial" extends = "struts-Default">
<Action name = "createuser" class = "register. createuseraction">
<Result name = "success"> createresult. jsp </result>
<Result name = "input"> empty. jsp </result>
</Action>
</Package>

Define the Action Unit under the package Tag: The action name is createuser, And the createuseraction. Java class is executed under the Register package.

You can also set method = "XX ()" to indicate the methods in the action to be called. The default method is the execute () method ..

The action tag contains the input or success type of the returned value. Both types correspond to the next page to jump.

Now, the corresponding jump page is written. After reading the above action code, the input returned result indicates that the user name is blank and the jump page is empty. JSP casually writes a simple little white JSP and adds a sentence: the user name cannot be blank. That's all. The createresult. jsp page jumps to when the user name is not empty.

The user's username and password must be displayed on this page. To call the data in struts, you need to call struts at the beginning of the page declaration:

<% @ Taglib prefix = "S" uri = "/Struts-tags" %>

The subject content is as follows:

<Body>
User Added! <Br/>
Username: <s: property value = "user. username"/> <br/>
Password: <s: property value = "user. Password"/>
</Body>

<S: property is a tag library of struts2, which searches for corresponding operations based on the values in the value:

User. Username: Call getuser () in action and GetUserName () in user. Java to obtain data.



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.