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>

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏青青天空树

springboot+security整合3

  这篇讲解如何自定义鉴权过程,实现根据数据库查询出的url和method是否匹配当前请求的url和method来决定有没有权限。security鉴权过程如下:...

51120
来自专栏JavaEdge

SpringMVC之Controller查找(Spring4.0.3/Spring5.0.4源码进化对比)0 摘要1 SpringMVC请求流程2 SpringMVC初始化过程总结

44260
来自专栏函数式编程语言及工具

Scalaz(49)- scalaz-stream: 深入了解-Sink/Channel

   一个完整的scalaz-stream有以下几个部分组成:Source -> Transducer -> Sink,用直白文字来描述就是:“输入 -> 传换...

22380
来自专栏osc同步分享-java技术分享站

springmvc 其他

<!-- 配置直接转发的页面 --> <!-- 可以直接相应转发的页面, 而无需再经过 Handler 的方法. --> <mvc:view-contr...

36180
来自专栏用户画像

Java 枚举 String-String

10230
来自专栏用户2442861的专栏

HttpEntity的类型及其使用

  代表底层流的基本实体。通常是在http报文中获取的实体。他只有一个空参的构造方法。刚创建时没有内容,长度为负值。需要通过两个方法,把值赋进去。

1.1K10
来自专栏编程坑太多

springboot(17)统一异常处理

20820
来自专栏小樱的经验随笔

Codeforces 708A Letters Cyclic Shift

A. Letters Cyclic Shift time limit per test:1 second memory limit per test:256 m...

353100
来自专栏乐享123

Javascript设计模式 - 笔记2

13850
来自专栏ml

HDUOJ----数塔

数塔 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Oth...

32770

扫码关注云+社区

领取腾讯云代金券