【web开发学习笔记】Structs2 Action学习笔记(三)action通配符的使用

action学习笔记3-有关于通配符的讨论

使用通配符,将配置量降到最低,只是,一定要遵守"约定优于配置"的原则。

一:前端htm


<前端代码html>
	</head>
		<body>
			<a href="<%=context %>/actions/Studentadd">加入学生</a>
			<a href="<%=context %>/actions/Studentdelete">删除学生</a>
			<a href="<%=context %>/actions/Teacher_add">加入老师</a>
			<a href="<%=context %>/actions/Teacher_delete">删除老师</a>
		</body>
	</html>

二:struct.xml

//struct.xml
<struts>
    <constant name="struts.devMode" value="true" />
    <package name="actions" extends="struts-default" namespace="/actions">
        <action name="Student*" class="com.struts2.action.StudentAction" method="{1}">
            <result>/Student{1}_success.jsp</result>
        </action>        
        <action name="*_*" class="com.struts2.action.{1}Action" method="{2}">
            <result>/{1}_{2}_success.jsp</result>
        </action>
    </package>
</struts>

三:类包

//structs2调用的类包
package com.struts2.action;
import com.opensymphony.xwork2.ActionSupport;
public class StudentAction extends ActionSupport {
	public String add() {
		return SUCCESS;
	}
	public String delete() {
		return SUCCESS;
	}	
}

四:过程分析

运行步骤:点击<a href="<%=context %>/actions/Studentadd">加入学生</a> -> 通过struct.xml查找/actions/Studentadd -> 没有相匹配的action,则与namespace里面的Student*匹配  -> 调用类class="com.struts2.action.StudentAction -> 运行方法的选择,Studentadd与Student*匹配后,*就表示为add,{1},{2}的选择之后讨论。

五:讨论

加入一个新类
package com.bjsxt.struts2.action;
import com.opensymphony.xwork2.ActionSupport;
public class TeacherAction extends ActionSupport {
	public String add() {
		return SUCCESS;
	}	
	public String delete() {
		return SUCCESS;
	}	
}

当我们点击<a href="<%=context %>/actions/TeacherAdd">加入老师</a> -> 通过struct.xml查找/actions/TeacherAdd-> 没有相匹配的action,则与namespace里面的*_*匹配  -> 调用类class="com.struts2.action.{1}Action -> 运行方法的选择,Teacher与*_*中第一个星号匹配后,第二个*就表示为add。{1},{2}的选择依据*的顺序决定。

六:结论

约定的好的话,配置极其简单。通过第五步的讨论,当我们继续加入新类之后,配置不须要改动。
原文地址:https://www.cnblogs.com/wgwyanfs/p/7225728.html