建立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"
<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">
/student_delete.jsp
</package>
</struts>
在這裡,我們沒有去添加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"
<result>/student_delete.jsp</result>
</action>
的時候,是去的是:student_delete.jsp 這個頁面,而不是我們的student_add.jsp頁面,這是為什麼呢?
原因是:在struts2中,當我們通路的url來到的時候,伺服器就會在struts.xml檔案中找最接近這個url的action(如果
是同一個包中),我們很容易發現:
"*_*"和"student_add" 和url相比較,顯然是後者要接近,是以選擇了student_delete.jsp,而非student_add.jsp
------------------------------------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() {
public string edit() {
public string find() {
}
teacheraction.java
代碼;
public class teacheraction extends actionsupport {
private static final long serialversionuid = -5023520095036169843l;
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>
student_add.jsp
<title>my jsp 'student_add.jsp' starting page</title>
my jsp 'student_add.jsp' starting page <br>
<a href="<%=basepath %>index.jsp">home</a>
student_delete.jsp
<title>my jsp 'student_delete.jsp' starting page</title>
my jsp 'student_delete.jsp' starting page <br>
student_edit.jsp
<title>my jsp 'student_edit.jsp' starting page</title>
my jsp 'student_edit.jsp' starting page <br>
<a href="<%=basepath %>index.jsp">home</a>
student_find.jsp
<title>my jsp 'student_find.jsp' starting page</title>
my jsp 'student_find.jsp' starting page <br>
<a href="<%=basepath %>index.jsp">home</a>
teacher_add.jsp
<title>my jsp 'teacher_add.jsp' starting page</title>
my jsp 'teacher_add.jsp' starting page<br>
teacher_delete.jsp
<title>my jsp 'teacher_delete.jsp' starting page</title>
my jsp 'teacher_delete.jsp' starting page <br>