java - 有没有一种方法可以在使用重定向操作时获取操作类中的属性值,而无需将它们作为查询字符串参数提供?

标签 java struts2 query-string querystringparameter

POST请求成功时,请求将被重定向,如下所示。

private String message; //Getter and setter.

//Required validators.
@Action(value = "AddUpadteCategory",
    results = {
        @Result(name=ActionSupport.SUCCESS, type="redirectAction", params={"namespace", "/admin_side", "actionName", "Category", "currentPage", "${currentPage}", "message", "${message}", "status", "${status}"}),
        @Result(name = ActionSupport.INPUT, location = "Category.jsp")},
    interceptorRefs={
        @InterceptorRef(value="defaultStack", params={"params.acceptParamNames", "catId, catName, currentPage, rowCount, totalPages, status", "validation.validateAnnotatedMethodOnly", "true"})
    })
public String insertOrUpdate()
{
    if(true)  //Some conditional check.
    {
         setMessage(getText("message.key.true"));
    }
    else
    {
          setMessage(getText("message.key.false"));
    }

    return ActionSupport.SUCCESS;
}

如果没有发生验证/转换错误,则此消息将作为查询字符串参数提供。

有时,此消息可能会很长,形成一个很长的 URL 字符串。

有没有办法在使用重定向操作时获取此消息而不将其作为查询字符串参数传递?

无法将此消息存储到 session 中。

最佳答案

使用存储拦截器并将其operationMode参数设置为AUTOMATIC,如下所示:

<action name="actionName" class="..." method="...">
    <interceptor-ref name="store">
        <param name="operationMode">AUTOMATIC</param>
    </interceptor-ref>
    <interceptor-ref name="defaultStack" />
    <result ..../>
</action>

它会自动将您的消息设置在适当的范围内。对于 redirectAction 结果,它将是 session 范围,但在显示消息后,此拦截器将自动删除它们。

编辑(注释语法):

 interceptorRefs={
        @InterceptorRef(value="store", params={"operationMode", "AUTOMATIC"}),
        @InterceptorRef(value="defaultStack", params={"params.acceptParamNames", "catId, catName, currentPage, rowCount, totalPages, status", "validation.validateAnnotatedMethodOnly", "true"})
    })

关于java - 有没有一种方法可以在使用重定向操作时获取操作类中的属性值,而无需将它们作为查询字符串参数提供?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21576871/

相关文章:

struts2 - 有没有办法让 Struts 2 资源包指向文件位置?

java - 在 Struts2 中验证整数值?

ruby-on-rails - 当存在相同的键时,Rails 中的查询字符串操作

django - 如何使用查询参数构造 Django 反向/url?

java - Gradle 中仅包含多个主要的sourcsets

Java 和 Tomcat - CodeCache 已满。编译器已被禁用

java - 带有Opera和Edge浏览器的远程Webdriver

java - 如何在使用 "No Line Found Error"时防止 "While Loop?"

java - 让 sitemesh 与 struts2 一起工作

php - http_build_query 函数的 urlencoding 过多