一个Action处理多个业务逻辑 || 动态改变form的Action

本例实现:
    注:本例可通过struts.xml文件中指定action的method即可代替。即:实现一个Action类多个业务处理逻辑。
    struts的一个Action处理多个业务逻辑问题,设计到form的动态提交
1,struts.xml配置如下:
<struts> 
   [color=red] <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>该常量一定要设置为true[/color]
    <!-- 配置的package的name不能和主文件struts中的package名称相同 -->
	<package name="user1" extends="struts-default">
		<action name="login2" class="com.dd.action.LoginAction">
		    <!-- 定义三个逻辑视图和物理资源之间的映射 -->
		    <result name="input">/login.jsp</result>
		    <result name="success">/welcome.jsp</result>
		    <result name="error">/error.jsp</result>
		</action>
		<action name="loginRegist" class="com.dd.action.LoginRegistAction">
		    <result name="input">/loginRegist.jsp</result>
		    <result name="success">/loginRegist.jsp</result>
		</action>
	</package>
</struts>    


2,Action的实现
package com.dd.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginRegistAction extends ActionSupport {
	private String username;
	private String password;
	private String tip;
	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;
	}
	public String regist(){
		System.out.println("注册成功");
		setTip("恭喜,你已经注册成功");
		return SUCCESS;
	}
	@Override
	public String execute() throws Exception {
		System.out.println("登录成功");
		if(getUsername().equals("admin") && getPassword().equals("admin")){
			setTip("恭喜,登录成功");
			return SUCCESS;
		}else{
			setTip("悲哀,登录失败");
			return INPUT;
		}
	}
}

3,用户入库界面代码:
<%@ 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 '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">
	<script type="text/javascript">
	  function loginregist(){
	   myform = document.forms[0];
	   myform.action = "loginRegist!regist";
	   myform.submit();
	  }
	</script>
  </head>
  <body><%--struts标签key对应的是国际化资源文件中的key --%>
    <s:form action="loginRegist">
       <s:textfield name="username" key="user"></s:textfield>
       <s:textfield name="password" key="pass"></s:textfield>
       <input type="button" onclick="loginregist();" value="注册"/>
       <s:submit key="login"></s:submit>
    </s:form>
    <s:property value="tip"/>
  </body>
</html>



你可能感兴趣的:(1Action处理多个业务逻辑,form动态提交,一个表单多个提交按钮)