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

Struts2中Action的动态调用方法

2017-10-28 19:47 405 查看
在Struts2中,Action执行的时候并不一定要执行execute,我们可以指定Action执行哪个方法,下面分别介绍三种方法来指定Action执行哪个方法:

1.第一种方法,通过Action里的method属性指定执行方法,我们可以在struts.xml配置文件中配置Action的时候用method="   " 来指定执行的哪个方法。
(1).接下来附上一个例子,通过第一种方法来指定执行方法,首先,复制一个已经搭建好struts2环境的web项目,这里我们复制ActionTest这个项目,然后粘贴,出现下图:
修改项目名称,点击OK:



然后在选中ActionMethod这个项目,右键选择Properties,根据下图箭头提示信息修改即可:





把上图的Web Context-root修改成/ActionMethod即可,点击OK按钮,再点击弹出来的框的finish按钮即可,然后删除我们不需要的页面等。

(2).首先新建一个UserAction.java类,放在com.gk.user包下,继承于ActionSupport类,里面有3个方法,一个login()方法,一个add()方法,一个delete()方法,用来指定Action中method这个属性所要执行的方法,代码如下:

[java] view plain copy

package com.gk.user;  
  
import com.opensymphony.xwork2.ActionSupport;  
  
public class UserAction extends ActionSupport{  
      
    public String login(){  
        return "success";  
    }  
      
      
    public String add(){  
        return "success";  
    }  
      
      
    public String delete(){  
        return "success";  
    }  
      
      
}  

(3).接着打开我们所需要的index.jsp页面,改编码为utf-8,然后给这个页面添加三个超链接,代码如下:

[html] view plain copy

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
<%  
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 'index.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="userlogin">用户登录</a>  
    <a href="useradd">添加用户</a>  
    <a href="userdelete">删除用户</a>  
  </body>  
</html>  

其中超链接标记<a>里面的href属性值是action名字,即在struts.xml文件里action的name属性。

(4).接下来新建三个jsp页面,改编码为utf-8,当点击用户登录的时候,通过action来转发到User_login.jsp页面,当点击添加用户时,通过action转发到User_add.jsp页面,当点击删除用户时,通过action转发到User_delete.jsp页面,代码分别如下:
User_login.jsp页面:

[html] view plain copy

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
<%  
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 'login.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>  
   <center>  
   <form action="/xxx" method="post">  
        用户名:<input type="text" name="username"/><br>  
        密码:<input type="password" name="password"/><br>  
        <input type="submit" value="提交"/>  
    </form>  
    </center>  
  </body>  
</html>  

User_add.jsp页面:

[html] view plain copy

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
<%  
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 'user_add.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>  
        添加用户成功  
  </body>  
</html>  

User_delete.jsp页面:

[html] view plain copy

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
<%  
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 'user_add.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>  
        删除用户成功  
  </body>  
</html>  

(5).接下来我们就可以配置struts.xml文件,打开struts.xml文件,修改后代码如下:

[html] view plain copy

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC  
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
    "http://struts.apache.org/dtds/struts-2.0.dtd">  
          
<struts>  
  
    <package name="user" namespace="/" extends="struts-default">  
        <action name="userlogin" class="com.gk.user.UserAction" method="login">  
            <result name="success">/User_login.jsp</result>  
        </action>  
          
        <action name="useradd" class="com.gk.user.UserAction" method="add">  
            <result name="success">/User_add.jsp</result>  
        </action>  
          
        <action name="userdelete" class="com.gk.user.UserAction" method="delete">  
            <result name="success">/User_delete.jsp</result>  
        </action>  
    </package>  
</struts>  

其中第一个action标记里面的名称name为userlogin,class类为包下的UserAction类,method方法为login,只要UserAction下有个方法login并返回类型为String就可以转发到result子标记下的指定的视图,如果没有method属性,默认是执行execute()方法,后面2个action类似。

(6).接下来部署此应用到Tomcat服务器上,开启Tomcat服务器,在地址栏输入地址:http://localhost:8083/ActionMethod/index.jsp ,页面如下:



点击用户登录的超链接时,出现下图界面,地址栏后面变为struts.xml文件里的action标记的name属性,也为超链接<a>标记的href属性的值,其实是通过超链接跳转到一个action下的视图:



点击添加用户和删除用户时,如下所示:





2.第二种方法,动态方法调用DMI,我们可以在url地址中动态指定action执行哪个方法,可以通过action+!+方法名。(注:只要Action对象中有这个方法,并且返回类型为String就可以调用,这样struts.xml文件配置文件就可以不需要配置method属性):
(1)接下来以上面那个项目为例,修改一下其中的代码即可,在UserAction.java类中添加多两个方法,分别为update()方法和find()方法,返回类型都为String,代码如下:

[java] view plain copy

package com.gk.user;  
  
import com.opensymphony.xwork2.ActionSupport;  
  
public class UserAction extends ActionSupport{  
      
    public String login(){  
        return "success";  
    }  
      
      
    public String add(){  
        return "success";  
    }  
      
      
    public String delete(){  
        return "success";  
    }  
      
    public String update(){  
        return "update";  
    }  
      
    public String find(){  
        return "find";  
    }  
      
      
}  

(2).接下来修改index.jsp页面,代码如下:

[html] view plain copy

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
<%  
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 'index.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="userlogin">用户登录</a>  
    <a href="useradd">添加用户</a>  
    <a href="userdelete">删除用户</a>  
    <a href="userupdate!update">修改用户</a>  
    <a href="userfind!find">查询用户</a>  
  </body>  
</html>  

其中的修改用户链接和查询用户链接中的href属性的值为action+!+method,如果我们的action的name属性为userupdate的话,方法为update的话,可以写成userupdate!update,然后我们可以不用再struts.xml文件配置method属性。

(3).接下来新建2个jsp页面,编码改成utf-8,当用户点击修改用户超链接时,跳转到User_update.jsp页面,当用户点击查询用户链接时,跳转到User_find.jsp页面,代码分别如下:
User_update.jsp页面:

[html] view plain copy

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
<%  
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 'User_update.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>  
        修改用户成功  
  </body>  
</html>  

User_find.jsp页面:

[html] view plain copy

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
<%  
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 'User_update.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>  
        查询用户成功  
  </body>  
</html>  

(4).接下来打开struts.xml文件,进行修改,其中必须要在struts.xml文件中加入下列这行:

[html] view plain copy

<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>  

 该属性设置Struts 2是否支持动态方法调用,如果没有这一行要加上去,要不然会报错误,如果你加上这一行,但是你把value值改成false,也会报错!
struts.xml文件:

[html] view plain copy

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC  
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
    "http://struts.apache.org/dtds/struts-2.0.dtd">  
  
<struts>  
  
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>  
    <package name="user" namespace="/" extends="struts-default">  
        <action name="userlogin" class="com.gk.user.UserAction" method="login">  
            <result name="success">/User_login.jsp</result>  
        </action>  
  
        <action name="useradd" class="com.gk.user.UserAction" method="add">  
            <result name="success">/User_add.jsp</result>  
        </action>  
  
        <action name="userdelete" class="com.gk.user.UserAction"  
            method="delete">  
            <result name="success">/User_delete.jsp</result>  
        </action>  
  
        <action name="userupdate" class="com.gk.user.UserAction">  
            <result name="update">/User_update.jsp</result>  
        </action>  
        <action name="userfind" class="com.gk.user.UserAction">  
            <result name="find">/User_find.jsp</result>  
        </action>  
    </package>  
</struts>  

这里介绍最后面两个action,name为action名称,class为包下的 UserAction类,不用指定method方法,在index.jsp下定义的后两个超链接中,通过action名称加上感叹号,再加上UserAction下定义的方法名,就可以通过action请求跳转到相应的页面。

(5).接着重新部署下,开启Tomcat服务器,运行效果如下:



点击修改用户超链接和查询用户超链接,如下图所示:



跳转时地址栏后面变成了上图的红色文本。



3.第三种:使用通配符实现动态调用。

struts.xml在配置action指定name的时候,可以使用模式字符串,就是用一个
*
号来代替一个或者多个字符,接下来,就可以在class,method,以及result子元素中使用{N}(N表示地N个*匹配的字串)来代表子串。 

例如:

1.使用通配符动态设置method
<action name="*Action" class="com.tuxianchao.LoginAction" method="{1}"></action>
表示当请求为loginAction的时候,method为login,调用LoginAction中的login方法处理请求;
当请求为registAction的时候,method为regist,调用LoginAction中的regist方法处理请求。
1
2
3

2.使用通配符来指定设置class
<action name="*Action" class="com.tuxianchao.{1}Action"></action>
表示当请求为loginAction的时候,,调用LoginAction中的execute方法处理请求;
当请求为registAction的时候,调用registAction中的execute方法处理请求。
1
2
3

3.使用通配符设置result
<action name="*">
<result>/{1}.jsp</result>
</action>
这是一个通用的action,表示对于任何请求,都会返回对应请求字符串的JSP页面
1
2
3
4

多个*的应用: 

1.在任何情况下,优先对应完全和action的name相同的 

对于两个通配符的情况,如果两个action都匹配,那么机会按照配置的顺序来匹配,对于任何带*的来说,不存在优先级的问题,都是同级的。


实现动态调用

对于上面的例子,可修改配置文件为:
<action name="*Action" class="com.tuxianchao.LoginAction"
method="{1}">
<result name="success">/jsp/welcome.jsp</result>
<result name="error">/jsp/error.jsp</result>
<result name="regist">/jsp/regist.jsp</result>
</action>
1
2
3
4
5
6

修改对应的action就可以了。添加一个login方法。

4.总结:
(1).Action执行的时候并不一定要执行execute方法。
(2).可以在配置文件中配置Action的时候用method=来指定执行哪个方法,也可以在url地址中动态指定(动态方法调用DMI)(推荐),而前者会产生太多的action,所以不推荐使用。

其实动态调用就是通过各种方式决定调用action中哪一个方法来处理请求,或者说调用哪一个action来处理请求。

参考:
http://blog.csdn.net/u012561176/article/details/44347429

http://blog.csdn.net/qq_25201665/article/details/54663525
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: