Struts2 struts 2字段错误值未显示
我使用重写验证方法并添加了错误Struts2 struts 2字段错误值未显示,struts2,struts-validation,Struts2,Struts Validation,我使用重写验证方法并添加了错误 addFieldError(“测试”、“测试打印”) 并在jsp中使用 但这些错误不会显示在input.jsp中 我的jsp内容类型也是 我的struts.xml类似于 <action name="test" class="ListInfo"> <result>/input.jsp</result> </action> &
addFieldError(“测试”、“测试打印”)代码>
并在jsp中使用
但这些错误不会显示在input.jsp中
我的jsp内容类型也是
我的struts.xml类似于
<action name="test" class="ListInfo">
<result>/input.jsp</result>
</action>
<action name="Proceed" class="Details">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="execAndWait">
<param name="delay">100</param>
</interceptor-ref>
<result name="wait">Wait.jsp</result>
<result name="success">/Summary.jsp</result>
<result name="input" type="chain">test</result>
<result name="failure" type="chain">test</result>
</action>
/input.jsp
100
Wait.jsp
/Summary.jsp
测试
测试
结果表明,错误(字段和操作)不会跨链维护
以下证明了这一点(假设struts2约定插件版本):
Action foo始终链接到Action bar(因此我们只需要Action bar的视图)
行动食品
package com.quaternion.action;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Result;
/** #1 SOMETHING WILL BE ADDED HERE TO FIX THE ISSUE**/
@Result(name="input", type="chain", location="bar")
public class Foo extends ActionSupport{
private String name;
@Override
public void validate(){
super.addActionError("Just an action error");
super.addFieldError("name", "Name is all ways wrong... for no good reason.");
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
操作栏
package com.quaternion.action;
import com.opensymphony.xwork2.ActionSupport;
/** #2 SOMETHING WILL BE ADDED HERE TO FIX THE ISSUE**/
public class Bar extends ActionSupport{
}
条形图视图:/WEB-INF/content/bar.jsp
<%@taglib prefix="s" uri="/struts-tags"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<h1>Action Bar</h1>
<s:actionerror/>
<s:fielderror name="name"/>
</body>
</html>
在第二个操作(#2)中,我们需要添加注释和导入以支持它们:
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
@InterceptorRefs({
@InterceptorRef(value = "store", params = {"operationMode","STORE"}),
@InterceptorRef("defaultStack"),
})
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
@InterceptorRefs({
@InterceptorRef(value = "store", params = {"operationMode","RETRIEVE"}),
@InterceptorRef("defaultStack"),
})
现在它可以工作了。如果你打算使用动作链(IMO几乎从不需要,几乎从来都不是一个好主意,而且几乎总是导致动作之间的意外耦合),那么就使用已经提供的,并且避免配置“链”之外的东西
根据文件:
struts.xwork.chaining.copyErrors
-设置为true可复制操作错误
struts.xwork.chaining.copyFieldErrors
-设置为true以复制字段错误
struts.xwork.chaining.copyMessages
-设置为true以复制操作消息
我不记得在默认链接期间是否保留了操作错误。顺便说一句,链接几乎总是不好的。@DaveNewton:好的观点,我完全错过了:)@saket更好的方法,而不是使用链,重定向到测试操作,并使用messageStore拦截器保留操作错误和字段错误。字段错误必须按名称引用。。。您在字段中提供了一个名称,对吗?如果使用chain提供了一个答案,则消息存储区仅在OP需要其他计算值时才会提供消息链(以及…任何重定向方式都需要).HI我将struts.xml更改为“RETRIEVE/input.jsp STORE/Summary.jsp test”,但它重定向正确,但仍然没有显示错误。在validate methodaddFieldError(“Description”)中,“应该打印错误”)代码>在jsp
中运行。。我用一个简单的例子试了一下。。但当我包含header.jsp时,它不起作用。你能提供任何可能出错的提示吗..允许保留文档中详述的字段错误和操作错误/消息。如果OP有链接的习惯。。。然后,链接拦截器的“一刀切”方法可能会带来问题。有一个原因是结转错误不是一般情况,一个动作的验证通常与第二个动作的验证没有多大关系。。。在这种情况下,仍然使用store更为具体。从用户的角度来看,最好这两者在这方面的行为一致。我在应用程序中将struts.xwork.chaining.copyErrors设置为true,然后每当我通过addActionError()添加一些错误时,Struts2都会查找“输入”,而不是action方法返回的结果。有什么想法吗?@如果有错误,默认的工作流拦截器IIRC将转到“input”
结果。因此,这似乎是有意为之。(公平地说,我可能记不清了。)