struts2中的动态方法调用DMI



通常,在struts2中,如果想执行特定的方法,往往会在struts.xml中,配置action的method属性值为要执行的方法名,默认为execute方法。为了程序的扩展,这种方法不推荐,而是使用DMI方式,举例如下(场景为用户的增、删、改):

(1)UserAction

  1. package com.struts2.study.yy;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. public class UserAction extends ActionSupport {  
  6.       
  7.     private static final long serialVersionUID = 1L;  
  8.   
  9.     public String add(){  
  10.         System.out.println("-----add-----");  
  11.         return SUCCESS;  
  12.     }  
  13.       
  14.     public String delete(){  
  15.         System.out.println("-----delete-----");  
  16.         return SUCCESS;  
  17.     }  
  18.       
  19.     public String modify(){  
  20.         System.out.println("-----modify-----");  
  21.         return SUCCESS;  
  22.     }  
  23. }  
package com.struts2.study.yy;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	
	private static final long serialVersionUID = 1L;

	public String add(){
		System.out.println("-----add-----");
		return SUCCESS;
	}
	
	public String delete(){
		System.out.println("-----delete-----");
		return SUCCESS;
	}
	
	public String modify(){
		System.out.println("-----modify-----");
		return SUCCESS;
	}
}

(2)struts.xml

  1. <package name="user"  extends="struts-default" namespace="/">  
  2.     <action name="user" class="com.struts2.study.yy.UserAction">  
  3.             <result name="success">/user/success.jsp</result>  
  4.     </action>  
  5.    </package>  
 <package name="user"  extends="struts-default" namespace="/">
    	<action name="user" class="com.struts2.study.yy.UserAction">
    			<result name="success">/user/success.jsp</result>
    	</action>
    </package>
(3)程序入口main.jsp
  1. <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'mainp.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>   
  26.     <a href="<%=path %>/user!add">add user</a><br>  
  27.     <a href="<%=path %>/user!delete">delete user</a><br>  
  28.     <a href="<%=path %>/user!modify">modify user</a>  
  29. </html>  
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'mainp.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body> 
    <a href="<%=path %>/user!add">add user</a><br>
    <a href="<%=path %>/user!delete">delete user</a><br>
    <a href="<%=path %>/user!modify">modify user</a>
</html>

(4)跳转页面success.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'success.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.     success!  
  27.   </body>  
  28. </html>  
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'success.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    success!
  </body>
</html>

(5)结果

当分别点击三个链接时,后台会打出如下日志:

  1. -----add-----  
  2. -----delete-----  
  3. -----modify-----  
-----add-----
-----delete-----
-----modify-----

  通过DMI方式,可以简化配置信息。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值