Struts2拦截器Interceptor的使用

一、拦截器的使用:

只有用户登录时才可以查看商品,否则点击查看商品时会跳回login登录页面。

struts2.xml中拦截器Interceptor的配置:

<interceptors>
			<!-- 声明自定义的拦截器 -->
			<interceptor name="myInterceptor" class="Interceptor.MyInterceptor"></interceptor>
			<!-- 声明拦截器栈,可以存放多个拦截器,这里存入自定义拦截器外加默认拦截器 -->
			<interceptor-stack name="myInterceptorStack">
				<interceptor-ref name="myInterceptor"></interceptor-ref>
				<interceptor-ref name="defaultStack"></interceptor-ref>
			</interceptor-stack>
		</interceptors>

Action中对应方法的配置

<action name="product_show" class="Action.ProductAction" method="productShow" >
			<result name="success">/product_show.jsp</result>
			
			<result name="login">/login.jsp</result>
			在action中引用拦截器
			<interceptor-ref name="myInterceptorStack"></interceptor-ref>
		</action>

LoginAction

//利用getModel模型获取数据
		if("tom".equals(user.getUsername())&&"123".equals(user.getPassword())) {
		//将用户存入session
			request.getSession().setAttribute("user", user);
			return "success";
		} else {
			return "fail";
		}

MyInterceptor

	public class MyInterceptor implements Interceptor {

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

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

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		User user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
		if(user != null) {
			//用户已经登录,直接放行
			return invocation.invoke();
		} else {
			ProductAction productAction = (ProductAction) invocation.getAction();
			productAction.addActionError("权限不足");
			//没有登录,跳到登录界面
			//要在showProduct这个Action中配置名称为login的result视图
			return "login";
			
		}
	}

}

login.jsp

<body>
  		<!-- 可以显示vs.set("msg", "用户名或密码错误");的错误信息 -->
  		<s:property value="msg"/>
  		<!--可以显示productAction.addActionError("权限不足");的错误信息  -->
  		<s:actionerror/>
  		<hr>
    	<form action="${pageContext.request.contextPath }/login.action" method="post">
    		username:<input type="text" name="username"><br>
    		password:<input type="password" name="password"><br>
    		<input type="submit" value="登录">
    	</form>
  </body>

success.jsp

<body>
		<a href="${ pageContext.request.contextPath }/product_productShow">查看商品</a>
  </body>

product_show.jsp

 <body>
    	<table border="1">
    		<tr>
    			<td>商品名称</td>
    			<td>商品价格</td>
    			<td>商品数量</td>
    		</tr>
    		<s:iterator value="list">
    			<tr>
    				<td><s:property value="name"/></td>
    				<td><s:property value="price"/></td>
    				<td><s:property value="count"/></td>
    			</tr>
    		
    		</s:iterator>
    	</table>
    	<s:debug></s:debug>
  </body>

二、假设用通配符,Action中有许多方法,只要拦截其中一个时候的处理

假设现在ProductAction中有add()方法和productShow()方法,现在只要拦截productShow()方法。

拦截器配置

<interceptors>
			<!-- 声明自定义的拦截器 -->
			<interceptor name="myInterceptor" class="Interceptor.MyInterceptor">
				<!-- 声明只拦截productShow方法 -->
				<param name="includeMethods">productShow</param>
			</interceptor>
			<!-- 声明拦截器栈,可以存放多个拦截器,这里存入自定义拦截器外加默认拦截器 -->
			<interceptor-stack name="myInterceptorStack">
				<interceptor-ref name="myInterceptor"></interceptor-ref>
				<interceptor-ref name="defaultStack"></interceptor-ref>
			</interceptor-stack>
		</interceptors>

对应Action的配置

<action name="product_*" class="Action.ProductAction" method="{1}" >
			<result name="success">/product_show.jsp</result>
			<result name="login">/login.jsp</result>
			<!-- 在action中引用拦截器 -->
			<interceptor-ref name="myInterceptorStack"></interceptor-ref>
		</action>
这是success.jsp界面,product_* 对应下面的product_productShow, *对应productShow,即Action中的productShow方法。
<body>
		<a href="${ pageContext.request.contextPath }/product_productShow">查看商品</a>
  </body>

Interceptor(这边是继承MethodFilterInterceptor类,上边是实现Interceptor接口)

public class MyInterceptor extends MethodFilterInterceptor {

	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		System.out.println("Interceptor执行拦截...");
		User user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
		if(user != null) {
			//用户已经登录,直接放行
			return invocation.invoke();
		} else {
			ProductAction productAction = (ProductAction) invocation.getAction();
			productAction.addActionError("权限不足");
			//没有登录,跳到登录界面
			return "login";
			
		}
	}
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值