01.创建对应的login.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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 'index.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> <%-- 通配符的使用!!! 必须要掌握!!!!--%> <a href="user/UserAction_add">新增用户</a> <a href="user/UserAction_delete">删除用户</a> <a href="user/UserAction_update">修改用户</a> <a href="user/UserAction_find">查询用户</a> <%--验证{0} 是整个通配符 一般不使用--%> <a href="user/UserAction_aaa">验证{0} 是整个通配符</a> <a href="user/UserAction_validates">验证精确匹配</a> </body> </html>
02.创建struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="true"/> <package name="default" namespace="/user" extends="struts-default"> <!-- <a href="user/UserAction_add">新增用户</a> <a href="user/UserAction_delete">删除用户</a> 使用通配符的方式: 前提====》命名规范 *:就是通配符 {0}:匹配整个Action的name ===》 *_*(这不是笑脸!!!) {1}:匹配的是通配符中的第1个子串 {2}:匹配的是通配符中的第2个子串 <a href="user/UserAction_validate">验证</a> 在xml文件中 通配符能匹配action 但是普通的action也能匹配! 到底执行哪一个? 精确匹配 肯定优先 模糊匹配 --> <action name="*_*" class="cn.bdqn.action.{1}" method="{2}"> <result name="addSuccess">/addSuccess.jsp</result> <result name="updateSuccess">/updateSuccess.jsp</result> <result name="findSuccess">/findSuccess.jsp</result> <result name="deleteSuccess">/deleteSuccess.jsp</result> <result name="validatesSuccess">/validatesSuccess.jsp</result> </action> <!-- 一般不使用{0} <a href="user/UserAction_aaa">验证{0} 是整个通配符</a> <action name="*_*" class="cn.bdqn.action.{1}" method="{0}"> <result name="validatesSuccess">/validatesSuccess.jsp</result> </action> --> <action name="UserAction_validates" class="cn.bdqn.action.UserAction" method="validates"> <result name="validatesSuccess">/deleteSuccess.jsp</result> </action> </package> </struts>
03.创建对应的UserAction
package cn.bdqn.action; import com.opensymphony.xwork2.ActionSupport; /** * 用户登录的action */ public class UserAction extends ActionSupport{ //新增用户的方法 public String add(){ System.out.println("执行add()"); return "addSuccess"; } //修改用户的方法 public String update(){ System.out.println("执行update()"); return "updateSuccess"; } //查询用户的方法 public String find(){ System.out.println("执行find()"); return "findSuccess"; } //删除用户的方法 public String delete(){ System.out.println("执行delete()"); return "deleteSuccess"; } //验证的方法 public String validates(){ System.out.println("执行validates()"); return "validatesSuccess"; } //验证{0}的方法 public String UserAction_aaa(){ System.out.println("执行UserAction_aaa()"); return "validatesSuccess"; } }
04.创建对应的成功页面 省略!!!