以前網站作的基本差不錯了,客戶有其餘的活動需求,考慮能夠用struts2的攔截器實現html
攔截器代碼:java
package org.main.manage.interceptor.temp; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import org.main.common.util.PropertyUtil; import org.main.manage.dict.OrderDict; import org.main.manage.entity.TSecond; import org.main.manage.entity.TUser; import org.main.manage.service.OrderService; import org.main.manage.service.SecondService; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class TaiAnCBCSecTempInterceptor implements Interceptor { private static final long serialVersionUID = -2578561479301489061L; private SecondService secondService; private OrderService orderService; public OrderService getOrderService() { return orderService; } public void setOrderService(OrderService orderService) { this.orderService = orderService; } public SecondService getSecondService() { return secondService; } public void setSecondService(SecondService secondService) { this.secondService = secondService; } public void destroy() { } public void init() { System.out.println("攔截器初始化完畢"); } public String intercept(ActionInvocation invocation) throws Exception { ActionContext ctx = invocation.getInvocationContext(); Map session = ctx.getSession(); TUser user = (TUser)session.get("user"); if(null == user){ return "login"; } HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST); String secNo = request.getParameter("secNo"); if(不符合邏輯){ //提示頁面 return "no_role_taian"; } //執行action return invocation.invoke(); } }
applicationContext.xml文件中引入攔截器配置文件
apache
<import resource="interceptorContext.xml"/>
interceptorContext.xml文件內容:
session
<!-- 活動Struts攔截器struts過濾器測試 -->
<bean id="taiAnCBCSecTempInterceptor" class="org.main.manage.interceptor.temp.TaiAnCBCSecTempInterceptor">
調用service注入配置
<property name="orderService" ref="orderService"></property>
<property name="secondService" ref="secondService"></property>
</bean>
在second.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="second" extends="default"> <interceptors> <interceptor name="taiAnCBCSecTempInterceptor" class="taiAnCBCSecTempInterceptor" /> <interceptor-stack name="cbcSecTempInterceptorStack"> <interceptor-ref name="defaultStack"/> <interceptor-ref name="params"/> <!-- 活動Struts攔截器struts過濾器測試 --> <interceptor-ref name="taiAnCBCSecTempInterceptor" /> </interceptor-stack> </interceptors> <!--//// WEB \\\\--> <action name="show_second" class="secondWeb" method="show_second"> <interceptor-ref name="cbcSecTempInterceptorStack"></interceptor-ref> <result name="no_role" type="redirect">/no_role.jsp</result> <result name="no_role_taian" type="redirect">/no_role_taian.jsp</result> //${secNo}接受後臺傳過來的值 <result name="login" type="redirect">/login_user.action?url=show_second.action?secNo=${secNo}</result> </action> <!--\\\\ WEB ////--> </package> </struts>