Struts2 DMI的使用
Struts2的Action类中可以放置多个方法并在struts.xml中配置供页面调用。只要方法符合execute()方法的标准即返回类型是String就可以了。
同一个Action多个方法调用的方式中有三种方法
1.在<action>标签中有一个method的属性可以配置自定义的方法。
2.使用DMI方法。
3.使用通配符
今天我们着重介绍第两种方法的使用:
第一种方法的弊端是你在Action类中定义多少方法就需要使用多少个<action></action>标签,这样会导致struts.xml文件越来越大。
第二种方法的使用:
Action类的:
1 package test.struts2.DMI; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class TestDMI extends ActionSupport { 6 private String message; 7 8 public String getMessage() { 9 return message; 10 } 11 12 public String searchUsers()throws Exception{ 13 message = "你调用的是searchUsers()查询方法!"; 14 return SUCCESS; 15 } 16 17 public String addUser()throws Exception{ 18 message = "你调用的是addUser()添加方法!"; 19 return SUCCESS; 20 } 21 22 }
和一起写法一样
struts.xml配置文件写法
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" > 3 <struts> 4 <constant name="struts.i18n.encoding" value="GBK"></constant> 5 <package name="struts2" extends="struts-default" namespace="/"> 6 <action name="user" class="test.struts2.DMI.TestDMI"> 7 <result>result.jsp</result> 8 </action> 9 <!--下面两个是第一种方法的写法,如果使用DMI下面两个action就不要了--> 10 <action name="userAdd" class="test.struts2.DMI.TestDMI" method="addUser"> 11 <result>result.jsp</result> 12 </action> 13 <action name="userSearch" class="test.struts2.DMI.TestDMI" method="searchUsers"> 14 <result>result.jsp</result> 15 </action> 16 </package> 17 </struts>
页面调用的写法
1 <%@ page language="java" pageEncoding="GBK"%> 2 <% 3 String path = request.getContextPath(); 4 %> 5 6 <html> 7 <head> 8 9 <title>Struts DMI</title> 10 <meta http-equiv="pragma" content="no-cache"> 11 <meta http-equiv="cache-control" content="no-cache"> 12 <meta http-equiv="expires" content="0"> 13 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 14 <meta http-equiv="description" content="This is my page"> 15 </head> 16 17 <body> 18 <a href="userAdd">通过普通调用addUser()方法</a> 19 <br/> 20 <a href="userSearch">通过普通调用searchUsers()方法</a> 21 <hr> 22 <a href="user!addUser">通过DMI方式调用addUser()方法</a> 23 <br/> 24 <a href="user!searchUsers">通过DMI方式调用searchUsers()方法</a> 25 </body> 26 </html>
注意:大家看我的超链接中userAdd并没有加扩展名.action因为Struts2默认就会加上.action,我说的是在你没有更改默认扩展名的情况下可以不加扩展名。如果你把扩展名修改了就必须加上你修改过的扩展名如:.do;加了扩展名的页面如下:
1 <%@ page language="java" pageEncoding="GBK"%> 2 <% 3 String path = request.getContextPath(); 4 %> 5 6 <html> 7 <head> 8 9 <title>Struts DMI</title> 10 <meta http-equiv="pragma" content="no-cache"> 11 <meta http-equiv="cache-control" content="no-cache"> 12 <meta http-equiv="expires" content="0"> 13 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 14 <meta http-equiv="description" content="This is my page"> 15 </head> 16 17 <body> 18 <a href="userAdd.action">通过普通调用addUser()方法</a> 19 <br/> 20 <a href="userSearch.action">通过普通调用searchUsers()方法</a> 21 <hr> 22 <a href="user!addUser.action">通过DMI方式调用addUser()方法</a> 23 <br/> 24 <a href="user!searchUsers.action">通过DMI方式调用searchUsers()方法</a> 25 </body> 26 </html>
如果想在链接后面加参数的话写法如:href="user!addUser.action?userName=张三"
第三种使用通配符弊端是struts.xml中比较混乱,代码可读性比较差。
posted on 2013-09-05 19:35 Arts&Crafts 阅读(631) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构