读书人

Struts 二 Actions介绍

发布时间: 2012-09-19 13:43:54 作者: rapoo

Struts 2 Actions介绍
当客户端请求与action的name相匹配时,框架会使用struts.xml文件中的映射来处理请求.通常指向action的映射是由标签生成的.action标签(在struts.xml文件的struts根节点中)通过name,框架提供的默认扩展和其它必须的信息来指定action.

Handler类的默认进入方法是由Action接口定义的.

Struts2 Action接口 :

所有的Action可能会实现这个拥有execute()方法的接口.你可以随意创建POJO来维护相同的由这个接口定义的契约而不需确实实现这个接口.



Java代码
package com.opensymphony.xwork2;
public interface Action {
//The action execution was a failure.
public final static String ERROR;
//The action execution require more input in order to succeed
public final static String INPUT;
//The action could not execute, since the user most was not logged in
public final static String LOGIN;
// The action execution was successful but do not show a view.
public final static String NONE;
//The action execution was successful.
public final static String SUCCESS;
//the logic of the action is implemented
public String execute() throws Exception;
}

package com.opensymphony.xwork2;
public interface Action {
//The action execution was a failure.
public final static String ERROR;
//The action execution require more input in order to succeed
public final static String INPUT;
//The action could not execute, since the user most was not logged in
public final static String LOGIN;
// The action execution was successful but do not show a view.
public final static String NONE;
//The action execution was successful.
public final static String SUCCESS;
//the logic of the action is implemented
public String execute() throws Exception;
}



实现Action接口是可选的.如果Action没有被实现,框架会调用反射来寻找execute()方法.如果没有execute()方法并且没有其它方法在配置文件中被指定,框架会抛出异常.

Struts2引入了一个简单的实现使得Action类很接近POJO(简单Java旧对象).一个Action最基本的使用方法就是在工作时始终有一个单一的结果被返回,就像这样 :




Java代码
public class NewAction {
public String execute() throws Exception {
// do the work
return "success";
}
}

public class NewAction {
public String execute() throws Exception {
// do the work
return "success";
}
}

注意这里Action类并没有继承任何类或接口.一个Action执行时调用的方法就是"execute"方法."execute"没有任何参数且返回一个String对象.然而在Struts2中如果你使用了helper接口(Helper提供了常用的常量结果像是"success", "none", "error", "input"和"login"),你就能得到不同的返回类型而不仅仅是String对象.


Action类经常起着模块的作用,并依据请求对象和输入的参数来执行指定的业务逻辑.在早期的Struts版本中(早于Struts2.0),一个Action类应该继承org.apache.struts.Action,并且需要重写含有4个参数的Action.execute()方法.


基本上,Action意味着处理若干对象像HttpServletRequest, HttpServletResponse.但在这,我们的Action没有处理任何对象参数.这就产生了一个重要的问题,你是如何访问你想要用来工作的对象的.答案就在于"控制反转"或"依赖注入"模式.


为了提供一个松耦合的体系,Struts2用了一种叫依赖注入或叫控制反转的技术.依赖注入可以由构造器注入,接口注入,setter注入来实现.Struts2使用setter注入.这就意味着要在Action中使用对象,我们仅需提供一个setter方法.同样有些对象如HttpServletRequest可以通过访问ActionContext或实现ServletRequestAware来获得.首选继承ServletRequestAware.任何与业务无关的对象都有一个相对应的Action需要实现的接口(就是aware接口)



为了更好的理解反转控制,让我们看个处理Action请求访问当前请求HttpServletRequest对象的例子.在这个例子中依赖注入的途径是接口注入.通过名称的暗示,我们知道使用接口注入必须要有个接口被实现.该接口包含setter方法,用来向Action提供数据.在我们的例子中我们使用ServletRequestAware接口,就是这个 :



Java代码
public interface ServletRequestAware {
public void setServletRequest(HttpServletRequest request);
}

public interface ServletRequestAware {
public void setServletRequest(HttpServletRequest request);
}


当我们实现这个接口时,我们简单的Action得到了一点点修改,但是现在我们有了一个HttpServerRequest对象可以使用.



Java代码
ublic class NewAction implements addDependency {
private HttpServletRequest request;
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public String execute() throws Exception {
// do the work using the request
return "SUCCESS";
}
}

ublic class NewAction implements addDependency {
private HttpServletRequest request;
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public String execute() throws Exception {
// do the work using the request
return "SUCCESS";
}
}


在Struts2中,任意请求都会创建一个Action的实例.它不是共享的,在请求完成是,它会被丢弃.


Action映射

Action映射可以指定一系列结果类型,一系列例外处理程序和拦截器栈.但只有name属性是必须的.其它属性都可以在package作用域中得到.通过struts.xml文件指定.

这个Action的配置文件就像这样 :



Xml代码
<action name="new" >
<result>view.jsp</result>
</action>

<action name="new" >
<result>view.jsp</result>
</action>

name属性以new.action的形式提供URL信息来调用Action.扩展名.action是在struts.properties中配置的.class属性提供了调用的Action的名字和所在包的全名.


Struts2像处理POJO一样处理Action,而且允许返回不同的结果----这取决于逻辑的结果.想要得到不同结果,类应该这样修改 :



Java代码
class NewAction
{
public String execute() throws Exception
{
if (its-ok())
{
return "login";
}
else
{
return "none";
}
}
}

class NewAction
{
public String execute() throws Exception
{
if (its-ok())
{
return "login";
}
else
{
return "none";
}
}
}


现在类提供了两个不同的结果,我们需要在struts.xml中为每一种情况配置action标签.像这样修改配置 :



Xml代码
<action name="new" >
<result>login.jsp</result>
<result name="none">none.jsp</result>
</action>

读书人网 >软件架构设计

热点推荐