拦截器不允许在struts2中显示字段错误

时间:2015-07-03 17:18:37

标签: java xml struts2 struts-validation struts2-interceptors

这里我已经包含了一个用于登录目的的拦截器类。但是在struts.xml中提到它后,当我点击注册页面上的注册按钮时,我的struts表单上看不到字段错误。
下面是我的struts.xml。请更正我应该放置 LoginInterceptors 类拦截器。下面是我的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>

以下是我的 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();

    }

}

由于拦截器总是先执行,因此表单字段错误不可见,也不会重定向到目标页面。输出是“第一个inter”,然后是剩余的测试线。

1 个答案:

答案 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>