当用到多个提交按钮时,需要多个Action处理或者一个Action中的不同方法时,通过以下三种方式来进行。
1、动态方法调用:
案例:多个按钮提交
login3.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix = "s" uri = "/struts-tags" %>
<%
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 'login3.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">
-->
<script type="text/javascript">
function login() {
var form = document.getElementsByTagName("form")[0];
form.action = "LoginAndRegister!login";
form.submit();
}
function register() {
var form = document.getElementsByTagName("form")[0];
form.action = "LoginAndRegister!register";
form.submit();
}
</script>
</head>
<body>
<s:form action = "" method = "post">
<s:textfield name = "username" label = "用户名"></s:textfield><br><br>
<s:password name = "password" label = "密码"></s:password><br><br>
<s:submit value = "登录" οnclick="login()"></s:submit>
<s:submit value = "注册" οnclick="register()"></s:submit>
</s:form>
</body>
</html>
Action页面
package com.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAndRegisterAction extends ActionSupport{
private String username;
private String password;
private String tip;//保持输出结果
public String login() {
if(username.equals("admin") && password.equals("admin")){
this.setTip("欢迎"+username+"登录");
return SUCCESS;
}
return LOGIN;
}
public String register() {
this.setTip("欢迎"+username+"注册");
return SUCCESS;
}
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;
}
public String getTip() {
return tip;
}
public void setTip(String tip) {
this.tip = tip;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix = "s" uri = "/struts-tags" %>
<%
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 'ok3.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>
<s:property value = "tip"/>
</body>
</html>
struts.xml配置:
<action name="LoginAndRegister" class = "com.action.LoginAndRegisterAction">
<result name = "success">/ok3.jsp</result>
<result name = "login">/login3.jsp</result>
</action>
2、为Action元素指定method属性
修改login3.jsp页面中的js代码:
<script type="text/javascript">
function login() {
var form = document.getElementsByTagName("form")[0];
form.action = "login3";
form.submit();
}
function register() {
var form = document.getElementsByTagName("form")[0];
form.action = "register3";
form.submit();
}
</script>
<action name="login3" class = "com.action.LoginAndRegisterAction" method = "login">
<result name = "success">/ok3.jsp</result>
<result name = "login">/login3.jsp</result>
</action>
<action name="register3" class = "com.action.LoginAndRegisterAction" method = "register">
<result name = "success">/ok3.jsp</result>
</action>
3、通配符映射:
修改login3.jsp页面js代码:
<script type="text/javascript">
function login() {
var form = document.getElementsByTagName("form")[0];
form.action = "Action_login";
form.submit();
}
function register() {
var form = document.getElementsByTagName("form")[0];
form.action = "Action_register";
form.submit();
}
修改struts.xml配置:
<action name="Action_*" class = "com.action.LoginAndRegisterAction" method = "{1}">
<result name = "success">/ok3.jsp</result>
<result name = "login">/login3.jsp</result>
</action>