---
title: "《研磨struts2》第四章 Action 之 4.1 Action基础 "
author: "研磨struts2"
date: "2012-04-22T07:14:47Z"
tags: ["Struts2"]
categories: ["企业架构"]
slug: "struts2-action-4-1-action"
draft: false
lastmod: "2012-04-22T07:14:47Z"
id: 1493711
url: "https://rubbing-struts2.iteye.com/blog/1493711"
---

## 4.1  Action基础

### []()4.1.1  Action是什么

在Struts2中，一个Action类代表一次请求或调用，每个请求的动作都对应于一个相应的Action类，一个Action类是一个独立的工作单元。

也就是说，用户的每次请求，都会转到一个相应的Action类里面，由这个Action类来进行处理，因此说一个Action类代表了用户的一次请求或调用。

       换句简单的话来说，Action就是用来处理一次用户请求的对象。

### []()4.1.2  Action能干什么

认识了Action是什么过后，来看看Action能干什么。

1：先看看前面HelloWorld中实现的Action对象，示例代码如下：

 

java代码：

[查看](http://sishuok.com/forum/blogPost/list/4045.html)[复制到剪贴板](http://sishuok.com/forum/blogPost/list/4045.html)[打印](http://sishuok.com/forum/blogPost/list/4045.html)

01. public class HelloWorldAction implements Action {
02. private String account;
03. private String password;
04. private String submitFlag;
05. public String execute() throws Exception {
06. //1：收集参数，不用做了，数据会直接映射到上面的属性里面
07. //2：组织参数，暂时不需要，先不做
08. //3：调用模型的逻辑功能处理，这里不需要，只是简单的输出一下传入的参数
09. System.out.println("用户输入的参数为==="+"account="+account
10. +",password="+password+",submitFlag="+submitFlag);
11. //4：根据逻辑处理的结果来选择下一个页面，这里直接选择转向欢迎页面的result
12. return "toWelcome";
13. }
14. public String getAccount() {
15. return account;
16. }
17. public void setAccount(String account) {
18. this.account = account;
19. }
20. public String getPassword() {
21. return password;
22. }
23. public void setPassword(String password) {
24. this.password = password;
25. }
26. public String getSubmitFlag() {
27. return submitFlag;
28. }
29. public void setSubmitFlag(String submitFlag) {
30. this.submitFlag = submitFlag;
31. }
32. }

根据上面的Action实现，你会发现，在Struts2里面，Action充当着MVC中模型的角色，也就是说Action既封装了业务数据，又要处理业务功能。当然，在实际的JavaEE开发中，逻辑部分会放到逻辑层去实现，这就演变成Action只是去调用逻辑层来进行业务逻辑的处理，并不是真的在Action里面去实现业务逻辑的处理。

       上面这个Action的属性和属性对应的getter/setter方法，就是用来接收用户请求的数据，并把这些数据封装在Action中，在后续处理中可以访问这些数据。

再仔细察看上面Action的实现中的execute方法的实现，你会发现execute方法里面实现的功能，正是前面学习的MVC的控制器部分的功能。因此，从另外一个角度来说，Struts2的Action也充当着MVC中控制器的角色。

**2：那么到底Struts2的Action相当于MVC中的控制器部分还是模型部分呢？**

虽然两种说法都过得去，但是目前大家的共识是把Struts2的Action作为MVC中的模型角色来看待。

原因在于，在Struts2运行的时候，是由Struts2中的前端控制器FilterDispatcher分发并调用相应的Action的，此时这个Action仅仅相当于一次请求的命令处理，层面比较小，再加上已经有FilterDispatcher来做控制器进行分发调度了，因此，一般不把Struts2的Action当作MVC的控制器来看，而是当作MVC的模型部分来看。

### []()4.1.3  Action的基本配置

不管Action采用何种实现方式，要正确运行，都需要在struts.xml中进行配置，这是使用Action的基础。

就以前面HelloWorldAction为例来看看Action的基本配置，复杂的配置在后面有详细介绍。HelloWorldAction的定义如下：

 

java代码：

[查看](http://sishuok.com/forum/blogPost/list/4045.html)[复制到剪贴板](http://sishuok.com/forum/blogPost/list/4045.html)[打印](http://sishuok.com/forum/blogPost/list/4045.html)

1. package cn.javass.hello.struts2impl.action;
2. public class HelloWorldAction implements Action {
3. //省略了
4. }

那么此时在struts.xml中，Action对应的配置为：

 

java代码：

[查看](http://sishuok.com/forum/blogPost/list/4045.html)[复制到剪贴板](http://sishuok.com/forum/blogPost/list/4045.html)[打印](http://sishuok.com/forum/blogPost/list/4045.html)

1. &lt;action name="helloworldAction" class="cn.javass.hello.struts2impl.action.HelloWorldAction"&gt;
2. &lt;/action&gt;

对于Action本身的配置非常简单，只需要配置Action的名称，和Action类的全路径名，简单点说就是：一个类的全路径名=这个类的包路径+“.”+这个类的名称。

 

私塾在线网站原创《[研磨struts2](http://sishuok.com/forum/blogCategory/showByCategory.html?categories_id=81&user_id=5948)》系列

转自请注明出处：【[http://sishuok.com/forum/blogPost/list/0/4045.html](http://sishuok.com/forum/blogPost/list/0/4045.html)】

欢迎访问[http://sishuok.com](http://sishuok.com/)获取更多内容