struts2中的动态方法调用DMI
2016-01-26 23:26
615 查看
通常,在struts2中,如果想执行特定的方法,往往会在struts.xml中,配置action的method属性值为要执行的方法名,默认为execute方法。为了程序的扩展,这种方法不推荐,而是使用DMI方式,举例如下(场景为用户的增、删、改):
(1)UserAction
[java]
view plaincopyprint?
package com.struts2.study.yy;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private static final long serialVersionUID = 1L;
public String add(){
System.out.println("-----add-----");
return SUCCESS;
}
public String delete(){
System.out.println("-----delete-----");
return SUCCESS;
}
public String modify(){
System.out.println("-----modify-----");
return SUCCESS;
}
}
(2)struts.xml
[html]
view plaincopyprint?
<package name="user" extends="struts-default" namespace="/">
<action name="user" class="com.struts2.study.yy.UserAction">
<result name="success">/user/success.jsp</result>
</action>
</package>
[html]
view plaincopyprint?
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'mainp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="<%=path %>/user!add">add user</a><br>
<a href="<%=path %>/user!delete">delete user</a><br>
<a href="<%=path %>/user!modify">modify user</a>
</html>
(4)跳转页面success.jsp
[html]
view plaincopyprint?
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'success.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
success!
</body>
</html>
(5)结果
当分别点击三个链接时,后台会打出如下日志:
[html]
view plaincopyprint?
-----add-----
-----delete-----
-----modify-----
通过DMI方式,可以简化配置信息。
通常,在struts2中,如果想执行特定的方法,往往会在struts.xml中,配置action的method属性值为要执行的方法名,默认为execute方法。为了程序的扩展,这种方法不推荐,而是使用DMI方式,举例如下(场景为用户的增、删、改):
(1)UserAction
[java]
view plaincopyprint?
package com.struts2.study.yy;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private static final long serialVersionUID = 1L;
public String add(){
System.out.println("-----add-----");
return SUCCESS;
}
public String delete(){
System.out.println("-----delete-----");
return SUCCESS;
}
public String modify(){
System.out.println("-----modify-----");
return SUCCESS;
}
}
package com.struts2.study.yy; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { private static final long serialVersionUID = 1L; public String add(){ System.out.println("-----add-----"); return SUCCESS; } public String delete(){ System.out.println("-----delete-----"); return SUCCESS; } public String modify(){ System.out.println("-----modify-----"); return SUCCESS; } }
(2)struts.xml
[html]
view plaincopyprint?
<package name="user" extends="struts-default" namespace="/">
<action name="user" class="com.struts2.study.yy.UserAction">
<result name="success">/user/success.jsp</result>
</action>
</package>
<package name="user" extends="struts-default" namespace="/"> <action name="user" class="com.struts2.study.yy.UserAction"> <result name="success">/user/success.jsp</result> </action> </package>(3)程序入口main.jsp
[html]
view plaincopyprint?
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'mainp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="<%=path %>/user!add">add user</a><br>
<a href="<%=path %>/user!delete">delete user</a><br>
<a href="<%=path %>/user!modify">modify user</a>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'mainp.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <a href="<%=path %>/user!add">add user</a><br> <a href="<%=path %>/user!delete">delete user</a><br> <a href="<%=path %>/user!modify">modify user</a> </html>
(4)跳转页面success.jsp
[html]
view plaincopyprint?
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'success.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
success!
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> success! </body> </html>
(5)结果
当分别点击三个链接时,后台会打出如下日志:
[html]
view plaincopyprint?
-----add-----
-----delete-----
-----modify-----
-----add----- -----delete----- -----modify-----
通过DMI方式,可以简化配置信息。
相关文章推荐
- springMVC乱码问题-转
- struts2拦截器的实现原理及源码剖析
- struts2的核心和工作原理
- java NIO 实现 SocketChannel通信
- struts2的拦截器,替换掉敏感词汇
- Java关键字——super、this
- XML——流机制解析器
- hdu acm hdu 2115 Java
- win 10系统安装myeclipse 9.1 破解问题
- spring mvc javaConfig版本搭建常见问题
- java内部类总结,举例
- XML——使用 XPath来定位信息+使用命名空间
- 运行java的class文件方法详解
- java数组复制
- 充满BUG的世界观——再遇Java内存泄露
- 【Eclipse】--Eclipse反编译插件jadeclipse的安装
- jsp中js获取java的传值
- java实现_用数组实现栈
- Spring 的优秀工具类盘点第 2 部分
- Spring 的优秀工具类盘点第 1 部分