Results 1 to 5 of 5

Thread: Applying Advice to Struts Action Class to handle exception

  1. #1
    Join Date
    Jul 2005
    Posts
    17

    Default Applying Advice to Struts Action Class to handle exception

    I want to apply logging advice to Struts Action classes. Specifically I want to apply logging when the Struts Action class throws an exception.

    I have integrated the Struts with Spring using the ContextLoaderPlugin in the struts-config.xml as :

    <plug-in
    className="org.springframework.web.struts.ContextL oaderPlugIn">
    <set-property property="contextConfigLocation" value="/WEB-INF/spring-test.xml"/>
    </plug-in>

    My struts-config.xml contains the following action mapping:

    <action path="/myAction"
    type="org.springframework.web.struts.DelegatingAct ionProxy"
    name="mForm"
    scope="request"
    parameter="method"
    validate="false">
    <forward name="success" path="/index.jsp" />
    </action>

    My spring config file has following reference:

    <bean name="/Module/myAction"
    class="MyAction">
    <property name="test">
    <ref bean="test"/>
    </property>
    </bean>

    Everything works fine and struts is integrated perfectly but If i try to intercept the above struts action to put in a logging advice for exceptions it says that class has to be a struts action. Is this a limitation as far as struts can go with spring such that no advices can be applied to struts action classes ?

    Another way around this is purely to use struts exception handler declared in the struts config file but in that case I am not able to get the action class name which actually throws the excpetion for logging purposes. Thats why I was looking at applying spring advices to struts action classes.

    Any direction in this issue will be greatly appreciated.

  2. #2
    Join Date
    Aug 2004
    Location
    Southampton, UK
    Posts
    826

    Default

    Can you post the configuration you are attempting to use for proxying? Also can you post a sample of one of your action classes?

    Rob
    Rob Harrop
    Lead Engineer, dm Server
    SpringSource
    http://www.springsource.com

    Co-Author - Pro Spring

  3. #3
    Join Date
    Jul 2005
    Posts
    17

    Default

    Here the relevant part from the spring config file:

    <bean name="/Announce/viewTopAnnInCategory"
    class="org.cait.argyle.module.announcement.struts. action.ViewTopAnnInCategoryAction">
    <property name="viewTopAnnInCategory">
    <ref bean="viewTopAnn"/>
    </property>
    </bean>

    <bean id="argyleExceptionActionAdvice"
    class="org.cait.argyle.module.common.spring.Argyle ExceptionActionAdvice"/>

    <bean id="exceptionAutoProxy" class="org.springframework.aop.framework.autoproxy .BeanNameAutoProxyCreator">
    <property name="beanNames">
    <list>
    <value>/Announce/viewTopAnnInCategory</value>
    </list>
    </property>
    <property name="interceptorNames">
    <list>
    <value>argyleExceptionActionAdvice</value>
    </list>
    </property>
    </bean>

    Following is the relevant Action class:

    public class ViewTopAnnInCategoryAction extends ArgyleTopAction implements ArgyleAction {

    /**
    *
    * @param mapping The ActionMapping used to select this instance
    * @param actionForm The optional ActionForm bean for this request (if any)
    * @param request The HTTP request we are processing
    * @param response The HTTP response we are creating
    *
    * @exception ServletException if a servlet exception occurs
    */

    static Log logger = LogManager.getLog(ViewTopAnnInCategoryAction.class );

    private ViewTopAnnInCategory viewTopAnnInCategory = null;

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
    HttpServletResponse response) throws Exception {

    /** Get the user session Object */
    HttpSession session = request.getSession(true);

    /** access control */
    checkUserAccess(session, "view", "category" );

    String groupId = (String)request.getParameter("groupId");
    Vector anns = viewTopAnnInCategory.getTopAnnInCategory(groupId);
    request.setAttribute("topAnnsInCategory",anns);
    return (mapping.findForward("success"));
    }

    /**
    * @return Returns the viewTopAnnInCategory.
    */
    public ViewTopAnnInCategory getViewTopAnnInCategory() {
    return viewTopAnnInCategory;
    }
    /**
    * @param viewTopAnnInCategory The viewTopAnnInCategory to set.
    */
    public void setViewTopAnnInCategory(
    ViewTopAnnInCategory viewTopAnnInCategory) {
    this.viewTopAnnInCategory = viewTopAnnInCategory;
    }
    }

  4. #4
    Join Date
    Aug 2004
    Location
    Southampton, UK
    Posts
    826

    Default

    I think the problem is related to the fact that BeanNameAutoProxyCreator is creating the AOP proxies using JDK proxies and not bytecode proxies. This means that the proxy will only implement the interfaces of your object and it wont actually be a subclass of the Struts Action class. You can force the bytecode proxies to be used by setting proxyTargetClass to true in the configuration for BeanNameAutoProxyCreator.

    Rob
    Rob Harrop
    Lead Engineer, dm Server
    SpringSource
    http://www.springsource.com

    Co-Author - Pro Spring

  5. #5
    Join Date
    Jul 2005
    Posts
    17

    Default

    Thanks for the directions. I added the cglib jar and set the proxyTarget=true for the BeanNameAutoProxyCreator class. I am able to apply advices to struts action classes using this way.

    Thanks

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. Replies: 3
    Last Post: Sep 4th, 2005, 11:11 PM
  5. Stack Overflow
    By rayho222 in forum Container
    Replies: 6
    Last Post: May 17th, 2005, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •