Action动态方法调用DMI,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="default" extends="struts-default" namespace="/user">
<action name="user" class="com.cn.user.action.UserAction">
<result>/user_add_success.jsp</result>
<result name="delete">/user_delete_success.jsp</result>
</action>
</package>
</struts>
Index.jsp页面调用:
<a href="<%=basePath %>/user/user!add">添加用户</a><br />
<a href="<%=basePath %>/user/user!delete">删除用户</a><br />
UserAction的内容:
package com.cn.user.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
public String add() {
return "success";
}
public String delete(){
return "delete";
}
}
使用通配符
Strtus.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="actions" extends="struts-default" namespace="/actions">
<action name="Student*" class="com.cn.action.StudentAction" method="{1}">
<result>/Student{1}_success.jsp</result>
</action>
<action name="*_*" class="com.cn.action.{1}Action" method="{2}">
<result>/{1}_{2}_success.jsp</result>
</action>
</package>
</struts>
Action的内容:
public class StudentAction extends ActionSupport {
public String add() {
return SUCCESS;
}
public String delete() {
return SUCCESS;
}
}
public class TeacherAction extends ActionSupport {
public String add() {
return SUCCESS;
}
public String delete() {
return SUCCESS;
}
}
Index.jsp页面的内容:
使用通配符,将配置量降到最低<br />
但是 一定要遵守"约定优于配置"的原则,在实际项目开发中由于多人开发,为了使代码通俗易懂一般情况下很少使用通配符。<br />
<a href="<%=basePath%>/actions/Studentadd">添加学生</a>
<a href="<%=basePath%>/actions/Studentdelete">删除学生</a><br />
//上面配置文件里({1}_{2}_success.jsp)在这里即Teacher_add_success.jsp
<a href="<%=context%>/actions/Teacher_add">添加老师</a>
<a href="<%=context%>/actions/Teacher_delete">删除老师</a>
<a href="<%=context%>/actions/Course_add">添加课程</a>
<a href="<%=context%>/actions/Course_delete">删除课程</a>
</body>
</html>