struts2如何控制转换错误
我有一个包含 3 个字段、两个字符串(一个选择和一个文本字段)和一个整数的表单。当我在字段中输入字母或除 int (52.4 或 aaa)之外的其他内容时,我收到一个无法“捕获”的错误,我的选择消失,并且在消息框中我收到了来自我的验证 xml 文件的消息(在法语)和另一个英语(我想是由 struts 发送的)。
tomcat中的错误:
(ognl.OgnlValueStack 60 ) Error setting expression 'userSize' with value '[Ljava.lang.String;@14aa6c3'
消息框上的错误:
Invalid field value for field "userSize".
test size
JSP代码:
<s:form action="sendUserCreation" method="POST">
<s:action namespace="/" name="civilityPicklist" executeResult="true"/><br />
<s:textfield name="lastName" /><br />
<s:textfield name="userSize" /><br />
struts.xml代码:
<action name="createUser">
<result>/jsp/user/create.jsp</result>
</action>
<action name="sendUserCreation" class="fr.action.UserAction" method="createUser">
<result>/jsp/splash.jsp?messKey=${messKey}</result>
<result name="input">/jsp/user/create.jsp</result>
</action>
<action name="civilityPicklist" class="fr.imaps.oxygene.portal.action.CivilityPicklistAction">
<result>/jsp/user/civilityPicklist.jsp</result>
</action>
UserAction-validation.xml代码:
<validators>
<field name="userSize">
<field-validator type="conversion">
<message>test size</message>
</field-validator>
</field>
</validators>
UserAction代码:
private int userSize;
public int getUserSize() {
return userSize;
}
public void setUserSize(int userSize) {
this.userSize = userSize;
}
我认为“转换”验证类型会“捕获”此类问题,但似乎不会...我的代码有什么问题吗?
i've got a form with 3 fields, two String (one select and a text field) and an int. When i put a letter or something else than an int (52.4 or aaa) in the field i got an error that i cannot "catch", my select disapear and on the message box i ve got a message from my validation xml file (in french) and another one in english (i suppose sent by struts).
error in tomcat :
(ognl.OgnlValueStack 60 ) Error setting expression 'userSize' with value '[Ljava.lang.String;@14aa6c3'
error on message box :
Invalid field value for field "userSize".
test size
JSP code :
<s:form action="sendUserCreation" method="POST">
<s:action namespace="/" name="civilityPicklist" executeResult="true"/><br />
<s:textfield name="lastName" /><br />
<s:textfield name="userSize" /><br />
struts.xml code :
<action name="createUser">
<result>/jsp/user/create.jsp</result>
</action>
<action name="sendUserCreation" class="fr.action.UserAction" method="createUser">
<result>/jsp/splash.jsp?messKey=${messKey}</result>
<result name="input">/jsp/user/create.jsp</result>
</action>
<action name="civilityPicklist" class="fr.imaps.oxygene.portal.action.CivilityPicklistAction">
<result>/jsp/user/civilityPicklist.jsp</result>
</action>
UserAction-validation.xml code:
<validators>
<field name="userSize">
<field-validator type="conversion">
<message>test size</message>
</field-validator>
</field>
</validators>
UserAction code :
private int userSize;
public int getUserSize() {
return userSize;
}
public void setUserSize(int userSize) {
this.userSize = userSize;
}
I thought the "conversion" validation type will "catch" this kind of problem but it seems not... what's wrong with my code ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我快速浏览了一下这一点,我认为出现双重消息的原因是,至少在我的情况下,看起来转换错误可能被处理两次,首先由 StrutsConversionErrorInterceptor 处理,然后再由 AnnotationValidationInterceptor 处理。这两个拦截器都在 struts-default 堆栈中。
从堆栈中删除 StrutsConversionErrorInterceptor 会删除 Struts 生成的消息,并将用户定义的消息保留为唯一显示的错误消息。然而,我还没有时间调查去除这个受体的任何其他副作用。
I've been taking a quick look at this and I think that the reason for the double message is that, in my case at least, it looks like conversion errors may be being handled twice, firstly by the StrutsConversionErrorInterceptor and again by the AnnotationValidationInterceptor. Both these interceptors are in the struts-default stack.
Removing the StrutsConversionErrorInterceptor from the stack removes the Struts generated message and leaves the user defined message as the only error message displayed. However, I've not had time yet to investigate any other side consequences of removing this inreceptor.