terryjs 发表于 2013-2-6 09:09:20

struts2 配置 Action(一)

一。动态方法调用
    Struts1 提供了 DispatchAction , 从而允许一个Action内包含多个处理逻辑。
    Struts2 也可以用 DMI(Dynamic Method Invocation)调用来处理这种请求。
  
 
   login!regist.action  
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>登录页面</title><script type="text/javascript">function regist(){//获取该页面中的第一个表单元素targetForm = document.forms;//动态修改目标表单的action属性targetForm.action = "login!regist.action";//提交表单targetForm.submit();}</script></head><body><form action="login.action" method="post"><table width="300" align="center"><tr><td>用户名:</td><td><input type="text" name="username"/></td></tr><tr><td>密  码:</td><td><input type="text" name="password"/></td></tr><tr><td><input type="submit" value="登录"/></td><td><input type="button" value="注册" /></td></tr><table></form></body></html> import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ActionContext;public class LoginRegistAction extends ActionSupport{//封装用户请求参数的两个属性private String username;private String password;//封装处理结果的tip属性private String tip;//username属性对应的setter和getter方法public String getUsername(){return username;}public void setUsername(String username){this.username = username;}//password属性对应的getter和setter方法public String getPassword(){return password;}public void setPassword(String password){this.password = password;}//tip属性对应的setter和getter方法public String getTip(){return tip;}public void setTip(String tip){this.tip = tip;}//Action包含的注册逻辑public String regist() throws Exception{ActionContext.getContext().getSession().put("user" , getUsername());setTip("恭喜您," + getUsername() + ",您已经注册成功!");return SUCCESS;}//Action包含的默认处理逻辑public String execute() throws Exception{if (getUsername().equals("crazyit")&& getPassword().equals("leegang") ){ActionContext.getContext().getSession().put("user",getUsername());setTip("欢迎," + getUsername() + ",您已经登录成功!");return SUCCESS;}else{return ERROR;}}} 上面2段代码所示:单击 “登录” 按钮时,系统将提交给对应Action的默认处理方法。当点击 “注册” 按钮时,系统将提交给 loginAction 的 regist 方法处理。
注意 :使用动态方法前 必须设置 Struts2 允许动态方法调用。 将 struts.enable.DynamicMethodInvocation 常量设置为 true 时,开启,否则就 关闭。
 
 
 
 
二。为 action 元素指定 method属性

   还是上面 一。中的2个 button 的问题。struts2 还提供了 method="" 的解决方案。
<?xml version="1.0" encoding="GBK"?><!-- 下面指定了Struts 2配置文件的DTD信息 --><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd"><!-- struts是配置文件的根元素 --><struts><!-- 配置一个package元素 --><package name="lee" extends="struts-default"><!-- 配置名为login的Action,该Action采用默认的execute方法处理用户请求 --><action name="login" class="lee.LoginRegistAction"><!-- 下面定义了三个Result映射 --><result name="input">/login.jsp</result><result name="error">/error.jsp</result><result name="success">/welcome.jsp</result></action><!-- 配置名为Regist的Action,该Action采用默认的regist方法处理用户请求 --><action name="regist" class="lee.LoginRegistAction" method="regist"><!-- 下面定义了三个Result映射 --><result name="input">/login.jsp</result><result name="error">/error.jsp</result><result name="success">/welcome.jsp</result></action><action name=""><result>.</result></action></package></struts> js改成:
function regist(){//获取该页面中的第一个表单元素targetForm = document.forms;//动态修改目标表单的action属性targetForm.action = "regist.action";//提交表单targetForm.submit();} 上面定义了 login 和 regist 两个逻辑 Action 它们都对应 lee.LoginRegistAction。其中 login 的 Action 对应处理的是默认的 execute() 方法,而 regist 的 Action 对应的是 regist()方法。然后如上所示 修改 JSP 中 javascript 代码。
 通过这种方法,就能完成 一。中一样的效果
 
三。使用通配符
 上述 二。中冗余代码太多,可以使用通配符
<?xml version="1.0" encoding="GBK"?><!-- 指定Struts 2配置文件的DTD信息 --><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd"><!-- Struts 2配置文件的根元素 --><struts><!-- 配置一个package元素 --><package name="lee" extends="struts-default"><!-- 使用通配符配置了Action名,method属性是个动态值--><action name="*Action" class="lee.LoginRegistAction" method="{1}"><!-- 定义了三个Result --><result name="input">/login.jsp</result><result name="error">/error.jsp</result><result name="success">/welcome.jsp</result> </action><action name=""><result name="success">.</result></action></package></struts>      上述 *Action 定义了一系列的逻辑 Action ------- 只要用户请求的 URL 是 *Action.action 的模式,都可以通过该 Action类处理。
      method属性使用了 表达式 {1},该表达式的值就是 name 属性值中第一个*的值。例如: URL 为 loginAction.action ,则调用 lee.LoginRegistAction类的 regist()方法。
 
LoginRegistAction 类代码:
import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ActionContext;public class LoginRegistAction extends ActionSupport{//封装用户请求参数的两个属性private String username;private String password;//封装处理结果的属性private String tip;//username属性的setter和getter方法public String getUsername(){return username;}public void setUsername(String username){this.username = username;}//password属性的setter和getter方法public String getPassword(){return password;}public void setPassword(String password){this.password = password;}//tip属性的setter和getter方法public String getTip(){return tip;}public void setTip(String tip){this.tip = tip;}//处理用户注册的注册逻辑public String regist() throws Exception{ActionContext.getContext().getSession().put("user" , getUsername());setTip("恭喜您," + getUsername() + ",您已经注册成功!");return SUCCESS;}//处理用户登录的登录逻辑public String login() throws Exception{if (getUsername().equals("crazyit")&& getPassword().equals("leegang") ){ActionContext.getContext().getSession().put("user",getUsername());setTip("欢迎," + getUsername() + ",您已经登录成功!");return SUCCESS;}else{return ERROR;}}} js代码:
 
function regist(){//获取该页面中的第一个表单元素targetForm = document.forms;//动态修改目标表单的action属性targetForm.action = "registAction.action";//提交表单targetForm.submit();} 
 
如果有需要, Struts2 允许 在 class 属性和 method 属性中同时使用表达式。
如下:
<action name="*_*" method="{2}" class="actions.{1}"> 如果有 URL 为 Book_save.action请求,因为匹配 *_* 模式,且第一个 * 的值为 Book,第二个 * 的值为 save,则意味着调用 Book 处理类的 save()方法来处理用户请求。

 
注意:如果有 URL 为 abcAction.action 的请求,如果 struts.xml 文件中有名为 abcAction 的 Action,则一定由该 Action来处理用户请求;如果 struts.xml 中没有 abcAction 的 Action,则搜索 name 属性值匹配 abcAction 的 Action,例如: name 为 *Action 或 *,但是 *Action 并不会比 * 更优先匹配 abcAction 请求,而是先找到哪个 Action,就先由那个Acion来处理用户请求。因此,一般我们应该将名为 * 的Action匹配在最后,否则 Struts2 将使用该Action处理所有希望使用模式匹配的请求。
代码如下:
<?xml version="1.0" encoding="GBK"?><!-- 指定Struts 2配置文件的DTD信息 --><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd"><struts><package name="lee" extends="struts-default"><!-- 配置name为*Action的第二个Action --><action name="*Action" class="lee.TwoAction"><result name="success">/welcome.jsp</result></action><!-- 配置name为loginAction的第三个Action --><action name="loginAction" class="lee.LoginAction"><result name="input">/login.jsp</result><result name="error">/error.jsp</result><result name="success">/welcome.jsp</result></action><!-- 配置name为*的第一个Action --><action name="*" class="lee.FirstAction"><result name="success">/welcome.jsp</result></action></package></struts> 四。配置 Action 的默认处理类
Struts2 默认 ActionSupport 作为 Action 处理类,<action>中没有 class 属性 就默认使用 ActionSupport。


 struts-default.xml 中有:
<package name="struts-default" abstract="true">    <default-class-ref class="com.opensymphony.xwork2.ActionSupport" /></package > Struts2 允许定义自己的默认 Action,只需在 struts.xml 中配置:
 
<default-class-refname="simpleViewResultAction" /><action name="simpleViewResultAction" class="lee.SimpleViewResultAction">   <result name="success">/welcome.jsp</result></action> <default-class-ref >中的 name (="simpleViewResultAction")指向容器中另一个有效的 Action (<action name="simpleViewResultAction"
>)
 
 
 
 
 
页: [1]
查看完整版本: struts2 配置 Action(一)