0

我目前的设置有问题。我的 JSP 中有这个动作标签

<s:action name="doLogin" executeResult="true"></s:action>

下面是对应的 struts.xml 条目

<action name="doLogin" class="siteLoginAction">
    <result name="input">login</result>
    <result name="success">home</result>
</action>

jsp 只是一个登录 div(表单、文本字段、标签)。

现在的问题是,当我测试验证功能时

@Override
public void validate() {
    String username = siteUser.getSiteUserUsername();
    String password = siteUser.getSiteUserPassword();

    if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
        addFieldError("siteUser.siteUserUsername", "Invalid login");
    } else if (!siteUserService.checkLoginExists(username, password, getBlogSiteUrl())) {
        addFieldError("siteUser.siteUserPassword", "Invalid login");
    }
}

如您所见,它有效,

在此处输入图像描述

但它不会消失。每次我访问该页面时,错误消息都会保留在那里。

这是因为 Struts2 动作的单例默认模型吗?我已经尝试过 @Scope("prototype") 但如果我使用它,它根本不会显示错误

4

1 回答 1

0

我的 JSP 中有这个<s:action>标签

你不应该使用那个标签,它是一种古老而无用的技术,放弃它 ASAYC。

这是因为 Struts2 动作的单例默认模型吗?我已经尝试过 @Scope("prototype") 但如果我使用它,它根本不会显示错误

操作是 ThreadLocal。您所指的“默认模型” 是 Spring bean 的默认范围(Singleton),因此如果由 Spring 管理,则是 Struts2 Action的默认范围。在这种情况下,您应该使用scope="prototype"它来使 Spring 托管操作作为 ThreadLocal 对象正常工作,这是错误的。

这个问题现在无法回答。只需删除<s:action/>标签,使用scope="prototype"(或删除 Spring,至少在操作处理上)然后看看会发生什么,然后如果它仍然不起作用,请回到这里再次询问:那时它会很容易帮助你。

于 2016-05-19T08:10:37.453 回答