日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Struts2 ActionWildcard(通配符配置)约定优于配置

發布時間:2025/3/8 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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(通配符配置)约定优于配置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。