2015-07-03 46 views
0

這裏我包含了一個用於登錄目的的攔截器類,但是在struts.xml中提到它之後,當我點擊註冊按鈕時,字段錯誤在我的struts表單上不可見我的註冊頁面。
以下是我的struts.xml。請更正我應該放在哪裏LoginInceptors攔截器類。下面是我在struts.xml攔截器不允許在struts2中顯示字段錯誤

<?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> 
    <constant name="struts.devMode" value="true" /> 
    <constant name="struts.enable.DynamicMethodInvocation" value="true" /> 
    <package name="myPack" extends="struts-default"> 
     <interceptors> 
      <interceptor name="cache" class="org.entity.CacheInterceptor" /> 
      <interceptor name="cacheone" class="org.entity.LoginInterceptors"></interceptor> 
      <interceptor-stack name="cacheStack"> 
       <interceptor-ref name="cache" /> 
       <interceptor-ref name="cacheone"></interceptor-ref> 
       <interceptor-ref name="defaultStack" /> 
      </interceptor-stack> 
      </interceptors> 
     <default-interceptor-ref name="cacheStack" /> 
     <action name="registration" class="org.entity.RegistrationAction"> 
      <result name="input">BloodBankSection.jsp</result> 
      <result name="success">success.jsp</result> 
      <result name="error">BloodCampError.jsp</result> 
     </action> 
     <action name="AdminAction" class="org.entity.AdminAction"> 
      <result name="input">Admin.jsp</result> 
      <result name="success">AdminLoginSucess.jsp</result> 
      <result name="error">Admin.jsp</result> 
     </action> 
     <action name="feedback" class="org.entity.FeedbackAction"> 
      <result name="input">Feedback.jsp</result> 
      <result name="success">/feedbacksuccess.jsp</result> 
      <result name="error">/Feedback.jsp</result> 
     </action> 
     <action name="UpdateStockAction" class="org.entity.UpdateStockAction"> 
      <interceptor-ref name="cacheone"></interceptor-ref> 
      <result name="input">UpdateStock.jsp</result> 
      <result name="success">updatesuccess.jsp</result> 
      <result name="error">UpdateStock.jsp</result> 
     </action> 
     <action name="LoginAction" class="org.entity.LoginAction"> 
      <result name="input">BloodBankSection.jsp</result> 
      <result name="success">UpdateStock.jsp</result> 
      <result name="error">bloodloginerror.jsp</result> 
     </action> 
     <action name="delete" class="org.entity.DeleteAction"> 
      <result name="success">AdminLoginSucess.jsp</result> 
      <result name="error">BloodBankSection.jsp</result> 
     </action> 
     <action name="DonorAction" class="org.entity.DonorAction"> 
      <result name="input">DonorSection.jsp</result> 
      <result name="success">dsuccess.jsp</result> 
      <result name="error">DonorSection.jsp</result> 
     </action> 
     <action name="DonorLoginAction" class="org.entity.DonorLoginAction"> 
      <result name="input">DonorSection.jsp</result> 
      <result name="success">DonorLoginSuccess.jsp</result> 
      <result name="error">donorloginerror.jsp</result> 
     </action> 
     <action name="SearchOneAction" class="org.entity.SearchOneAction"> 
      <result name="input">DonorLoginSuccess.jsp</result> 
      <result name="success">DonorLoginSuccess.jsp</result> 
      <result name="error">errorsearch.jsp</result> 
     </action> 
     <action name="SearchTwoAction" class="org.entity.SearchTwoAction"> 
      <result name="input">DonorLoginSuccess.jsp</result> 
      <result name="success">DonorLoginSuccess.jsp</result> 
      <result name="error">errorsearch.jsp</result> 
     </action> 
     <action name="SearchThreeAction" class="org.entity.SearchThreeAction"> 
      <result name="input">DonorLoginSuccess.jsp</result> 
      <result name="success">DonorLoginSuccess.jsp</result> 
      <result name="error">errorsearch.jsp</result> 
     </action> 

     <action name="DonorInfoAction" class="org.entity.DonorInfoForBloodBank"> 
      <result name="input">donorbloodsection.jsp</result> 
      <result name="success">donorbloodsection.jsp</result> 
      <result name="error">feedbacksuccess.jsp</result> 
     </action> 
     <action name="FPA" class="org.entity.ForgotAction"> 
      <result name="input">ForgotPassword.jsp</result> 
      <result name="success">ForgotPasswordSuccess.jsp</result> 
      <result name="error">errorpassword.jsp</result> 
     </action> 
     <action name="DonorForgotAction" class="org.entity.DonorForgotAction"> 
      <result name="input">DonorForgot.jsp</result> 
      <result name="success">DonorForgotSuccess.jsp</result> 
      <result name="error">errorpassword2.jsp</result> 
     </action> 

     <action name="BloodCampAction" class="org.entity.BloodCampAction"> 
      <result name="input">BloodCampInfoReg.jsp</result> 
      <result name="success">BloodCampSuccess.jsp</result> 
      <result name="error">BloodCampError.jsp</result> 
     </action> 

     <action name="BloodBankEditAction" class="org.entity.BloodBankEditAction"> 
      <result name="input">BloodUpdateProfile.jsp</result> 
      <result name="success">UpdateStock.jsp</result> 
      <result name="error">BloodUpdateProfile.jsp</result> 
     </action> 
     <action name="DonorEditAction" class="org.entity.DonorEditAction"> 
      <result name="input">DonorUpdateProfile.jsp</result> 
      <result name="success">DonorLoginSuccess.jsp</result> 
      <result name="error">DonorUpdateProfile.jsp</result> 
     </action> 
     <action name="logout" class="org.entity.LoginAction" method="logout"> 
      <result type="redirect">BloodBankSection.jsp</result> 
     </action> 
     <action name="logout2" class="org.entity.DonorLoginAction" method="logout"> 
      <result type="redirect">DonorSection.jsp</result> 
     </action>  
     <action name="LastDonatedDateAction" class="org.entity.LastDonatedDateAction"> 
      <result name="input">DonorLoginSuccess.jsp</result> 
      <result name="success">LastDonatedSuccess.jsp</result> 
      <result name="error">LastDonatedError.jsp</result> 
     </action> 

    </package> 
</struts> 

下面是我LoginInterceptors類。

package org.entity; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 
import org.apache.struts2.ServletActionContext; 
import com.opensymphony.xwork2.ActionInvocation; 
import com.opensymphony.xwork2.interceptor.Interceptor; 

public class LoginInterceptors 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 action) throws Exception { 
     HttpSession session=ServletActionContext.getRequest().getSession(false); 
     if(session==null || session.getAttribute("login")==null) 
     { 
      ServletActionContext.getResponse().sendRedirect("BloodBankSection.jsp"); 
      System.out.println("First Inter"); 
     } 

     return action.invoke(); 

    } 

} 

由於攔截總是先執行,表單字段錯誤是不可見的,也不會被重定向到預期的頁面。輸出是「第一次間隔」,然後是剩餘的測試線。

+0

什麼是實際問題? –

+0

我的方案是:當我點擊註冊按鈕時,(空白)表單被處理,但它沒有給出任何字段錯誤,用戶無法獲得註冊。validate方法被執行,但是因爲攔截器它不會顯示在struts表單上。 –

+0

由於攔截器而隱藏了現場錯誤 –

回答

1

首先,從實際登錄操作的攔截器堆棧中刪除登錄攔截器。由於您尚未登錄,因此它會阻止執行操作。

其次,攔截器不應該直接重定向。它會創建一個新的請求,這也意味着驗證錯誤會丟失。讓它返回一個結果值。

@Override 
public String intercept(ActionInvocation action) throws Exception { 
    HttpSession session=ServletActionContext.getRequest().getSession(false); 
    if(session==null || session.getAttribute("login")==null) 
    { 
     System.out.println("First Inter"); 
     return "login"; 
    } 
    return action.invoke(); 
} 

然後,您可以爲它創建一個<global-results>條目。哪(通常)不應該是重定向。

<struts> 
    <package name="myPack" extends="struts-default"> 

     <global-results> 
      <result name="login">BloodBankSection.jsp</result> 
     </global-results> 

</struts> 
+0

非常感謝他的工作! :) –