一、创建JavaBean:LoginBean
package com.zpj.domain;public class LoginBean { private String username; private String pwd; public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; }}
二、创建Action:LoginAction
package com.zpj.action;import com.opensymphony.xwork2.ActionSupport;import com.zpj.domain.LoginBean;public class LoginAction extends ActionSupport { private LoginBean loginBean; public LoginBean getLoginBean() { return loginBean; } public void setLoginBean(LoginBean loginBean) { this.loginBean = loginBean; } /* * 默认的validate()方法; */ public void validate() { System.out.println("这个是validate方法=====1"); super.validate(); } /* * 执行完这个方法后将执行默认的validate()方法; */ public void validateSaveUser() { System.out.println("这个是validateSaveUser方法=====2"); super.validate(); } /* * 默认的execute()方法; */ public String execute() throws Exception { System.out.println("这个是execute方法=====3"); return "ok"; } /* * 如果执行了此方法,则不再执行默认的execute()方法; */ public String saveUser() throws Exception { System.out.println("这个是saveUser方法=====4"); return "saveUserOk"; } /* * 如果执行了此方法,则不再执行默认的execute()方法; */ public String checkUser() throws Exception { System.out.println("这个是checkUser方法=====5"); return "checkUserOk"; }}
三、配置struts.xml文件
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="s2" extends="struts-default"> <!-- 定义多动作控制器方法一 --> <action name="loginAction" class="com.zpj.action.LoginAction"> <result name="ok">/success.jsp</result> <result name="saveUserOk">/success.jsp</result> <result name="checkUserOk">/success.jsp</result> <!-- 重定向到index.jsp页面 --> <result name="input" type="redirect">/index.jsp</result> </action> <!-- 定义多动作控制器方法二 --> <action name="*loginDo" method="{1}" class="com.zpj.action.LoginAction"> <result name="ok">/success.jsp</result> <result name="saveUserOk">/success.jsp</result> <result name="checkUserOk">/success.jsp</result> <!-- 重定向到index.jsp页面 --> <result name="input" type="redirect">/index.jsp</result> </action> </package> </struts>
四、创建首页index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="s2" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <title>My JSP 'index.jsp' starting page</title> <!-- <link rel="stylesheet" type="text/css" href="http://pannijingling.blog.163.com/blog/styles.css"> --></head><body> <center> <!-- 使用第一种多动作控制器的调用方法--> <s2:form action="loginAction!saveUser"> <s2:textfield name="loginBean.username" label="姓名"></s2:textfield> <s2:password name="loginBean.pwd" label="密码"></s2:password> <s2:submit label="登陆"></s2:submit> </s2:form> <hr> <!-- 使用第二种多动作控制器的调用方法--> <s2:form action="checkUserloginDo"> <s2:textfield name="loginBean.username" label="姓名"></s2:textfield> <s2:password name="loginBean.pwd" label="密码"></s2:password> <s2:submit label="登陆"></s2:submit> </s2:form> <hr> <!-- 下面这个form中 action="loginDo"和 action="loginAction"效果是一样的--> <s2:form action="loginDo"> <s2:textfield name="loginBean.username" label="姓名"></s2:textfield> <s2:password name="loginBean.pwd" label="密码"></s2:password> <s2:submit label="登陆"></s2:submit> </s2:form> </center></body></html>
五、创建登陆成功的页面success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="s2" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <title>My JSP 'success.jsp' starting page</title> <!-- <link rel="stylesheet" type="text/css" href="http://pannijingling.blog.163.com/blog/styles.css"> --></head><body> <center> <font color="red" size="20">欢迎您,登陆成功!</font> <br><br><br><br><br><br> <s2:a href="http://pannijingling.blog.163.com/blog/index.jsp">返回上一页</s2:a> </center></body></html>