Struts 2 Error Messages

I have a jsp page with two forms pointing to two classes of actions respectively. Now each form has a field where I displayed errors and action messages as:

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

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

How to specify an action error message corresponding to an action class.

+4
source share
2 answers

Return the same parameter with different values ​​from both actions to find out which action you came from and show <actionerror/>it <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;
+2

addActionError addActionMessage. addFieldError , - . JSP <s:fielderror>, .

- :

addFieldError("your_action_name_", "your_message");

- :

addFieldError("your_other_action_name_", "your_other_message");

JSP:

<s:fielderror fieldName="your_action_name_" />

<s:fielderror fieldName="your_other_action_name_" />
+1

All Articles