五、动态方法调用Dynamic Method Invocation
采用“action!方法名称” 方式调用
范例:
(1)编写UserAction
package com.zgy.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
public String add(){
return SUCCESS;
}
}
(2)配置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="user" class="com.zgy.action.UserAction">
<result>/user_add_success.jsp</result>
</action>
</package>
</struts>
(3)编写user_add_success.jsp
<%@ 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 ‘user_add_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>
user add success <br>
</body>
</html>
(4)浏览器访问如下url
http://localhost:8080/DynamicMethodInvocation/user/user!add
使用通配符可以将配置量降到最低。使用*匹配,用{n}的形式与第n个*匹配。
范例:
(1)编写StudentAction.java
package com.zgy.action;
public class StudentAction {
public String add(){
return "success";
}
public String delete(){
return "success";
}
}
(2)配置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="actions" extends="struts-default" namespace="/actions">
<action name="Student*" class="com.zgy.action.StudentAction" method="{1}">
<result>/student_{1}_success.jsp</result>
</action>
<action name="*_*" class="com.zgy.{1}Action">
<result>/{1}_{2}_success.jsp</result>
</action>
</package>
</struts>
(3)编写student_add_sucess.jsp
<%@ 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 ‘studentadd_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>
student add success <br>
</body>
</html>
(4)编写student_delete_sccess.jsp
<%@ 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 ‘student_delete_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>
student delete success<br>
</body>
</html>
(5)浏览器端分别访问
http://localhost:8080/ActionWildcard/actions/Studentdelete
http://localhost:8080/ActionWildcard/actions/Studentadd
此种方法是在action类中添加属性,用于接受传递进来的参数
范例:
(1)编写UserAction.jsp
package com.zgy.action;
public class UserAction {
private String name ;
private int age;
public String add(){
System.out.println("name="+name);
System.out.println("age="+age);
return "success";
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public int getAge(){
return this.age;
}
public void setAge(int age){
this.age = age;
}
}
(2)配置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="user" class="com.zgy.action.UserAction" method="add">
<result>/user_add_success.jsp</result>
</action>
</package>
</struts>
(3)编写user_add_success.jsp页面
<%@ 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 ‘user_add_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>
user add success <br>
</body>
</html>
(4)浏览器端输入如下url
http://localhost:8080/ActionAttributeInput/user/user?name=zhangsan&age=24
使用域模型接受参数,然后在action中调用域模型的对象
范例:
(1)创建User.java,包含两个属性:name/age
package com.zgy.domain;
public class User {
private String name ;
private int age ;
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public int getAge(){
return this.age;
}
public void setAge(int age){
this.age = age;
}
}
(2)创建UserAction.java,调用User
package com.zgy.action;
import com.zgy.domain.User;
public class UserAction {
private User user;
public String add(){
System.out.println("name="+user.getName());
System.out.println("age="+user.getAge());
return "success";
}
public User getUser(){
return user;
}
public void setUser(User user){
this.user = user;
}
}
(3)配置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="user" class="com.zgy.action.UserAction" method="add">
<result>/user_add_success.jsp</result>
</action>
</package>
</struts>
(4)编写user_add_success.jsp
<%@ 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 ‘user_add_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>
user add success <br>
</body>
</html>
(5)浏览器端输入
http://localhost:8080/DomainModel/user/user?user.name=zhangsan&user.age=24
原文地址:http://blog.csdn.net/yaguanzhou2014/article/details/41951701