上一篇文章中我写了一个LoginAction类,其中包含了一个默认的execute() 页面提交处理方法。如何做到一个Action中包含多个处理方法呢,而不是默认的去执行execute方法。
在struts2中 可以通过三种方式来轻松实现这种动态方法调用。
1 可以在配置文件中struts.xml中配置Action元素的时候用method属性来指定执行哪个方法。
2 jsp页面提交的时候用感叹号方式表达式。格式为Action名 + !+ 方法名。
3 在配置文件中使用通配符方式。
我写的运行例子如下: (基于上一篇文章中搭建的struts2集成环境)
首先是LoginAction
//这是一个很普通的java类
public class LoginAction{
//封装的用户请求的两个参数-页面提交后属性被赋值
//属性和页面控件属性保持一致。
private String username="nihao";
private String password="123";
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
//专门处理用户请求的execute()方法 默认调用
public String execute() throws Exception{
System.out.println("执行了 execute()方法");
if(username.equals("weiguo") && password.equals("123")){
return "success";
}else{
return "error";
}
}
//专门处理用户请求的方法1 strut.xml中 method属性来指定 调用
public String addMetd() throws Exception{
System.out.println("执行了 addMetd() 方法");
if(username.equals("weiguo") && password.equals("123")){
return "success";
}else{
return "error";
}
}
//专门处理用户请求的方法2 感叹号方式表达式 会调用
public String addMetd2() throws Exception{
System.out.println("执行了 addMetd2() 方法");
if(username.equals("weiguo") && password.equals("123")){
return "success";
}else{
return "error";
}
}
//专门处理用户请求的方法3 统配符方式调用
public String threeMetd3() throws Exception{
System.out.println("执行了 threeMetd3() 方法");
return "success";
}
}
然后是修改struts.xml配置文件
<struts>
<!-- include节点是struts2中组件化的方式 可以将每个功能模块独立到一个xml配置文件中 然后用include节点引用 -->
<include file="struts-default.xml"></include>
<!-- package 提供了将多个 Action 组织为一个业务模块的方式, package相当一个业务模块的抽象 名字必须是唯一的 -->
<!--namespace:定义package命名空间 该命名空间影响到url的地址,例如此命名空间为/test那么访问是的地址为http://localhost:8080/struts2/test/XX.action -->
<package name="main" extends="struts-default">
<!-- Action 必须是在 包空间下定义 -->
<!-- class 中指定要执行的action类 从WEB-INF/classes的文件夹做为根,开始找 -->
<action name="login" class="com.mycom.LoginAction" >
<!-- jsp文件在工程中的位置 WebRoot\jsp\login.jsp -->
<result name="success">/jsp/welcome.jsp</result>
<result name="error">/jsp/error.jsp</result>
</action>
<!-- 第一种实现方式的配置内容 -->
<action name="method1" class="com.mycom.LoginAction" method="addMetd" >
<result name="success">/jsp/welcome.jsp</result>
<result name="error">/jsp/error.jsp</result>
</action>
<!-- 第二种实现方式的配置内容 不需要设置 method, 感叹号表达式中指定 -->
<action name="signAction" class="com.mycom.LoginAction" >
<result name="success">/jsp/welcome.jsp</result>
<result name="error">/jsp/error.jsp</result>
</action>
<!-- 第三种实现方式的配置内容,用到了通配符和占位符 -->
<action name="Action*" class="com.mycom.LoginAction" method="{1}" >
<result name="success">/jsp/{1}page.jsp</result>
</action>
</package>
</struts>
登录页面 login.jsp文件中修改为:
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>登陆页面</title>
</head>
<body>
<form action="login" method="post" >
<table align="center">
<tr><td>用户名: <input type="text" name="username"></td></tr>
<tr><td>密码: <input type="text" name="password"></td></tr>
<tr align="center">
<td><input type="submit" value="sub提交">
<input type="button" value="method提交" οnclick="subMethod();" >
<input type="button" value="!方式提交" οnclick="twoMethod();" >
<input type="button" value="*方式提交" οnclick="threeMethod();" >
</td>
</tr>
</table>
</form>
</body>
<script>
// struts.xml中映射的Action名 method1 调用xml中定义的 method 为 addMeth
function subMethod(){
document.forms[0].action="method1";
document.forms[0].submit();
}
//struts.xml中映射的Action名 + !+ 方法名
//struts.xml中映射的Action名 signAction 调用 method 为 addMetd2
function twoMethod(){
document.forms[0].action="signAction!addMetd2";
document.forms[0].submit();
}
//struts.xml中映射的Action名 方法名 用* {1}的形式来表达
function threeMethod(){
document.forms[0].action="ActionthreeMetd3";
document.forms[0].submit();
}
</script>
</html>
为了说明第三种方式,我建立了一个jsp文件-threeMetd3page.jsp
如图:
threeMetd3page.jsp 内容如下:
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>转向页面</title>
</head>
<body>
这个你要的threeMetd3page.jsp 页面
</body>
</html>
通过以上的代码,可以很容易演示出3中动态方法调用的效果,真正项目开发中第一种,第三种方式用的多些。第一种可读性好,第三种通用型好,可以省去很多配置代码。