struts2 拦截器
来源:互联网 发布:北京赛车pk10平台源码 编辑:程序博客网 时间:2024/06/05 11:28
执行的流程 提交请求到服务器端,请求会先经过Struts2的核心过滤器(StrutsPrepareAndExecuteFilter)。 过滤器的功能是完成了一部分代码功能。 就是执行了一系列的拦截器,进行一些处理工作。 可以在struts-default.xml配置文件中看到有很多的拦截器。 拦截器执行完后,会根据struts.xml的配置文件找到请求路径,找到具体的类,通过反射的方式让方法执行。总结 JSP页面-->StrutsPrepereAndExecuteFilter过滤器-->执行一系列拦截器(完成了部分代码)-->执行到目标Action-->返回字符串-->结果页面(result)-->页面跳转
<package cn.happy.Interceptor;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;import com.opensymphony.xwork2.interceptor.Interceptor;import java.util.Map;/** * Created by WuCunJian on 2017/10/25. */public class MyTimerInterceptor implements Interceptor { public void destroy() { } public void init() { System.out.println("拦截器已经成功初始化........"); } public String intercept(ActionInvocation invocation) throws Exception { System.out.println("对象"+invocation); //Action是谁 Object action = invocation.getAction(); System.out.println("action============="+action); String value; //判定session里面key值对应的value是否存在 Map<String, Object> session = ActionContext.getContext().getSession(); Object name = session.get("uname"); String actionName = invocation.getProxy().getActionName(); invocation.getProxy().getNamespace();//user System.out.println("actionName======"+actionName); if (actionName.equals("login")) { System.out.println("================================Action是Login"); //user/login.action value= invocation.invoke(); }else if(name!=null) { //已经登录了,,放行 value= invocation.invoke(); String method = invocation.getProxy().getMethod(); System.out.println("方法:"+method); }else{ //走到这个else,证明你访问的Action,既不是登录,也不是被授权访问的操作 value="login"; } System.out.println("逻辑视图名"+value); return value; }}UserInfo的持久层package cn.happy.entity;/** * Created by Happy on 2017-10-24. */public class UserInfo { 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; }}web.xml<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app> <display-name>Archetype Created Web Application</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>
?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.3.dtd"><struts> <constant name="struts.devMode" value="true"></constant><package name="default" namespace="/" extends="struts-default"> <interceptors> <interceptor name="myTimer" class="cn.happy.Interceptor.MyTimerInterceptor"></interceptor> <interceptor-stack name="myStack"> <interceptor-ref name="myTimer"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack></interceptors> <default-interceptor-ref name="myStack"></default-interceptor-ref> <default-action-ref name="login"></default-action-ref> <action name="login" class="cn.happy.action.MyTimerAction"> <result name="success">/success.jsp</result> <result name="login">/login.jsp</result> </action></package></struts>
login.jsp<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib prefix="s" uri="/struts-tags" %><html><head> <title>Title</title></head><body><div align="center"> <s:form action="login" method="POST"> 账号:<s:textfield name="user.username"></s:textfield><br/> 密码:<s:password name="user.password"></s:password> <s:submit value="登陆"></s:submit> </s:form></div></body></html>
阅读全文
0 0
- 【Struts2】Struts2中拦截器
- struts2拦截器
- Struts2拦截器
- struts2.0拦截器
- struts2.0 拦截器
- struts2拦截器学习
- struts2 拦截器
- Struts2拦截器(Interceptor)
- struts2.0拦截器
- 用上struts2拦截器
- struts2拦截器简介
- Struts2:拦截器概述
- Struts2拦截器(Interceptor)
- struts2 拦截器
- 关于struts2拦截器
- Struts2的拦截器
- struts2拦截器
- Struts2拦截器总结
- Nutch2.2.1的配置(使用MySQL作为数据存储)
- 【java】IO详解之BIO
- 核函数在机器学习上的
- 改造二叉树(lis+中序遍历)
- Python与机器学习之常用的Numpy操作
- struts2 拦截器
- javascript高级系列之slice()与substring()
- Hibernate Mapping 加载慢,时常连接超时问题
- angular用户列表信息
- 网康上网行为管理NI3200-60
- iOS开发中 ----更换头像图片--代码块
- 特殊回文数
- 【jquery】基础DOM和CSS操作
- Condition的实现分析