一個web應用可能有成百上千個action聲明,可以利用Struts提供的通配符映射機制把多個彼此相似的映射關系簡化為一個映射關系
1.index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="UserAction-save">User Save</a>
<br><br>
<a href="UserAction-update">User Update</a>
<br><br>
<a href="UserAction-delete">User Delete</a>
<br><br>
<a href="UserAction-query">User Query</a>
</body>
</html>
2.struts.xml
**重點:
若找到多個比對,沒有通配符的勝出
若找到不止一個通配符的比對,按先後順序比對**
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 配置Struts可以受理的請求的擴充名 -->
<constant name="struts.action.extension" value="action,do,"></constant>
<package extends="struts-default" name="Hello" namespace="/">
<action name="UserAction-*" class="TestAction.UserAction"
method="{1}">
<result name="{1}-success">/success.jsp</result>
</action>
<action name="*-update" class="TestAction.UserAction"
method="test">
<result name="query-success">/success.jsp</result>
</action>
<!--
<action name="UserAction-save" class="TestAction.UserAction"
method="test">
<result name="query-success">/success.jsp</result>
</action>
-->
</package>
</struts>
3.UserAction.java
package TestAction;
public class UserAction {
public String save()
{
System.out.println("save");
return "save-success";
}
public String update()
{
System.out.println("update");
return "update-success";
}
public String delete()
{
System.out.println("delete");
return "delete-success";
}
public String query()
{
System.out.println("query");
return "query-success";
}
public String test()
{
System.out.println("test");
return "query-success";
}
}