玩了一天终于把struts2的intercepter搞明白了,有那么点小兴奋,呵呵。
下面把俺的成果分享一下:
1.在注册页面中(index.jsp)注册一下:
<% session.setAttribute("user","lzw"); %>
2.在登录界面(login.jsp)登录:
<form action="login.action" method="post"> <table align="center"> <tr> <td> 用户名: </td> <td> <input type="text" name="userName" /> </td> </tr> <tr> <td> 密 码: </td> <td> <input type="password" name="password" /> </td> </tr> <tr align="center"> <td colspan="2"> <input type="submit" value="登录" /> </td> </tr> </table> </form>
3.登录类:LoginAction.java
package com.lzw.action; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction 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("开始执行execute()"); Map<String, Object> map = ActionContext.getContext().getSession(); String user = (String) map.get("user"); System.out.println("The user'name is ---------- " + user); return SUCCESS; } }
4.success.jsp页面
welcome ${userName} to struts2 world。
5.拦截器类:LoginInterceper.java
package com.lzw.intercepter; import java.util.Map; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class LoginIntercepter extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { Map<String, Object> session = invocation.getInvocationContext() .getSession(); String user = (String) session.get("user"); System.out.println("11---------------- " + user); if ("".equals(user) || null == user) { System.out.println("22---------------- "); return Action.INPUT; } else { System.out.println(" 33---------------- " + user); return invocation.invoke(); } } }
6.struts2配置文件struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="struts" extends="struts-default"> <interceptors> <interceptor name="authority" class="com.lzw.intercepter.LoginIntercepter" /> <interceptor-stack name="loginStack"> <interceptor-ref name="authority" /> <interceptor-ref name="defaultStack" /> </interceptor-stack> </interceptors> <default-interceptor-ref name="loginStack" /> <action name="login" class="com.lzw.action.LoginAction"> <result>/success.jsp</result> <result name="input" type="redirect">/login.jsp</result> </action> </package> </struts>7.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>index.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>/*</url-pattern> </filter-mapping> </web-app>
8.部署到tomcat服务器上,在地址栏上输入:http://localhsot:8080/test来注册,
再重新输入:http://localhost:8080/test/login.jsp
相关推荐
Interceptor拦截器详解和部分源码的指导,方便深入学习struts2,增加理解
十二、总结 本教程对struts2的基本知识进行了一些说明,关于struts2的更多详细内容应参看struts2的官方文档及提供的app实例。 下面对struts2的基本执行流程作一简要说明,此流程说明可以结合官方提供的struts2结构图...
Struts 2的基石——拦截器(Interceptor);在Struts 2_0中国际化(i18n)您的应用程序;在Struts 2_0中实现表单数据校验(Validation);Struts 2与AJAX;在Struts 2中实现IoC......
拦截器是一种AOP(面向切面编程)思想的编程方式.它提供一种机制是开发者能够把相对独立的代码抽离出来,...下面这篇文章主要给大家介绍了关于Struts2拦截器Interceptor的原理与配置的相关资料,需要的朋友可以参考下。
利用Struts2的核心技术----拦截器实现权限控制。详细代码详解
拦截器是AOP中的概念,它本身是一段代码,可以通过定义“织入点”,来...正如上面 Struts2的Reference中讲述的,Struts2的Interceptor,其拦截的对象是Action代码,可以定义在Action代码之前或者之后执行拦截器的代码。
118 8.3.5 Struts2配置文件 119 8.4 Action的配置方式 121 8.4.1 动态方法调用 121 8.4.2 设置action元素的method属性 122 8.4.3 使用通配符配置action 122 8.4.4 默认action 123 8.5 拦截器Interceptor 123 8.5.1 ...
9.2 利用拦截器(Interceptor)生成审计日志 9.3 Hibernate的事件处理机制 9.4 批量处理数据 9.4.1 通过Session来进行批量操作 9.4.2 通过StatelessSession来进行批量操作 9.4.3 通过HQL来进行批量操作 ...
9.2 利用拦截器(Interceptor)生成审计日志 9.3 Hibernate的事件处理机制 9.4 批量处理数据 9.4.1 通过Session来进行批量操作 9.4.2 通过StatelessSession来进行批量操作 9.4.3 通过HQL来进行批量操作 ...
9.2 利用拦截器(Interceptor)生成审计日志 9.3 Hibernate的事件处理机制 9.4 批量处理数据 9.4.1 通过Session来进行批量操作 9.4.2 通过StatelessSession来进行批量操作 9.4.3 通过HQL来进行批量操作 ...
9.2 利用拦截器(Interceptor)生成审计日志 9.3 Hibernate的事件处理机制 9.4 批量处理数据 9.4.1 通过Session来进行批量操作 9.4.2 通过StatelessSession来进行批量操作 9.4.3 通过HQL来进行批量操作 ...
• Struts • Webwork • Tapestry • Spring MVC 以上框架都是非常优秀的。说实话,如果阿里巴巴网站在2001年开始,就有这么多可选择的话,无论选择哪一个都不会有问题。因为这些年来,所有的开源Web框架都在...