Reputation:
In the struts.xml I use:
<result name="error">error</result>
Then in my action I use:
addActionError("ERROR RETURNED");
return ERROR;
When I submit the form then it just goes to a blank page and does nothing. However, if I FORCE an exception to be thrown in the action then it goes to the error page and shows the ActionError message. So am I doing this wrong? If so, how should I tell struts to show an error page using "if statements" instead of relying solely on expensive try catches?
Please view edits below for more details.
EDIT 1:
I'm using struts 2 version: 2.1.8.1
EDIT 2:
For example, here is my action code that I'm using to test:
String test = "";
int number = 0;
try {
if (number == 1) {
test = SUCCESS;
} else if (number == 2) {
addActionError("ERROR RETURNED?");
test = ERROR;
} else if (number == 3) {
addActionError("ERROR RETURNED?");
test = INPUT;
} else {
test = LOGIN;
}
} catch (Exception e) {
addActionError("ERROR RETURNED? " + e);
}
return test;
Here is my number.jsp code:
<s:form action="number_save" method="post">
<s:textfield name="number" label="Enter number" />
</s:form>
<s:actionerror />
EDIT 3:
Here is a longer version of my struts.xml:
<action name="number" method="numberCreate" class="NumberActionBean">
<result>number.jsp</result>
</action>
<action name="error">
<result>error.jsp</result>
</action>
<action name="number_save" method="numberSave" class="NumberActionBean">
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success" type="redirect">index</result>
<result name="input" type="redirect">number</result>
<result name="error">error</result>
<result name="login" type="redirect">login</result>
<result name="none">number</result>
</action>
EDIT 4:
My error.jsp
is simply a <s:actionerror />
tag with the general taglibs and html tags...
Conclusion
I'm unable to show AddActionError
messages unless there's an exception. I am unable to use if
statements in my action.
Upvotes: 0
Views: 257
Reputation: 10017
Solution summary:
do not redirect "error" as a ction, just use
<result name="error"> **error.jsp** </result>
if you rly wanna redict error action with error messages. check this link out: implement-message-store-interceptor
Upvotes: 0
Reputation: 27614
If your only concern here is "expensive try catches" I would not proceed with this. It's sub-optimization and it will introduce complexity that in the long run will be more costly to maintain than what you gain in milliseconds of performance.
A try-block does not add any significant cost overhead, unless an exception is actually thrown. And when we talk about "significant overhead" in this context, it's in the low milliseconds range.
And why are you trying to optimize an error-page? It's not like that's something that will be called often.
Upvotes: 0
Reputation: 10017
Maybe you should do this:
<result name="error">error.jsp</result>
then in the error.jsp:
<s:actionerror/>
i am not sure, if you only type "error" instead of "error.jsp" as a result, struts tread "error" as an action, so you won't get you error message.
best, j
for your EDIT 3:
<action name="error">
<result name="error">error.jsp</result>
</action>
<action name="number_save">
<result name="error" type="redirectAction">error</result>
</action>
Upvotes: 2