zoukankan      html  css  js  c++  java
  • Struts2 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_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="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="<%=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>
        My JSP 'index.jsp' starting page<br>
        <a href="<%=basePath %>Student_add">增加学生</a>
        <a href="<%=basePath %>Student_delete">删除学生</a><br>
        <a href="<%=basePath %>Student_edit">编辑学生</a>
        <a href="<%=basePath %>Student_find">查看学生</a><br>
        <a href="<%=basePath %>Teacher_add">增加老师</a>
        <a href="<%=basePath %>Teacher_delete">删除老师</a><br>
        <a href="<%=basePath %>Teacher_edit">编辑老师</a>
        <a href="<%=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="<%=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="styles.css">
     -->

      </head>
     
      <body>
       My JSP 'Student_add.jsp' starting page <br>
       <a href="<%=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="<%=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="styles.css">
     -->

      </head>
     
      <body>
        My JSP 'Student_delete.jsp' starting page <br>
       <a href="<%=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="<%=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="styles.css">
     -->

      </head>
     
      <body>
        My JSP 'Student_edit.jsp' starting page <br>
        <a href="<%=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="<%=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="styles.css">
     -->

      </head>
     
      <body>
        My JSP 'Student_find.jsp' starting page <br>
      <a href="<%=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="<%=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="styles.css">
     -->

      </head>
     
      <body>
       My JSP 'Teacher_add.jsp' starting page<br>
       <a href="<%=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="<%=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="styles.css">
     -->

      </head>
     
      <body>
       My JSP 'Teacher_delete.jsp' starting page <br>
      <a href="<%=basePath %>index.jsp">home</a>
      </body>
    </html>

  • 相关阅读:
    nginx日志
    silverlight 双击事件
    Silverlight button 图片切换样式
    Caliburn.Micro学习笔记(一)----引导类和命名匹配规则
    关闭Outlook时最小化 dll
    wpf键盘记录器
    WPF之TreeList的实现方法(一)
    精典算法之详解 河内之塔
    精典算法之二分查找法
    指针数组和数组指针
  • 原文地址:https://www.cnblogs.com/hongten/p/2122336.html
Copyright © 2011-2022 走看看