1、 ActionMethod:

Action执行的时候并不一定要执行execute方法,有两种替换办法如下:

①在配置文件中配置action的时候用“method”属性来指定执行哪个方法

②在url地址中动态指定(动态方法调用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="user" extends="struts-default" namespace="/user">
<action name="userAdd" class="com.bjsxt.struts2.user.action.UserAction" method="add">
<result>/user_add_success.jsp</result>
</action>
<action name="user" class="com.bjsxt.struts2.user.action.UserAction">
<result>/user_add_success.jsp</result>
<result name="delete">/user_delete_success.jsp</result>
</action>
</package>
</struts>

Index.jsp页面的内容:

 <?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<% String context = request.getContextPath(); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
<a href="<%=context %>/user/userAdd">添加用户</a><br />
<a href="<%=context %>/user/user!add">添加用户</a><br />
<a href="<%=context %>/user/user!delete">删除用户</a><br />
</body>
</html>

UserAction:

 package com.bjsxt.struts2.user.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
public String add() {
return SUCCESS;
}
public String delete(){
return "delete";
}
}

2、 使用通配符

struts2的配置文件是 struts.xml 在这个配置文件里面可以使用通配符..其中的好处就是,大大减少了配置文件的内容.当然,付出的代价是可读性.

使用通配符的原则是 约定高于配置.

在项目中,我们有很多的命名规则是约定的.我们使用通配符那就必须有一个统一的约定.否则通配符将无法成立

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.bjsxt.struts2.action.StudentAction" method="{1}">
<result>/Student{1}_success.jsp</result>
</action> <action name="*_*" class="com.bjsxt.struts2.action.{1}Action" method="{2}">
<result>/{1}_{2}_success.jsp</result>
</action>
</package>
</struts>

{1}表示的是第一个"*"的内容,注意,这里的大括号,比如,如果是Student_add那么{1}就是表示 add ..

当然,这里只有一个"*".你可以有两个,甚至三个四个....比如这样写 *_* 这样就是两个"*" .那么我们也可以用{1},{2}来分别的表示他们.

Action的内容:

 public class CourseAction extends ActionSupport {
public String add() {
return SUCCESS;
}
public String delete() {
return SUCCESS;
}
}
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:

 <?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%String context = request.getContextPath();%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
使用通配符,将配置量降到最低<br />
<a href="<%=context%>/actions/Studentadd">添加学生</a>
<a href="<%=context%>/actions/Studentdelete">删除学生</a><br />
不过,一定要遵守"约定优于配置"的原则<br />
<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>

相应的jsp页面有:

Course_add_seccess.jsp
Course_delete_success.jsp
Teacher_add_seccess.jsp
Teacher_delete_success.jsp
Studentadd_success.jsp
Studentdelete_success.jsp

最新文章

  1. 解决IE8 内置JSON.stringify,中文变unicode的问题
  2. property attribute: assign, strong, weak, unsafe_unretain and copy
  3. Shell教程6-Shell注释
  4. [三分]HDOJ 5531 Rebuild
  5. iOS tabbar点击动画效果实现
  6. Spring Boot启动过程(一)
  7. 老李推荐:第6章2节《MonkeyRunner源码剖析》Monkey原理分析-事件源-事件源概览-获取命令字串
  8. POJ 2593 Max Sequence
  9. 学习ELK之----01.建立ELK测试项目
  10. &#127827;JavaScript 对象原型链继承的弊端 &#127827;
  11. 自动化运维Ansible安装篇
  12. [No0000166]CPU的组成结构及其原理
  13. .NET/C#中对自定义对象集合进行自定义排序的方法
  14. html内容滚动
  15. WPF-学习笔记 动态修改控件Margin的值
  16. 变量声明和定义的关系------c++ primer
  17. 删除重复数据,保留一条ID最小的
  18. 状态机工作流,顺序工作流和Flowchart
  19. NLP--edit distance
  20. 洛谷 P1478 陶陶摘苹果(升级版)【贪心/结构体排序/可用01背包待补】

热门文章

  1. Linux时间子系统(五) POSIX Clock
  2. 更改Android应用程序的图标
  3. 批处理学习笔记11 - del命令和rd命令
  4. 服务器swap
  5. 深入浅出分析MySQL常用存储引擎
  6. Spark踩坑记——数据库(Hbase+Mysql)转
  7. tab栏切换fengz
  8. C++面向对象程序设计的一些知识点(4)
  9. opensips安装
  10. python学习笔记(17)--eclipse和pydev的安装及汉化