简体   繁体   English

如果使用 struts2-bean-validation-plugin 验证失败,Struts2 保持相同的操作 url

[英]Struts2 keep same action url if validation fail using struts2-bean-validation-plugin

I am using Struts 2 and I have two action one will show the register form and second will be register user.我正在使用 Struts 2,我有两个操作,一个是显示注册表单,第二个是注册用户。

I want to achieve is while submitting form if any validation fail then user will be redirect to previous page with errors details.我想要实现的是在提交表单时,如果任何验证失败,那么用户将被重定向到带有错误详细信息的上一页。

I have use struts2-bean-validation-plugin for validating user bean.我使用 struts2-bean-validation-plugin 来验证用户 bean。

My configuration files are as below我的配置文件如下

struts.xml 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.objectFactory" value="spring" />
    
        <package name="default" extends="struts-default">

        <interceptors>
            <interceptor name="beanValidation" class="org.apache.struts.beanvalidation.validation.interceptor.BeanValidationInterceptor" />
            <interceptor-stack name="appDefaultStack">
                <interceptor-ref name="beanValidation"/>
                <interceptor-ref name="defaultStack"/>
            </interceptor-stack>
        </interceptors>

        <action name="userRegisterForm" method="userRegisterForm" class="com.pc.collabtest.actions.UserRegisterAction">
            <result name="success">/userRegisterForm.jsp</result>
        </action>

        <action name="userRegister" method="userRegister" class="com.pc.collabtest.actions.UserRegisterAction" >
             <interceptor-ref name="appDefaultStack"/>
            <result name="userList" type="redirectAction">userList</result>
            <result name="userRegisterForm" type="redirectAction">userRegisterForm</result>
            <result name="input" type="redirectAction">userRegisterForm</result>
        </action>
    </package>
</struts> 

And Action class is而动作类是

package com.pc.collabtest.actions;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;
import com.pc.collabtest.model.User;
import com.pc.collabtest.service.UserService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class UserRegisterAction extends ActionSupport {

    private static final long serialVersionUID = 1L;

    @Valid
    public User user;

    @Autowired
    private UserService userService;    
    
    public String userRegisterForm() throws Exception {
        return "success";
    }

    public String userRegister() throws Exception {
        if (user != null) {
            if(userService.saveUser(user) != null) {
                return "userList";
            }
        }
        return "userRegisterForm";
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

}

Actually I need some configuration changes in struts.xml file you can see I have create two package as I don't need validation in every action ie showing registration form doesn't need any validation where submit registration form needed.实际上,我需要在struts.xml文件中进行一些配置更改,您可以看到我创建了两个包,因为我不需要在每个操作中进行验证,即显示注册表单不需要在需要提交注册表单的地方进行任何验证。 so I put u userRegister action in struts-bean-validation package.所以我把你的userRegister动作放在struts-bean-validation包中。 and whenever userRegister (submit) action return input result because of validation fail we will redirect userRegisterForm to show registration form but at this point struts will create new request object so we will loose error result so for that we use struts default interceptor store with operation mode STORE which will store error result in session and in userRegisterForm action again we use store interceptor but with operation mode RETRIEVE so that struts will fetch error result from session.并且每当userRegister (submit) action 由于验证失败而返回输入结果时,我们将重定向userRegisterForm以显示注册表单,但此时 struts 将创建新的请求对象,因此我们将丢失错误结果,因此我们使用带有操作模式的 struts 默认拦截器存储STORE将错误结果存储在sessionuserRegisterForm action 中,我们使用store拦截器,但使用操作模式RETRIEVE ,这样 struts 将从 session 中获取错误结果。

Note here you can see I use beanValidationDefaultStack interceptor which is mention in struts2-bean-validation-plugin.jar struts-plugin.xml file.注意这里你可以看到我使用了struts2-bean-validation-plugin.jar struts-plugin.xml 文件中提到的beanValidationDefaultStack拦截器。 because what I assume if we use interceptor then struts will now use default interceptor.因为我假设如果我们使用拦截器,那么 struts 现在将使用默认拦截器。

<?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.objectFactory" value="spring" />

    <package name="default" extends="struts-default">
        <action name="userRegisterForm" method="userRegisterForm" class="com.pc.collabtest.actions.UserRegisterAction">
            <interceptor-ref name="store"><param name="operationMode">RETRIEVE</param></interceptor-ref>
            <result name="success">/userRegisterForm.jsp</result>
        </action>
    </package>

    <package name="my-bean-validation" extends="struts-bean-validation">
        <action name="userRegister" method="userRegister" class="com.pc.collabtest.actions.UserRegisterAction">
            <interceptor-ref name="store"><param name="operationMode">STORE</param></interceptor-ref>
            <interceptor-ref name="beanValidationDefaultStack"></interceptor-ref>
            <result name="userList" type="redirectAction">userList</result>
            <result name="userRegisterForm" type="redirectAction">userRegisterForm</result>
            <result name="input" type="redirectAction">userRegisterForm</result>
        </action>
    </package>
</struts> ```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM