java 如何告诉 Struts2 在第一次显示表单时不要验证它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4069601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I tell Struts2 not to validate a form the first time it displays?
提问by Brian Kessler
I'm currently trying to learn Struts2.
我目前正在尝试学习 Struts2。
I've created a form, an action to process it, an XML to validate it, and actions in the struts.xml.
我已经创建了一个表单、一个处理它的操作、一个用于验证它的 XML 以及 struts.xml 中的操作。
Every time the form displays, even the first time, Struts2 tries to validate, so errors are displayed before the user had a chance to complete it.
每次表单显示时,即使是第一次,Struts2 都会尝试验证,因此在用户有机会完成之前显示错误。
Here is the relevant code:
这是相关的代码:
<!-- /WebContent/views/user/login.jsp -->
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
<s:head />
</head>
<body>
<h1>Login Page</h1>
<s:form action="executeUser">
<s:textfield key="userBean.userName" />
<s:password key="userBean.password" />
<s:submit align="center" />
</s:form>
</body>
</html>
<!-- /src/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" />
<package name="overviewofstruts" extends="struts-default">
<action name="loginUser" class="hu.flux.user.LoginUserAction" method="execute">
<result name="input">/views/user/login.jsp</result>
</action>
<action name="executeUser" class="hu.flux.user.LoginUserAction" method="execute">
<result name="input">/views/user/login.jsp</result>
<result name="success">/views/user/login_thankyou.jsp</result>
</action>
</package>
</struts>
// /src/hu/flux/user/LoginUserAction.java
package hu.flux.user;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginUserAction extends ActionSupport {
private User userBean;
public void setUserBean(User userBean) { this.userBean = userBean; }
public User getUserBean() { return userBean; }
public String login() throws Exception { return this.execute(); }
public String execute() throws Exception { return SUCCESS; }
public String input() throws Exception { return INPUT; }
}
<!-- // /src/hu/flux/user/LoginUserAction-validation.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<validator type="requiredstring">
<param name="fieldname">userBean.userName</param>
<message>Username is required.</message>
</validator>
<validator type="requiredstring">
<param name="fieldname">userBean.password</param>
<message>Password is required.</message>
</validator>
What do I need to do or change to get struts to show the form the first time without complaining about all the blank fields?
我需要做什么或更改才能让 struts 第一次显示表单而不抱怨所有空白字段?
回答by Dewfy
Yee, I know this issue. Usually I'm using following work-around.
是的,我知道这个问题。通常我使用以下解决方法。
Mark execute
with org.apache.struts2.interceptor.validation.SkipValidation
标记execute
为org.apache.struts2.interceptor.validation.SkipValidation
@SkipValidation
public String execute() throws Exception { return SUCCESS; }
So first pass will ignore validation method. But inputwill be validated.
所以第一遍将忽略验证方法。但输入将被验证。
回答by Rob Whelan
The @SkipValidation workaround will do it, but Struts validation already has built-in rules about when it will run (or not) -- it's better to learn the rules so you don't need the extra configuration. It's also worth learning so you aren't confused when validation doesn'trun when you need it...
@SkipValidation 解决方法会做到这一点,但 Struts 验证已经有关于它何时运行(或不运行)的内置规则——最好学习这些规则,这样你就不需要额外的配置。这也值得学习,因此当您需要验证时不会运行时,您不会感到困惑......
So, short answer: if you change this
所以,简短的回答:如果你改变这个
<action name="loginUser" class="hu.flux.user.LoginUserAction" method="execute">
to this
对此
<action name="loginUser" class="hu.flux.user.LoginUserAction" method="input">
(notice the method parameter) -- that'll fix the problem (implement the method in your action class as well).
(注意方法参数)——这将解决问题(也在你的操作类中实现该方法)。
Long answer: Open struts-default.xml, at the root of the struts-core JAR file and browse around. Validation is handled by the "validation" interceptor. Then there's another interceptor called "workflow" that handles automatically showing the "input" result if validation fails, so look at these together.
长答案:打开 struts-default.xml,在 struts-core JAR 文件的根目录并浏览。验证由“验证”拦截器处理。然后还有另一个称为“工作流”的拦截器,如果验证失败,它会自动处理显示“输入”结果,所以一起看看这些。
Find and you'll see this:
找到你会看到这个:
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
The excludeMethods refers to the action method parameter, and is for exactly what you're trying to do.
excludeMethods 指的是 action 方法参数,正是您要执行的操作。
You can also set up your own interceptor stack (modeled on the default, or one of the other examples) and define other excluded methods. Wildcards are supported in the names.
您还可以设置自己的拦截器堆栈(以默认方式或其他示例之一为模型)并定义其他排除方法。名称中支持通配符。