Announcement Announcement Module
Collapse
No announcement yet.
Applying Advice to Struts Action Class to handle exception Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment


    • #3
      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;
      }
      }

      Comment


      • #4
        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

        Comment


        • #5
          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

          Comment

          Working...
          X