Kush
Kush

Reputation: 75

Struts 2 action error messages

I am having a jsp page with two forms pointed to two action classes respectively. Now each form has a field where i have displayed the action errors & messages as :

<s:actionerror/>
<s:actionmessage/>

Now the problem is that, when action error/message is reported for any of the form ..the error/message is displayed in the both fields of the forms.

How do i specify an action error message respective to action class.

Upvotes: 4

Views: 10664

Answers (2)

Andrea Ligios
Andrea Ligios

Reputation: 50281

Return the same parameter with different values from both Actions to know which Action you are coming from, and show <actionerror/> and <actionmessage/> only inside its form.

<s:form action="firstAction">
    <s:if test="form==1">
        <s:actionerror/>
        <s:actionmessage/>
    </s:if>       
    <s:textfield name="someData" />
    <s:submit />
</s:form>


<s:form action="secondAction">
    <s:if test="form==2">
        <s:actionerror/>
        <s:actionmessage/>
    </s:if>    
    <s:textfield name="someOtherData" />
    <s:submit />
</s:form>

In firstAction.java

@Getter         private final static int form = 1;
@Getter @Setter private String someData;

In secondAction.java:

@Getter         private final static int form = 2;
@Getter @Setter private String someOtherData;

Upvotes: 2

Aleksandr M
Aleksandr M

Reputation: 24406

You cannot do this using addActionError or addActionMessage methods. But you can use addFieldError method with such key that it is not a name of any of the fields in your actions. And in JSP use <s:fielderror> tag to display that message.

Somewhere in the action class:

addFieldError("your_action_name_", "your_message");

Somewhere in other action class:

addFieldError("your_other_action_name_", "your_other_message");

In JSP:

<s:fielderror fieldName="your_action_name_" />

<s:fielderror fieldName="your_other_action_name_" />

Upvotes: 1

Related Questions