Frage

i have override validate method and added errors using addFieldError("test", "test print");

and in jsp used <s:fielderror />

but the errors are not displayed in input.jsp.

Also my jsp content type is <%@ page contentType="text/html; charset=UTF-8"%>

My struts.xml is like

            <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>
War es hilfreich?

Lösung

Turns out that errors (field and action are NOT maintained across a chain.

Following proves this (assumes struts2-conventions-plugin-VERSION):

Action foo always chains to action bar (so we only need a view for action bar)

Action 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;
    }
}

Action bar

package com.quaternion.action;

import com.opensymphony.xwork2.ActionSupport;

/** #2 SOMETHING WILL BE ADDED HERE TO FIX THE ISSUE**/

public class Bar extends ActionSupport{  
}

view for bar: /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>

Testing the above we see nothing show up in the errors.

To fix the issue we use the store interceptor: 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.

Andere Tipps

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top