struts2 intercepter简单例子

本文介绍了一个简单的Struts2应用程序实例,通过自定义拦截器实现了用户权限验证。该实例包括了注册页面、登录页面、登录操作类及自定义拦截器的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://weizhilizhiwei.iteye.com/blog/1005210

玩了一天终于把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> 密&nbsp;&nbsp;码: </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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值