一个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" name="code">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;}@Overridepublic String execute() throws Exception {System.out.println("登录成功");if(getUsername().equals("admin") && getPassword().equals("admin")){setTip("恭喜,登录成功");return SUCCESS;}else{setTip("悲哀,登录失败");return INPUT;}}}
<%@ 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>