您的位置:首页 > 编程语言 > Java开发

struts2中的动态方法调用DMI

2016-01-26 23:26 615 查看

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

(1)UserAction

[java]
view plaincopyprint?





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;  
    }  
}  

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

[html]
view plaincopyprint?





<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>  

<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

[html]
view plaincopyprint?





<%@ 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>  

<%@ 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

[html]
view plaincopyprint?





<%@ 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>  

<%@ 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)结果

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

[html]
view plaincopyprint?





-----add-----  
-----delete-----  
-----modify-----  

-----add-----
-----delete-----
-----modify-----


  通过DMI方式,可以简化配置信息。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: