Struts2 configuration: Action Config[笔记]

本文转载自http://www.blogjava.net/nobody_am/news/2007/04/26/113924.html

The action mappings are the basic "unit-of-work" in the framework. Essentially, the action maps an identifier to a handler class. When a request matches the action's name, the framework uses the mapping to determine how to process the request.

Action映射是框架的最基本的工作单元,本质上,action映射一个id到处理类,当一个请求匹配action名称,框架用映射来决定怎样处理这个请求。

Action Mappings

The action mapping can specify a set of result types, a set of exception handlers, and an interceptor stack. But, only the name attribute is required. The other attributes can also be provided at package scope.

Action映射能够描述N多的result type,n多的Exception handle,和一个interceptor栈,但是,只有name属性是需要的,其他属性也能够在package级别提供。

A Logon Action
<action name="Logon" class="tutorial.Logon">
  <result type="redirect-action">Menu</result>
  <result name="input">/tutorial/Logon.jsp</result>
</action>

Action Names

In a web application, the name attribute is matched a part of the location requested by a browser (or other HTTP client). The framework will drop the host and application name and the extension, and match what's in the middle. So, a request for http://www.planetstruts.org/struts2-mailreader/Welcome.do will map to the Welcome action.

在一个Web应用中,name属性匹配来自浏览器的一系列本地请求。框架能够去掉主机和应用名,以及扩展名,来比配中间的那个名称,所以请求.....将映射Welcome Action。

Within an application, the link to an action is usually generated by a Struts Tag. The tag can specify the action by name, and the framework will render the default extension and anything else that is needed.

在一个应用里面,action的link通常有StrutsTag产生,tag能够用名字描述,框架将渲染默认扩展和其他它需要的任何东西。

A Hello Form
<s:form action="Hello">
    <s:textfield label="Please enter your name" name="name"/>
    <s:submit/>
</s:form>
Action Names With Slashes

If your action names have slashes in them (for example, <action name="admin/home" class="tutorial.Admin"/>) you need to specifically enable that functionality via the struts.xml file by specifying <constant name="struts.enable.SlashesInActionNames" value="true"/>. See JIRA Issue WW-1383 for discussion as there are side effects to setting this property to true.

Action Methods

The default entry method to the handler class is defined by the Action interface.

Handler类的默认入口方法被定义在Action 接口里。

Action interface
public interface Action {
    public String execute() throws Exception;
}

 Implementing the Action interface is optional. If Action is not implemented, the framework will use reflection to look for an execute method.

实现Action接口是可选的,如果没有实现,框架自动用反射技术来搜寻execute方法。

Sometimes, developers like to create more than one entry point to an Action. For example, in the case of of a data-access Action, a developer might want separate entry-points for createretrieveupdate, and delete. A different entry point can be specified by the method attribute.

有时,在Action里,开发者喜欢创建多于一个入口,例如,在data-access Action案例中,一个developer可能象区分Create,retrieve,update和delete等各种入口点。一个不同的入口点可以被教书在method属性中。

<action name="delete" class="example.CrudAction" method="delete">

 If there is no execute method, and no other method, specified in the configuration, the framework will throw an exception.


Wildcard Method

Many times, a set of action mappings will share a common pattern. For example, all your edit actions might start with the word "edit", and call the edit method on the Action class. The delete actions might use the same pattern, but call the delete method instead.

很多时候,一系列的action因设将给一个通用模式共用,例如,你的所有edit action可能用“edit”开头,调用Action里的ecit方法。Delete Action可能用同样的方式,调用所有delete方法。

Rather than code a separate mapping for each action class that uses this pattern, you can write it once as a wildcard mapping.

不用为每个action以同样的模式写映射,你可以以疲惫符的方式只写一遍。


<action name="*Crud" class="example.Crud" method="{1}">

Here, a reference to "editCrud" will call the edit method on an instance of the Crud Action class. Likewise, a reference to "deleteCrud" will call the delete method instead.

Another common approach is to postfix the method name and set it off with an exclamation point (aka "bang"), underscore, or other special character.

另一个通用的方式是使用前缀

  • "action=Crud_input"
  • "action=Crud_delete"

To use a postfix wildcard, just move the asterisk and add an underscore.

<action name="Crud_*" class="example.Crud" method="{1}">

From the framework's perspective, a wildcard mapping creates a new "virtual" mapping with all the same attributes as a conventional, static mapping. As a result, you can use the expanded wildcard name as the name of validation, type conversion, and localization files, just as if it were an Action name (which it is!).

  • Crud_input-validation.xml
  • Crud_delete-conversion.xml

 The postfix "!" notation is also available in WebWork 2, but it is implemented differently. To use the old implementation, set struts.enable.DynamicMethodInvocation=TRUE in the struts.properties file. To use wildcards instead (preferred), set struts.enable.DynamicMethodInvocation=FALSE.

ActionSupport Default

If the class attribute in an action mapping is left blank, the com.opensymphony.xwork.ActionSupport class is used as a default.

如果不写class属性,com.opensymphony.xwork.actionSupport类默认被使用。

<action name="Hello">
   // ...
</action>

 The ActionSupport class has execute and input methods that return "success".

ActionSupport类有execute和input方法,并且都返回"success".

 To specify a different class as the default Action, set the  package attribute.

设置package的attribute,可以指定其他类为默认Action

 For more about using wildcards, see Wildcard Mappings.

Post-Back Default

A good practice is to link to actions rather than pages. Linking to actions encapsulates which server page renders, and ensures that an Action class can fire before a page renders.

Another common workflow stategy is to first render a page using an alternate method, like input and then have it submit back to the default execute method.

Using these two strategies together creates an opportunity to use a "post-back" form that doesn't specify an action. The form simply submits back to the action that created it.

一个最佳实践是链接一个action而不是page,链接到一个被server page渲染器包装的action,确保页面渲染之前,action类能被渲染。
另一个通常的工作流策略是用一个变通的方法渲染页面,例如input,然后使它提交返回到默认的execute方法。
把这两中策略结合起来,创建一个条件来使用没有action的“Post-back” Form,那个Form简单地返回创建它的他个action。

 


Posting Back
<s:form>
    <s:textfield label="Please enter your name" name="name"/>
    <s:submit/>
</s:form>

Action Default

Usually, if an action is requested, and the framework can't map the request to an action name, the result will be the usual "404 - Page not found" error. But, if you would prefer that an ominbus action handle any unmatched requests, you can specify a default action. If no other action matches, the default action is used instead.

通常,如果一个action被请求,而fromework不能映射那个请求到一个action名字,结果通常使一个“404-Page not found ”错误。但是,如果你更热衷于一个能处理任何不匹配请求的万能action,你能描述一个默认的action。如果没有其他action匹配,默认的action被使用。

<package name="Hello" extends="action-default">

<default-action-ref name="UnderConstruction">

<action name="UnderConstruction">
  <result>/UnderConstruction.jsp</result>
</action>

There are no special requirements for the default action. Each package can have its own default action, but there should only be one default action per namespace.

One to a Namespace

The default action features should be setup so that there is only one default action per namespace. If you have multiple packages declaring a default action with the same namespace, there is no guarantee which action will be the default.

Wildcard Default

Using wildcards is another approach to default actions. A wildcard action at the end of the configuration can be used to catch unmatched references.

<action name="*" >
  <result>/{1}.jsp</result>
</action>

When a new action is needed, just add a stub page.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值