[
https://issues.apache.org/jira/browse/WW-4234?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13806317#comment-13806317
]
Shailesh Arvind Vaishampayan commented on WW-4234:
--------------------------------------------------
@Lukasz : Yes i did. Please see below by custom interceptor :
public class BusinessExceptionInterceptor extends BaseInterceptor {
@Override
public String intercept(ActionInvocation ai) throws Exception {
String result = null;
try{
result = ai.invoke();
}catch(BusinessException e){
ActionSupport as = (ActionSupport)ai.getAction();
as.addActionMessage(e.getMessage());
//OR throw e
}
return result //have to comment this out if i am throwing
exception from catch instead of swallowing it;
}
}
my action config is as follows :
<package name="client" extends="default" namespace="/client">
<interceptors>
<interceptor name="busineesClientException"
class="com.jkweb.web.interceptors.BusinessExceptionInterceptor"></interceptor>
</interceptors>
<action name="showProject"
class="com.jkweb.web.actions.client.ClientAction" method="test">
<exception-mapping result="showProject"
exception="com.jkweb.exception.BusinessException" >
</exception-mapping>
<interceptor-ref
name="busineesClientException"></interceptor-ref>
<result name="showProject" type="tiles"
>showProject</result>
</action>
</package>
And my action class method is below :
public String test() throws Exception{
throw new BusinessException("test message");
//return "success";
}
However my result variable does not get initialized as action invocation throws
an exception. So if i swallow the exception in my custom interceptor by adding
action message result is null and nothing happens.
Contrary to that if I add action message and throw the exception as it is,
stack trace is displayed which i don't want.
What i want is
after catching the exception and adding action message i should be able to
return to the same page. Also, and this interceptor is applied to different
actions i should be able to return result corresponding to that particular
action.
Not sure how to do this.
> Ability to catch service layer exceptions at one place and allow adding
> action messages/errors before results are rendered
> --------------------------------------------------------------------------------------------------------------------------
>
> Key: WW-4234
> URL: https://issues.apache.org/jira/browse/WW-4234
> Project: Struts 2
> Issue Type: Improvement
> Affects Versions: 2.3.15.3
> Reporter: Shailesh Arvind Vaishampayan
> Priority: Minor
> Fix For: 2.3.x
>
>
> I am working with an application involoving Struts2 in web layer and Spring
> in business layer. I also have BusinessException class which will be used by
> all business services to create business related validation failures which
> must go up to web layer and should be show to users as validation message. I
> can easily do this by writing in my Action class:
> {code:java}
> ClientAction extends ActionSupport throws Exception{
> ....
> try{
> clientService.searchClient();
> }catch(InvalidClientSearchCriteriaException e){
> addActionMessage("Invlid Search Criteria");
> }
> ...
> {code}
> And similar code in every action class. However i dont want to pollute my
> action classes with try catch blocks. Instead it would be much better if i
> can write on try catch block at one place and catch all the exceptions there
> as BusinessExceptions and create messages /errors from embedded
> messages/errors in those exceptions. One approach i could think of was to use
> interceptor or preResultListener. But I cannot use interceptor like below
> which catches business exceptions thrown from action classes...
> {code:java}
> ExceptionInterceptor extends AbstractInterceptor(ActionInvocation
> ivocation,...){
> try{
> invocation.invoke();
> }catch(Exception e){
> if(e instanceof BusinessException){
> ActionSupport as = (ActionSupport)invocation.getAction();
> String message = extractMessagefromException()//--custom
> method to extract message embedded in exception.
> as.addActionMessages(message);
> //-- above will not work result has already been rendered right? and
> hence it wouldn't matter if i add actoinmessages now.
> }
> }
> }
> {code}
> Second approach of of preResultListener is to add action messages just like
> above in preResultListener's method as result is yet to be rendered and i can
> safely change that. However not sure if preResultListener will ever execute
> if exception is thrown in action? and even if it does how i can get the
> exception object thrown by the action.?
> There has to be something out of the box so that actions are not cluttered
> with try-catch blocks
--
This message was sent by Atlassian JIRA
(v6.1#6144)