Action配置(动态方法调用、为Action元素指定method属性、通配符映射)

当用到多个提交按钮时,需要多个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;
}
  
  
}


ok3.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 '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>


修改struts.xml配置:

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




  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柏油

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值