Struts2 官方教程之Coding Struts 2 Actions(十)

来源:互联网 发布:mac air忘记开机密码 编辑:程序博客网 时间:2024/05/17 03:05

This tutorial assumes you've completed the Using Struts 2 Tags tutorial and have a working Using_Tags_Struts2_Ant (or Using_Tags_Struts2_Mvn) project. The example code for this tutorial, Coding_Actions_Struts2_Ant or Coding_Actions_Struts2_Mvn, is available on Google Code - http://code.google.com/p/struts2-examples/downloads/list. After downloading and unzipping the file, you'll have a folder named Coding_Actions_Struts2_Ant (or Coding_Actions_Struts2_Mvn). In that folder will be a README.txt file with instructions on now to build and run the example application.

假设你已经做过之前的小练习,并且给出了下载地址。

Introduction

Coding a Struts 2 Action involves several parts:包含下面三个步骤

1. Mapping an action to a class     
2. Mapping a result to a view
3. Writing the controller logic in the Action class

In the previous tutorials we covered how to configure Struts to map a URL such as hello.action to a Action class such as HelloWorldAction (specifically the execute method).

下面是action配置

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


 

The Action mapping above also specified that if the execute method of class HelloWorldActionreturns success then the view HelloWorld.jsp will be returned to the browser.

This tutorial will introduce you to the basics of writing the controller logic in the Action class.

Struts 2 Action Classes

Action classes act as the controller in the MVC pattern. Action classes respond to a user action, execute business logic (or call upon other classes to do that), and then return a result that tells Struts what view to render.

Action classes在MVC模式中充当Controller。它会响应一个用户请求,执行业务逻辑,然后会返回一个视图。

Struts 2 Action classes usually extend the ActionSupport class, which is provided by the Struts 2 framework. ClassActionSupport provides default implementations for the most common actions (e.g. execute, input) and also implements several useful Struts 2 interfaces. When your Action class extends classActionSupport your class can either override the default implementations or inherit them.

Struts 2 Action类沉淀会继承自一个struts2框架提供的ActionSupport类,ActionSupport类提供了已经实现的一些常用方法(例如,execute,input),以及一些常用的接口。

If you examine class HelloWorldAction from tutorial Using Struts 2 Tags you'll see that it extends classActionSupport and then overrides method execute.

In method execute is where we placed what we want this controller to do in response to the hello.action.

Method execute of HelloWorldAction
public String execute() throws Exception {messageStore = new MessageStore() ;helloCount++;return SUCCESS;}

 Note that method execute declares it throws an Exception. We'll cover in a later tutorial how to configure Struts to handle any Exceptions thrown from the Action classes methods.

Processing Form Input In The Action Class

One of the most common responsibilities of the Action class is to process user input on a form and then make the result of the processing available to the view page. To illustrate this responsibility, let's say that on our view page, HelloWorld.jsp, we want
to display a personal hello, such as "Hello Struts User Bruce."

In the Using Struts 2 Tags example application we added a Struts 2 form to index.jsp.

下面使用一个简单的登陆表单,并且使用标签,返回"Hello Struts User Bruce."

Struts 2 Form Tags
<s:form action="hello"><s:textfield name="userName" label="Your name" /><s:submit value="Submit" /></s:form>

Make a note of the value of the name attribute for the Struts 2 textfield tag, which is userName. When the user clicks on the submit button for the above form, the action hello will be executed (hello.action). The form field values will be posted to the Struts 2 Action class (HelloWorldAction). The Action class may automatically receive those form field values provided it has a public set method that matches the form field name value.

请注意名为userName的标签属性名,当用户点击sumbit按钮时,hello  action将会被执行,那么表单里的值就会被提交到action类,它会通过定义的set方法自动接收传过来的值,这是和action里定义的属性相对应的。

So for the HelloWorldAction class to automatically receive the userName value it must have a public method setUserName (note the JavaBean convention discussed in tutorialHello World).

set方法必须是public,这样才能自动接收。

For the example application associated with this tutorial add the following Java code to class HelloWorldAction.

Add userName to HelloWorldAction
private String userName;public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}

To personalize the MessageStore message (recall that class MessageStore is storing the message to display) add the following Java code to the HelloWorldAction's execute method after the statement that instantiates the MessageStore object.

Add userName value to message
if (userName != null) {messageStore.setMessage( messageStore.getMessage() + " " + userName);}

Now build and deploy the application. Enter your name in the form and click the submit button. You should see the following page.

When the form is submitted, Struts will call any set methods of the HelloWorldAction class that match the form field names. So in this example method setUserName was called and passed the value the user entered in the userName form field.

On the index.jsp we also have a Struts 2 action link (see tutorial Using Struts 2 Tags) that includes a query string parameter: userName=Bruce+Phillips. If you click on that link you should see the result of:

Since the query string parameter is userName, Struts passed the value of that parameter to the setUserName method.

On the view page, HelloWorld.jsp, you can also access the userName value by using the Struts 2 property tag (see tutorialUsing Struts 2 Tags). Try showing just the userName value on the view page.

Summary

This tutorial introduced you to how to code the Action class so it can process user input on a form or values in a query string parameter. If the form had numerous fields, it would be cumbersome to have a set method that matches up with each form field. So in our next tutorial will cover how to integrate a model class, form fields in the view, and form processing in the Action class.

原创粉丝点击