Struts Quest:intializing and accessing classes used across actio

Discussions

Web tier: servlets, JSP, Web frameworks: Struts Quest:intializing and accessing classes used across actio

  1. I am really new to Struts. I would like to know what is the best way to create and initialize classes that would be used across All Actions....

    Since we are using struts for WEB, we already have api for checking entitlements of user, utility classes.

    I would like to have only single instance of them created rather than creating in the methods as and when they are needed.

    Please advice.

    Thanks
    Dimple
  2. Hi Dimple,

    You can put objects in a Session.
    For example, in an Action class, this is how you would put an object in a Session:

    public ActionForward execute( ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response) throws Exception
    {
    List nameList = new ArrayList();
    nameList.add("Homer Simpson");
    nameList.add("Bart Simpson");

    HttpSession session = request.getSession(false);
    session.setAttribute("ListOfNames", nameList)
    }

    This is how you would retrieve an object from the Session from an Action class futher downstream:
    public ActionForward execute( ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response) throws Exception
    {
    HttpSession session = request.getSession(false);
    List nameList = (ArrayList) session.getAttribute("ListOfNames");
    }
  3. I am more looking at ways to intantiate during teh startup of the servlet and somehow use the reference of object in all Actions and Forms
  4. I think Static classes would serve your purpose. The values set into a static class in the servlet would reflect in any Action class that uses that static class. Hope this solves your problem.

    -Parag
  5. I am really new to Struts. I would like to know what is the best way to create and initialize classes that would be used across All Actions....Since we are using struts for WEB, we already have api for checking entitlements of user, utility classes. I would like to have only single instance of them created rather than creating in the methods as and when they are needed.Please advice.ThanksDimple

    Implement a Plugin (http://struts.apache.org/api/org/apache/struts/action/PlugIn.html) and declare it in your <struts-config.xml>

    In the init method of your Plugin; store (keep alive) your singleton model objects that you want to expose to the application