2

我有覆盖验证方法并使用添加错误 addFieldError("test", "test print");

并在jsp中使用 <s:fielderror />

但错误未显示在 input.jsp 中。

我的jsp内容类型也是 <%@ page contentType="text/html; charset=UTF-8"%>

我的 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>
4

2 回答 2

3

结果发现错误(字段和操作不会跨链维护。

以下证明了这一点(假设 struts2-conventions-plugin-VERSION):

动作 foo 总是链接到动作栏(所以我们只需要动作栏的视图)

行动富

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>

测试上述内容,我们发现错误中没有显示任何内容。

为了解决这个问题,我们使用了商店拦截器:http ://struts.apache.org/2.0.14/struts2-core/apidocs/org/apache/struts2/interceptor/MessageStoreInterceptor.html

In the first action (#1) we will need to add annotations and the imports to support them:

import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;

@InterceptorRefs({
    @InterceptorRef(value = "store", params = {"operationMode","STORE"}),
    @InterceptorRef("defaultStack"),
})

In the second action (#2) we will need to add annotations and the imports to support them:

import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;

@InterceptorRefs({
    @InterceptorRef(value = "store", params = {"operationMode","RETRIEVE"}),
    @InterceptorRef("defaultStack"),
})

And now it works.

于 2012-04-05T20:10:24.700 回答
2

If you're going to use action chaining anyway (which IMO is almost never required, almost never a good idea, and almost always leads to unintended coupling between actions), use what the chaining interceptor already provides and avoid configuring something besides "chain".

As per the docs:

  • struts.xwork.chaining.copyErrors - set to true to copy Action Errors
  • struts.xwork.chaining.copyFieldErrors - set to true to copy Field Errors
  • struts.xwork.chaining.copyMessages - set to true to copy Action Messages
于 2012-04-06T12:51:30.870 回答