javaxmlstruts2struts-validationstruts2-interceptors

Interceptors not allowing field error to be displayed in struts2


Here I have included one interceptor class for login purposes .but after mentioning it in the struts.xml ,field errors are not visible on my struts form when i click on register button on my registration page.
Below is my struts.xml. please correct where i should put that LoginInterceptors class of interceptors. Below is my struts.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>
    <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>

Below is my LoginInterceptors class.

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();

    }

}

As interceptors is always executed first,form field errors are not visible and also not redirected to intended page. Output is "first inter" followed by remaining testing lines.


Solution

  • First, remove the login interceptor from the interceptor stack of the actual login action. It prevents the action from being executed since you are not logged in yet.

    Second, an interceptor shouldn't redirect directly. It creates a new request, which also means that validation errors are lost. Let it return a result value.

    @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();
    }
    

    Then you can create a <global-results> entry for it. Which should (normally) not be a redirect.

    <struts>
        <package name="myPack" extends="struts-default">
    
             <global-results>
                 <result name="login">BloodBankSection.jsp</result>
             </global-results>
    
    </struts>