Struts2学习——0300DMI动态方法调用
来源:互联网 发布:大卫罗宾逊数据 编辑:程序博客网 时间:2024/05/22 13:42
背景
现在有一个Action,它的action_name=user,背景是从现实小项目中抽象出来。现在有一个用户管理系统,而用户管理,肯定不仅仅是添加用户,还有的比如用户登录,后台删除用户等等需求,使得user的操作不仅仅是一个add(),肯定还有delete(),update()等等。所以如果按照我们前面的做法,肯定是弄很多个action,每个action一个execute方法,一个result。这种做法明显是烂透了……
DMI动态方法调用
1. 分析
我们还是基于代码来分析这个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" /> <!----------------- 法一 ------------------> <!-- 名称空间是/user --> <package name="user" extends="struts-default" namespace="/user"> <!-- action_name=userAdd,说明/user/userAdd的URL地址来找这个action --> <!-- action 对应了一个class,这个action对应的是class里面的add方法 --> <action name="userAdd" class="com.bjsxt.struts2.user.action.UserAction" method="add"> <result>/user_add_success.jsp</result> </action> <!----------------- 法二 ------------------> <!-- action_name=user 说明/user/user的URL来找它 --> <!-- 这里并没有指定method,比较灵活 --> <action name="user" class="com.bjsxt.struts2.user.action.UserAction"> <!-- result没有指定name,就是默认的name="success" --> <result>/user_add_success.jsp</result> </action> </package></struts>
UserAction
package com.bjsxt.struts2.user.action;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport { /* 其实也只有一个方法,add(),返回值是success。 */ public String add() { 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> <!-- 第一种方法的方式 --> <a href="<%=context %>/user/userAdd">添加用户</a> <br /> <!-- 法二的URL要加! 指定到底调用的是哪个add()方法 --> <a href="<%=context %>/user/user!add">添加用户</a> <br />前者会产生太多的action,所以不推荐使用</body></html>
2. 结果
index.jsp
法一
法二
可以得到结论,两种方法,都可以达到相同的效果,那么我们到底用哪一个方法更好呢?
3. 对比
两种方法,对比起来,肯定是后一种方法更好。因为,一个User里面肯定不只是一个addUser的方法,肯定有delete、update等方法。如果用的是第一种方法,那么,一种方法就要换一个method,在struts.xml里面就要配置太多东西。而第二种方法,在struts.xml文件里面只要写
<action name="user" class="com.bjsxt.struts2.user.action.UserAction"> <result>/user_add_success.jsp</result></action>
不用因为用其他的方法写很多个action,但是由于它有很多个method,因此虽然不用写多个action,但是一个action还是要针对不同方法返回值,写多个与之匹配的result
若有不足之处,请不吝赐教
0 0
- Struts2学习——0300DMI动态方法调用
- struts2——动态方法调用(dmi)!!!
- Struts2学习 动态方法调用(DMI)
- Struts2-动态方法调用(DMI)
- struts2动态方法调用(DMI)
- Struts2 DMI 动态方法调用
- Struts2 DMI动态方法调用
- 关于Struts2 namespace 的学习笔记(DMI动态方法调用)
- Struts2 ActionMethod 【DMI】(动态方法调用)
- Struts2的动态方法调用DMI
- struts2中的动态方法调用DMI
- Struts2中DMI(动态方法调用)
- Struts2动态方法调用(DMI)小问题
- struts2中的动态方法调用DMI
- struts2 action DMI动态方法调用
- struts2 ActionMethod DMI 动态方法调用
- struts2之动态方法调用(DMI)
- struts2——DMI动态调用失败解决方案
- Win10 睡眠自动重启的设置 --转自百度知道
- 我笔流心琢工词 也表诚心溢刻字
- USA1586 Molar mass
- JAVA接口介绍
- JQuery中遍历元素的父辈和祖先
- Struts2学习——0300DMI动态方法调用
- Android自定义圆角条形图表
- angular.js学习(1)--指令和控制器间的交互
- 1002-大数相加
- 虚函数和多态性1——动态绑定和静态绑定
- Gradle 安装以及查看jar文件本地的存储位置
- python挑战之总结
- POI读取03和07版本
- 数学建模中的Matlab模板程序