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>
總結
以上是生活随笔為你收集整理的Struts2 ActionWildcard(通配符配置)约定优于配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hbase 学习(十三)集群间备份原理
- 下一篇: TYVJ P1012 火柴棒等式 Lab