如果使用Struts2 -bean-validation-plugin验证失败,Struts2保持相同的动作url.&l



我使用Struts 2,我有两个动作,一个将显示注册表单,第二个将注册用户。

我想实现的是,而提交表单,如果任何验证失败,那么用户将被重定向到前一个页面与错误的详细信息。

我使用struts2-bean-validation-plugin来验证用户bean。

我的配置文件如下

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> 

Action类是

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;
}
}

实际上我需要一些配置更改struts.xml文件,你可以看到我已经创建了两个包,因为我不需要在每个动作验证,即显示注册表单不需要任何验证,其中提交注册表单需要。所以我输入uuserRegisterstruts-bean-validation包中。当userRegister(提交)动作返回输入结果由于验证失败,我们将重定向userRegisterForm来显示注册表单但是此时struts会创建新的请求对象这样我们就会丢失错误结果因此我们使用struts的默认拦截器store操作模式STORE将错误结果存储在会话中userRegisterForm再次使用storeRETRIEVE这样struts就可以从session中获取错误结果。

注意这里你可以看到我使用beanValidationDefaultStack中提到的拦截器struts2-bean-validation-plugin.jarstruts-plugin.xml文件。因为我假设如果我们使用拦截器,那么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> ```

相关内容

  • 没有找到相关文章

最新更新