有关Struts2_ActionMethod_DMI_动态方法调用的学习

动态的方法调用:DMI
在form的action中使用动态的方法调用。action="xxx!xxx.action",第一个xxx是action中的调用的方法,和execute一样的。后面一个是所请求的action,两个可以不相同
如hello!login.action  这样子可以减少很多action的配置

另外一种方式是在struts.xml文件中加入method的方式调用,但是需要很多的action配置

另外一种方式是使用通配符的方式,在action的配置文件中使用*xxxx或者xxxx*,然后在method中写method="{1}",在form的action中写xxxxxx,则action类中的方法名对应于xxxxxx中的名字。如helloLogin
则hello对应到action类中的方法,login则是请求的action
Jsp代码  
  1. <?xml version="1.0" encoding="GB18030" ?>   
  2. <%@ page language="java" contentType="text/html; charset=GB18030"  
  3.     pageEncoding="GB18030"%>   
  4.   
  5. <% String context = request.getContextPath(); %>   
  6.   
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
  8. <html xmlns="http://www.w3.org/1999/xhtml">   
  9. <head>   
  10. <meta http-equiv="Content-Type" content="text/html; charset=GB18030" />   
  11. <title>Insert title here</title>   
  12. </head>   
  13. <body>   
  14. Action执行的时候并不一定要执行execute方法<br />   
  15. 可以在配置文件中配置Action的时候用method=来指定执行哪个方法   
  16. 也可以在url地址中动态指定(动态方法调用DMI)(推荐)<br />   
  17.     <a href="<%=context %>/user/userAdd">添加用户</a>   
  18.     <br />   
  19.     <a href="<%=context %>/user/user!add">添加用户</a>   
  20.     <br />   
  21. 前者会产生太多的action,所以不推荐使用   
  22.        
  23. </body>   
  24. </html>  


Struts.xml代码  
  1. <?xml version="1.0" encoding="UTF-8" ?>   
  2. <!DOCTYPE struts PUBLIC   
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">   
  5.   
  6. <struts>   
  7.     <constant name="struts.devMode" value="true" />   
  8.     <package name="user" extends="struts-default" namespace="/user">   
  9.         <action name="userAdd" class="com.hugui.struts2.user.action.UserAction" method="add">   
  10.             <result>/user_add_success.jsp</result>   
  11.         </action>   
  12.            
  13.         <action name="user" class="com.hugui.struts2.user.action.UserAction">   
  14.             <result>/user_add_success.jsp</result>   
  15.         </action>   
  16.     </package>   
  17. </struts>  


Java代码  
  1. package com.hugui.struts2.user.action;   
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;   
  4.   
  5. public class UserAction extends ActionSupport {   
  6.     public String add() {   
  7.         return SUCCESS;   
  8.     }   
  9.        
  10.        
  11. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值