您的位置:首页 > 编程语言 > Java开发

struts2拦截器简单例子

2016-04-16 22:17 411 查看
自定义拦截器实现步骤:

1、实现Interceptor接口

在需要配置拦截器参数的情况下使用处理拦截的方法:
public String intercept(ActionInvocation invocation) throws Exception {
}

invocation.invoke():
1-1、只要能获取这些代码,则说明拦截器验证通过,接着访问action类中对应的方法执行后续操作

1-2、没有执行这行代码:不会去调action中的方法,而是转向struts.xml文件去搜索拦截器中返回的字符标志

2、在struts.xml文件中对自定义拦截器进行配置
2-1、使用<interceptors> <interceptor name="自定义拦截器的名称"></interceptor>  ...  </interceptors>来加载自定义的拦截器

2-2、引用配置好的拦截到对应的action中  <interceptor-ref name="自定义拦截器的名称"/>

例子:

修改web.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

  <display-name>struts11</display-name>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

   <!-- 过滤器 -->

   <filter>

    <!-- 过滤器核心类 -->

    <filter-name>struts2</filter-name>

    <!-- struts2的核心类 -->

    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

   </filter>

   

   <!-- 映射 -->

   <filter-mapping>

    <filter-name>struts2</filter-name>

    <!-- 过滤URL -->

    <url-pattern>/*</url-pattern>

   </filter-mapping>

</web-app>

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>
<package name="default" namespace="/" extends="struts-default">
<!-- 配置拦截器 -->
<interceptors>
<interceptor name="myIntercepter" class="com.caokaiyuan.intercepter.MyIntercepter"></interceptor>
</interceptors>
<!-- 登录 -->
<action name="loginAction" class="com.caokaiyuan.action.UserAction" method="login">
<result name="login">success.jsp</result>
</action>

<!-- 用户管理 -->
<action name="userAction" class="com.caokaiyuan.action.UserAction" method="user">
<!-- 引用拦截器 -->
<interceptor-ref name="myIntercepter"></interceptor-ref>
<result name="login">login.jsp</result>
<result name="user">user.jsp</result>
</action>

<!-- 客户管理 -->
<action name="clientAction" class="com.caokaiyuan.action.UserAction" method="client">
<!-- 引用拦截器 -->
<interceptor-ref name="myIntercepter"></interceptor-ref>
<!-- 配置拦截器不放行是返回字符串与 -->
<result name="login">login.jsp</result>
<result name="client">client.jsp</result>
</action>
</package>
</struts>

创建UserVO类

package com.caokaiyuan.vo;

import java.io.Serializable;

public class UserVO implements Serializable

{
private static final long serialVersionUID = 1L;

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;
}

}

创建action类UserAction

package com.caokaiyuan.action;

import org.apache.struts2.ServletActionContext;

import com.caokaiyuan.vo.UserVO;

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<UserVO>

{
private static final long serialVersionUID = 1L;

private UserVO user = new UserVO();

//登录
public String login() throws Exception
{
System.out.println(user);
ServletActionContext.getRequest().getSession().setAttribute("user", user);
return "login";
}

//客户管理
public String client() throws Exception
{
return "client";
}

//用户管理
public String user() throws Exception
{
return "user";
}

public UserVO getUser()
{
return user;
}

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

@Override
public UserVO getModel()
{
return user;
}

}

创建拦截器类MyIntercepter

package com.caokaiyuan.intercepter;

import com.caokaiyuan.vo.UserVO;

import com.opensymphony.xwork2.Action;

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.Interceptor;

public class MyIntercepter implements Interceptor

{
private static final long serialVersionUID = 1L;

@Override
public void destroy() 
{

}

@Override
public void init()
{
System.out.println("==========MyIntercepter==========");
}

@Override
public String intercept(ActionInvocation invocation) throws Exception 
{
UserVO user = (UserVO)invocation.getInvocationContext().getSession().get("user");

if (user != null)
{
System.out.println("拦截器放行!!");
return invocation.invoke();
}
else 
{
System.out.println("拦截器不放行!!");
return Action.LOGIN;
}
}

}

创建index.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!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>Insert title here</title>

</head>

<body>
<p><a href="login.jsp">用户登录</a></p>
<p><a href="userAction">用户管理</a></p>
<p><a href="clientAction">客户管理</a></p>

</body>

</html>

创建login.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!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>Insert title here</title>

</head>

<body>
<form action="loginAction" method="post">
<input type="text" name="usrename"/><br/>
<input type="password" name="password"/><br/>
<input type="submit" value="登录">
</form>

</body>

</html>

创建client.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!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>Insert title here</title>

</head>

<body>
<h1>用户管理模块</h1>

</body>

</html>

创建user.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!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>Insert title here</title>

</head>

<body>
<h1>用户管理模块</h1>

</body>

</html>

创建success.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!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>Insert title here</title>

</head>

<body>
登陆成功!!

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struts