Struts2验证无法正常工作,未显示验证错误消息 [英] Struts2 validation is not working properly, validation error messages not shown

查看:123
本文介绍了Struts2验证无法正常工作,未显示验证错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我使用简单的主题,但即使没有主题,也会发生相同的行为(页面格式除外).当我提交表单时,名称字段为空,并重定向到register.jsp而不显示验证错误.在检查日志或使用调试器后,似乎验证工作正常,并且服务器日志消息已按预期写入.我将发布通用代码.

First of all I use simple theme but even without it the same behaviour occurs (except page formatting). When I submit the form, name field gets empty and redirects to register.jsp without displaying the validation error. After checking the logs or with debugger, it seems that validation is working properly and server log messages are written as expected. I'll post generic code.

struts.properties

struts.ui.theme=simple

我的动作

private User user = new User()    // getter and setter

@Inject
transient UserDAO userDAO;

@Override
public User getModel() {
    return user;
}

public void validate(){

    LOG.debug("NAME VALIDATION " + user.getName());
    if("".equals(user.getName())){
        addFieldError("user.name", "Name can't be empty");
        LOG.debug("Validation Error on name");
    }       
}

经调试器检查后,validate方法有效,并写入了日志.

Checked with debugger, validate method is working and logs are written.

struts.xml

<package name="users" extends="struts-default">
    <action name="registerUser" method="prepareRegister" class="com.test.MyAction">
        <result name="success">/register.jsp</result>
    </action>

    <action name="saveOrUpdateUser" method="saveOrUpdate" class="com.test.MyAction">
        <result name="input" type="redirect">registerUser</result>
        <result name="success" type="redirect">listUser</result>
    </action>
</package>

register.jsp

<td>
    <s:textfield id = "userName" 
              label = "User Name" 
               name = "user.name" />
</td>
<s:fielderror fieldName = "user.name" />

请随时向我澄清.我对Struts 2相当陌生,我尝试了Struts 2文档验证方法,并检查了其他教程.我不知道我是否丢失了某些东西或者我有没有注意的错误配置,因为逻辑在起作用,而视图部分(jsp)却不在.预先感谢.

Feel free to ask me for clarifications. I am pretty new to struts 2, I tried the struts 2 documentation validation way, and checked other tutorials too. I don't know if I am missing something or I have some missconfiguration that I am not noticing, since the logic is working and the view part (jsp) is not. Thanks in advance.

推荐答案

阅读 INPUT结果的工作原理,然后已经放弃了ModelDriven设计模式,除了问题之外,该模式不会给您的编程体验带来任何好处,因为它们很容易隐藏在拦截器堆栈的中间 a>,请注意:

After having read how the INPUT result works and having abandoned the ModelDriven design pattern that adds nothing to your programming experience except problems, that might easily hide themselves in the middle of the Interceptor Stack, note that:

  • redirect是重定向到外部URL或非Action URL时使用的结果,而redirectAction重定向到Action时应使用;
  • 重定向时,将创建一个新请求,因此旧参数(包括操作错误以及消息和字段错误)将丢失.
  • redirect is the result to use when redirecting to external URLs or non-Action URLs, while redirectAction should be used when redirecting to an Action;
  • when redirecting, a new request is created, and hence the old parameters (including action errors and messages and field errors) are lost.

为防止这种情况,如果您想继续使用PRG模式(并因此使用重定向),可以使用 MessageStore拦截器,它将跨重定向为您存储和检索消息;只需定义一个包含它的拦截器堆栈即可解决您的问题.

To prevent that, if you want to keep using the PRG pattern (and hence the redirection), you can use the MessageStore Interceptor that will store and retrieve the messages for you across the redirections; just define an interceptor stack that contains it, and it will solve your problem.

或者像文档中示例中的那样进行一次

Or do it once like in the example from the documentation.

这篇关于Struts2验证无法正常工作,未显示验证错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆