Struts2动态方法调用

Struts2框架支持动态方法调用(Dynamic Method Invocation,DMI),解决一个Action对应多个请求的处理,减少Action数量。但是动态方法调用会带来安全隐患,通过URL可以执行Action中的任意方法,这一点应该注意!

动态方法调用有三种方法:

  1. 在struts.xml文件中配置Action节点的method属性
  2. 使用感叹号!标识要调用的方法名称(需在struts2.xml文件中设置常量开启功能,不推荐使用)
  3. 通配符方式(推荐使用)

第一种方法

同一个Action类中定义多个方法分别处理不一样的请求,需要为同一个Action类配置多个action节点。

示例:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="struts2" extends="struts-default" abstract="false">
		<!-- 下面三个action节点的class属性值都是cn.iborder.action.UserAction,其他两个属性值不同action节点不一样 -->
		<action name="logout" class="cn.iborder.action.UserAction" method="logout">
			<result name="success">/login.jsp</result>
		</action>
		<action name="login" class="cn.iborder.action.UserAction" method="login">
			<result name="success" type="redirect">/index.jsp</result>
			<result name="error" type="redirect">/error.jsp</result>
		</action>
		<action name="register" class="cn.iborder.action.UserAction" method="register">
			<result name="success" type="redirect">/login.jsp</result>
		</action>
	</package>
</struts>

第二种方法

在struts.xml文件中,设置constant元素的属性struts.enable.DynamicMethodInvocation值为true,开启Struts2动态方法调用功能(感叹号方式)。

通过在Action的名称中使用感叹号!来标识要调用的方法名称,格式为actionName!methodName.后缀。

示例:

User.java

package cn.iborder.entity;

public class User {
	private String userName;
	private String password;
	
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}

UserAction.java

package cn.iborder.action;

import com.opensymphony.xwork2.ActionSupport;
import cn.iborder.entity.User;

public class UserAction extends ActionSupport {
	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String login() {
		System.out.println("用户:"+user.getUserName());
		System.out.println("密码:"+user.getPassword());
		return "login";
	}
	
	public String logout() {
		System.out.println("成功注销");
		return "logout";
	}
}

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<!-- developerment Mode:开发模式,修改配置文件保存生效,不需重启tmocat -->
    <constant name="struts.devMode" value="true" />
    <!-- 开启动态方法调用 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
    
	<package name="struts2" extends="struts-default" abstract="false">
		<!-- 
             这个action节点的name属性值不能是user(全小写),否则报错,原因未知;
             测试发现,Action类类名去掉“Action”剩下前面那部分(全小写)不能是用作action节点的name属性值。
             前面使用的是myeclipse自带的Struts2.1版本,后改成2.3.31版本中没有这个问题
        -->
		<action name="User" class="cn.iborder.action.UserAction">
			<result name="login" type="redirect" >/index.jsp</result>
			<result name="logout" type="redirect">/login.jsp</result>
		</action>
		
	</package>
</struts>

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录</title>
</head>
<body>
	<h1>登录</h1>
	<form action="${pageContext.request.contextPath }/User!login.action" method="post">
		用户<input type="text" name="user.userName" /><br/>
		密码<input type="password" name="user.password" /><br/>
		<input type="submit" value="提交"/>
	</form>
</body>
</html>

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'index.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>
    <h1>hello world</h1>
    <a href="${pageContext.request.contextPath }/User!logout.action">注销</a>
  </body>
</html>

第三种方法

示例:

Student.java

package cn.iborder.entity;

public class Student {
	private String stuName;
	private String password;
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

StuAction.java

package cn.iborder.action;

import com.opensymphony.xwork2.ActionSupport;
import cn.iborder.entity.Student;

public class StuAction extends ActionSupport {
	private Student student;

	public Student getUser() {
		return student;
	}

	public void setUser(Student student) {
		this.student = student;
	}

	public String login() {
		System.out.println("用户:"+student.getStuName());
		System.out.println("密码:"+student.getPassword());
		return "login_success";
	}
	
	public String logout() {
		System.out.println("成功注销");
		return "logout_success";
	}
}

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<!-- developerment Mode:开发模式,修改配置文件保存生效,不需重启tmocat -->
    <constant name="struts.devMode" value="true" />
    
	<package name="struts2" extends="struts-default" abstract="false">
		<!--
			action节点的name属性值中的"*"代表通配符,可以匹配任意最字符,method属性值和第一个"*"的内容相同
			 例:jsp文件中的一个连接${pageContext.request.contextPath }/Stu_logout.action,其中Stu_logout.action
			 中的logout代表"*"匹配的字符此时method的属性值也是logout
		-->
		<action name="Stu_*" class="cn.iborder.action.StuAction" method="{1}">
			<result name="login_success" type="redirect" >/index.jsp</result>
			<result name="logout_success" type="redirect">/login.jsp</result>
			<result name="error" type="redirect">/error.jsp</result>
		</action>
	</package>
</struts>

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录</title>
</head>
<body>
	<h1>登录</h1>
    <!-- 表示调用Action类中的login方法 -->
	<form action="${pageContext.request.contextPath }/Stu_login.action" method="post">
		用户<input type="text" name="user.stuName" /><br/>
		密码<input type="password" name="user.password" /><br/>
		<input type="submit" value="提交"/>
	</form>
</body>
</html>

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'index.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>
    <h1>hello world</h1>
    <!-- 表示调用Action类中的logout方法 -->
    <a href="${pageContext.request.contextPath }/Stu_logout.action">注销</a>
  </body>
</html>

 

 

转载于:https://my.oschina.net/u/2321708/blog/808023

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值