ActionWildcard通配符

新建web project:struts2_0500_actionwildcard Build Path 项目图: src: StudentAction.java TeacherAction.java struts.xml WebRoot: index.jsp Student_add.jsp Student_delete.jsp Student_edit.jsp Student_find.jsp Teacher_add.jsp Teacher_

新建web project:struts2_0500_actionwildcard

Build Path

项目图:

  src:                  

    StudentAction.java

    TeacherAction.java

    struts.xml

  WebRoot:

    index.jsp

    Student_add.jsp

    Student_delete.jsp

    Student_edit.jsp

    Student_find.jsp

    Teacher_add.jsp

    Teacher_delete.jsp

 ------------------------------------Hongten---------------------------------

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>
 <constant name="struts.devMode" value="http://www.cnblogs.com/hongten/archive/2011/07/31/true" />
 <package name="Student" namespace="/" extends="struts-default">
  <action name="*_*" class="com.b510.hongten.{1}Action">
   <result>
    /{1}_{2}.jsp
            </result>
  </action>
  
  
  <action name="Student_add" class="com.b510.hongten.StudentAction"
   method="add">
   <result>
    /Student_delete.jsp
            </result>
  </action>
 </package>
</struts>

 ------------------------------------Hongten---------------------------------

 ------------------------------------Hongten---------------------------------

在这里,我们没有去添加Teacher_edit.jsp和Teacher_find.jsp,要想申明的是,如不美不雅我们要添加的时辰

直接添加便可,不会因为我们又添加了新的的文件,而影响整个法度模范的运行。可是添加的时辰

必定要遵守"约定优于设置装备安排"的原则。如:Teacher的首字母必定要大年夜写,Teacher_edit.jsp就得必定要以

这种形式去写。不然我们还是免不了去改削设置装备安排文件;

还有一个就是,我们看到struts.xml文件中有两个action,其拭魅这里只是为了做一个小测试二用的:

我们的法度模范中只用:

  <action name="*_*" class="com.b510.hongten.{1}Action">
   <result>/{1}_{2}.jsp</result>
    </action>

就足可使我们的法度模范很好的运行起来,可是添加了第二个action后:

  <action name="Student_add" class="com.b510.hongten.StudentAction"
   method="add">
   <result>/Student_delete.jsp</result>
   </action>

这时辰就会显现我们访谒一个url:http://localhost:1000/struts2_0500_actionwildcard/Student_add

的时辰,是去的是:Student_delete.jsp 这个页面,而不是我们的Student_add.jsp页面,这是为甚么呢?

原因是:在struts2中,当我们访谒的url来到的时辰,处事器就会在struts.xml文件中找最接近这个url的action(如不美不雅

是同一个包中),我们很等闲发现:

  "*_*"和"Student_add" 和url对角力计较,显然是后者要接近,所以选择了Student_delete.jsp,而非Student_add.jsp
 

 ------------------------------------Hongten--------------------------------- 

 ------------------------------------Hongten---------------------------------

StudentAction.java

代码:

package com.b510.hongten;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @author XHW
 * 
 * @date 2011-7-30
 * 
 */
public class StudentAction extends ActionSupport {
 private static final long serialVersionUID = -5023520095036169842L;

 public String add() {
  return SUCCESS;
 }

 public String delete() {
  return SUCCESS;
 }

 public String edit() {
  return SUCCESS;
 }

 public String find() {
  return SUCCESS;
 }
}

 ------------------------------------Hongten---------------------------------

 
 

TeacherAction.java

代码;

package com.b510.hongten;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @author XHW
 * 
 * @date 2011-7-30
 * 
 */
public class TeacherAction extends ActionSupport {
 private static final long serialVersionUID = -5023520095036169843L;

 public String add() {
  return SUCCESS;
 }

 public String delete() {
  return SUCCESS;
 }

 public String edit() {
  return SUCCESS;
 }

 public String find() {
  return SUCCESS;
 }
}

 ------------------------------------Hongten---------------------------------

 index.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="http://www.cnblogs.com/hongten/archive/2011/07/31/<%=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="http://www.cnblogs.com/hongten/archive/2011/07/31/styles.css">
 -->
  </head>
  
  <body>
    My JSP 'index.jsp' starting page<br>
    <a href="http://www.cnblogs.com/hongten/archive/2011/07/31/<%=basePath %>Student_add">增添学生</a>
    <a href="http://www.cnblogs.com/hongten/archive/2011/07/31/<%=basePath %>Student_delete">删除学生</a><br>
    <a href="http://www.cnblogs.com/hongten/archive/2011/07/31/<%=basePath %>Student_edit">编辑学生</a>
    <a href="http://www.cnblogs.com/hongten/archive/2011/07/31/<%=basePath %>Student_find">查看学生</a><br>
    <a href="http://www.cnblogs.com/hongten/archive/2011/07/31/<%=basePath %>Teacher_add">增添教员</a>
    <a href="http://www.cnblogs.com/hongten/archive/2011/07/31/<%=basePath %>Teacher_delete">删除教员</a><br>
    <a href="http://www.cnblogs.com/hongten/archive/2011/07/31/<%=basePath %>Teacher_edit">编辑教员</a>
    <a href="http://www.cnblogs.com/hongten/archive/2011/07/31/<%=basePath %>Teacher_find">查看教员</a><br>
  </body>
</html>

 ------------------------------------Hongten---------------------------------

Student_add.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="http://www.cnblogs.com/hongten/archive/2011/07/31/<%=basePath%>">
    
    <title>My JSP 'Student_add.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="http://www.cnblogs.com/hongten/archive/2011/07/31/styles.css">
 -->

  </head>
  

 
 

  <body>
   My JSP 'Student_add.jsp' starting page <br>
   <a href="http://www.cnblogs.com/hongten/archive/2011/07/31/<%=basePath %>index.jsp">home</a>
  </body>
</html>

 ------------------------------------Hongten---------------------------------

Student_delete.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="http://www.cnblogs.com/hongten/archive/2011/07/31/<%=basePath%>">
    
    <title>My JSP 'Student_delete.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="http://www.cnblogs.com/hongten/archive/2011/07/31/styles.css">
 -->

  </head>
  
  <body>
    My JSP 'Student_delete.jsp' starting page <br>
   <a href="http://www.cnblogs.com/hongten/archive/2011/07/31/<%=basePath %>index.jsp">home</a>
  </body>
</html>

 ------------------------------------Hongten---------------------------------

Student_edit.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="http://www.cnblogs.com/hongten/archive/2011/07/31/<%=basePath%>">
    
    <title>My JSP 'Student_edit.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="http://www.cnblogs.com/hongten/archive/2011/07/31/styles.css">
 -->

  </head>
  
  <body>
    My JSP 'Student_edit.jsp' starting page <br>
    <a href="http://www.cnblogs.com/hongten/archive/2011/07/31/<%=basePath %>index.jsp">home</a>
  </body>
</html>

 ------------------------------------Hongten---------------------------------

Student_find.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="http://www.cnblogs.com/hongten/archive/2011/07/31/<%=basePath%>">
    
    <title>My JSP 'Student_find.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="http://www.cnblogs.com/hongten/archive/2011/07/31/styles.css">

 

 

 -->

  </head>
  
  <body>
    My JSP 'Student_find.jsp' starting page <br>
  <a href="http://www.cnblogs.com/hongten/archive/2011/07/31/<%=basePath %>index.jsp">home</a>
  </body>
</html>

 ------------------------------------Hongten---------------------------------

Teacher_add.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="http://www.cnblogs.com/hongten/archive/2011/07/31/<%=basePath%>">
    
    <title>My JSP 'Teacher_add.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="http://www.cnblogs.com/hongten/archive/2011/07/31/styles.css">
 -->

  </head>
  
  <body>
   My JSP 'Teacher_add.jsp' starting page<br>
   <a href="http://www.cnblogs.com/hongten/archive/2011/07/31/<%=basePath %>index.jsp">home</a>
  </body>
</html>

 

 ------------------------------------Hongten---------------------------------

Teacher_delete.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="http://www.cnblogs.com/hongten/archive/2011/07/31/<%=basePath%>">
    
    <title>My JSP 'Teacher_delete.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="http://www.cnblogs.com/hongten/archive/2011/07/31/styles.css">
 -->

  </head>
  
  <body>
   My JSP 'Teacher_delete.jsp' starting page <br>
  <a href="http://www.cnblogs.com/hongten/archive/2011/07/31/<%=basePath %>index.jsp">home</a>
  </body>
</html>

原文地址:https://www.cnblogs.com/90zyh/p/2992789.html