ActionInvocation实例的invoke()与invokeActionOnly() – 记忆角落

ActionInvocation实例的invoke()与invokeActionOnly()

/ 0评 / 1

ActionInvocation实例的invoke()方法

ps:表示调用下一个拦截器或Action,调用invoke()方法时, intercept()方法可任意返回需要的字符串值,一般返回null即可.

  1. invoke()前的代码为请求向下走之前的业务流程处理
  2. invoke()后的代码为嵌套调用流程执行完毕后,请求重新回到原处的后继业务流程处理,相当于方法嵌套调用完毕重新回到原处。
  3. invoke()返回值为嵌套调用拦截器intercept()的返回值或Action返回的处理结果字符串,即上一个流程的返回值

ActionInvocation实例invokeActionOnly()方法

ps:表示抛开所有末调用的嵌套拦截器,直接调用Action.

调用invokeActionOnly()方法时,需注意:

  1. intercept()方法需有返回值,否则Action将无法正确调用视图响应
  2. intercept()方法中可直接返回到视图页面,而流程不往下传递。

intercept()方法中可直接返回与控制器中Action配置相匹配的字符串,并找到响应视图

如返回“error”字符串,会自动找到对应在控制器配置的error页面响应。


简单的项目作业...

设置一个登录页面 包含:用户名、密码输入框、提交按钮, 后台获取两个表单值后进行比对, 如果用户名与密码内容相同则跳到验证成功页面, 如果用户名与密码内容不同则跳到验证失败页面。用Struts框架完成。

增加两个自定义拦截器

拦截器1: 检查用户名、密码输入框是否为空 为空时返回到error1.jsp页面,并提示输入错误

拦截器2: 检查用户名中是否包含“admin”字符串 不包含“admin”字符串时返回到error2.jsp页面,提示没有权限

效果请自行测试,代码有点乱请别介意...

代码如下:

login.jsp

<%@ page language="java" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head> 
  <body>
  <center>
    <form action="login.action" method="post" focus="login">
      <h2>系统登录:</h2>
      <table border="0">
        <tr>
          <td>username:</td>
          <td><input type = "text" name = "username" /></td>
        </tr>
        <tr>
          <td>password:</td>
          <td><input type = "password" name = "password" /></td>
        </tr>
        <tr>
          <td colspan="2" align="center">
          <input type = "submit" name = "submit" value = "提      交" />
          </td>
        </tr>
      </table>
    </form>
   </center>
  </body>
</html>

success.jsp、fail.jsp、error.jsp三个页面省略...

Action类

package com.glj;

import com.opensymphony.xwork2.ActionSupport;

public class userAction extends ActionSupport{
	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;
	}
	@Override
	public String execute() throws Exception {
		System.out.println("执行了userAction");
		if (this.getUsername().equals(this.getPassword())) {
			return "ok";
		}
		else{
			return "no";
		}
	}
}

interceptor 类

package com.inter;

import com.glj.userAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class superCheck implements Interceptor{

	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	public void init() {
		// TODO Auto-generated method stub
		
	}

	public String intercept(ActionInvocation invocation) throws Exception {
		// TODO Auto-generated method stub
		userAction user = (userAction) invocation.getAction();
		String username = user.getUsername();
		if(username.toLowerCase().contains("admin")){
			invocation.invoke();
		}else{
			return "error";
		}
		System.out.println("执行了superCheck");
		return null;
	}

}

验证是否是管理员

package com.inter;

import com.glj.userAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class formCheck implements Interceptor{

	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	public void init() {
		// TODO Auto-generated method stub
		
	}

	public String intercept(ActionInvocation invocation) throws Exception {
		// TODO Auto-generated method stub
		userAction user = (userAction) invocation.getAction();
		String username = user.getUsername();
		String password = user.getPassword();
		if(username==null||password==null||username.equals("")||password.equals("")){
			return "error";
		}else{
			invocation.invoke();
		}
		System.out.println("执行了formCheck");
		return null;
	}
	
}

验证表单

Struts.xml

一旦使用了自定义的拦截器,本来配置在action上的默认的拦截器就失效了。 所以需要再加上defaultStack

<?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="webDemo" extends="struts-default" namespace="/">
		<interceptors>
			<interceptor name="formCheck" class="com.inter.formCheck"></interceptor>
			<interceptor name="superCheck" class="com.inter.superCheck"></interceptor>
		</interceptors>
		<action name="login" class="com.glj.userAction">
			<interceptor-ref name="defaultStack"></interceptor-ref>
			<interceptor-ref name="formCheck"></interceptor-ref>
			<interceptor-ref name="superCheck"></interceptor-ref>
			<result name="ok">/success.jsp</result>
			<result name="no">/fail.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
	</package>
</struts>    

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>
  		org.apache.struts2.dispatcher.FilterDispatcher
  	</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>*.action</url-pattern>
  </filter-mapping></web-app>

375611971363640027

发表评论

您的电子邮箱地址不会被公开。