[Struts2 Study Notes] Section 5: Compile the struts2 action Code and struts2 Study Notes

Source: Internet
Author: User

[Struts2 Study Notes] Section 5: Compile the struts2 action Code and struts2 Study Notes

Address: http://blog.csdn.net/sushengmiyan/article/details/40479299

Official documents: http://struts.apache.org/release/2.3.x/docs/coding-struts-2-actions.html

Author: sushengmiyan

Certificate ------------------------------------------------------------------------------------------------------------------------------------

As a matter of fact, I have learned the basic part of struts2, and I personally feel that I can have an intuitive understanding and understanding of the first four articles, so that I can use struts normally in the application. Other struts2 features, you can think about the API for a long time.

Now, let's explain a tutorial on struts2, which can be used as an alternative. It can be used as a reference to guide others in learning struts2.

Writing struts2 code requires only three steps:

1. Map an action to class

The action ing between action and class is configured in struts. xml. The previous configuration is as follows:

<action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">    <result name="success">/HelloWorld.jsp</result></action>


The preceding configuration specifies that a hello action corresponds to org. apache. struts. helloworld. action. HelloWorldAction class.


2. Map a result to view

<result name="success">/HelloWorld.jsp</result>
This is to map the success result to the view HelloWorld. jsp.

3. Compile the action processing logic

public String execute() throws Exception {             messageStore = new MessageStore() ;             helloCount++;             return SUCCESS; }
This is a method corresponding to the class and the place where transaction logic is processed. Based on your processing, return the processing result, such as success



In this case, it is necessary to explain the entire process:

First, log on to the interface and accept the user's input tag data input (user name and password)

Then, based on the struts. xml configuration file, find the set method of the corresponding user name and password, and set the input value to the corresponding class object.

Then, the httprequest method is called to obtain the input data (username and password) of the saved object)

Then execute the execute method and return the processing result (such as success)

It is best to display the view to the user (result. jsp) based on the processing result)


This is the entire process of struts2. I feel familiar with this process. It is very easy to add struts2 in my own program.


How struts2 action transfers data to JSP pages

Read your post

Let's talk about the shortcomings of your encoding.
Map request = (Map) ActionContext. getContext (). get ("request ");
Since you know that a request is an existing object, you should not use the request name when defining the object, which may cause misunderstandings to others and yourself.

You can use the struts2 label to do this.
<S: iterator value = "request">
<S: property value = "empId"/>
<S: property value = "eName"/>
<S: property value = "eSex"/>
<S: property value = "eSex"/>
<S: property value = "eSalary"/>
</S: iterator>

Struts2 configuration and action writing

The method declaration must conform to the following pattern, public, return String, no parameter. Exception can be thrown.

Public String foo () {return "success ";}
Public String foo () throws Exception {return "success ";}

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.