在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这个属性所要执行的方法,代码如下:
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,然后给这个页面添加三个超链接,代码如下:
<%@ 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页面:
<%@ 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页面:
<%@ 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页面:
<%@ 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文件,修改后代码如下:
<?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,代码如下:
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页面,代码如下:
<%@ 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页面:
<%@ 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页面:
<%@ 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文件中加入下列这行:
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
该属性设置Struts 2是否支持动态方法调用,如果没有这一行要加上去,要不然会报错误,如果你加上这一行,但是你把value值改成false,也会报错!
struts.xml文件:
<?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.接下来来总结一下:
(1).Action执行的时候并不一定要执行execute方法。
(2).可以在配置文件中配置Action的时候用method=来指定执行哪个方法,也可以在url地址中动态指定(动态方法调用DMI)(推荐),而前者会产生太多的action,所以不推荐使用。
以上内容仅供大家参考学习,我也是借鉴老师的知识所学的,写得不好,请见谅,如有错误,请指出,谢谢!