Discussions

Web tier: servlets, JSP, Web frameworks: Passing Request session object in Struts

  1. Passing Request session object in Struts (3 messages)

    This is probably a stupid newbie question, but I'm having a problem passing an object in the Request session from a Struts Action. If I place the object in "Session" scope it works fine. Everything I'm reading and the examples given show it should work fine [but it doesn't]. The error I keep receiving in my JSP is "Error 500: Cannot find bean cbnlist in any scope."

    Here's my code:
    Action.java

    import javax.servlet.http.HttpSession;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionError;
    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import java.util.List;

    public class getBrokerListingAction extends Action {

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

    ActionErrors errors = new ActionErrors();
    HttpSession session = request.getSession(false);

    try {

    String search = ((BrokerListingForm)form).getSearch();
    System.out.println("Search Criteria=" + search);


    if(search == null) {search = "SMITH";}

    List rs = DatabaseLookup.personLookup(search);
    session.setAttribute("cbnlist",rs);

    } catch (Exception e) {

    // Report the error using the appropriate name and ID.
    errors.add("name", new ActionError("id"));

    }

    return mapping.findForward("success");
    }
    }

    JSP file

    <%@ page language="java" %>
    <%@taglib uri="/tags/struts-html" prefix="html"%>
    <%@taglib uri="/tags/struts-bean" prefix="bean"%>
    <%@taglib uri="/tags/struts-logic" prefix="logic"%>

    <html:html>
    <body style="margin-top: 0px; margin-left: 0px">

    <html:errors />

    <html:form action="getBrokerListing" focus="search" >
    <html:text property="search" size="16" />
    <html:submit>
    <bean:message key="button.submit" /></html:submit>
    </html:form>

    <logic:iterate id="bbo" name="cbnlist">
    Name: <bean:write name="bbo" property="firstname" />&nbsp;<bean:write name="bbo" property="lastname" /><br>
    Pbno: <bean:write name="bbo" property="pbno" /><br>
    </logic:iterate>
    </body>
    </html:html>


    Struts-Config.xml

    <?xml version="1.0" encoding="ISO-8859-1" ?>

    <!DOCTYPE struts-config PUBLIC
              "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
              "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">


    <struts-config>

    <!-- ==================================== Data Source Configuration -->

    <!-- ======================================== Form Bean Definitions -->

        <form-beans>

        <form-bean
           name="brokerListingForm"
           type="cbn.generic_view.BrokerListingForm" />
        <form-bean
         name="brokerShortList"
         dynamic="true"
         type="org.apache.struts.action.DynaActionForm">
         <form-property name="cbnlist" type="cbn.generic_view.BrokerBaseObject" />
        </form-bean>

        </form-beans>


    <!-- ================================= Global Exception Definitions -->

        <global-exceptions>
        </global-exceptions>

    <!-- =================================== Global Forward Definitions -->

        <global-forwards>
    <forward
    name="mainpage"
    path="getBrokerListing.do" />
        </global-forwards>

    <!-- =================================== Action Mapping Definitions -->

    <action-mappings>
    <action
    path="/getBrokerListing"
    type="cbn.generic_view.getBrokerListingAction"
    name="brokerListingForm"
    scope="session"
    validate="false" >
    <forward name="success" path="/pages/broker_listing.jsp" redirect="true" />
    <forward name="failure" path="/pages/licensing.jsp" redirect="true">
    </forward>
    </action>
    </action-mappings>

    <!-- ===================================== Controller Configuration -->
        <controller
           contentType="text/html;charset=UTF-8"
           debug="3"
           nocache="true"
           processorClass="org.apache.struts.action.RequestProcessor"/>

    <!-- ================================ Message Resources Definitions -->

        <message-resources
           parameter="resources.application" />

    <!-- ======================================= Plug Ins Configuration -->


      <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
      </plug-in>

    </struts-config>

    Any help would be greatly appreciated.
  2. Sorry, gang. I meant to say Request SCOPE, not session.
  3. Sorry, gang. I meant to say Request SCOPE, not session.
    In that case, here is your problem:

    <forward name="success" path="/pages/broker_listing.jsp" redirect="true" />

    If you specify redirect="true", Struts uses a client-side redirect [response.sendRedirect()]. The JSP will be invoked by a new browser request, and any data stored in the old request will be lost.

    Your options are:

    1) Store the data in session scope.
    2) Don't set redirect="true".
  4. That worked! Thanks.[ Go to top ]

    That helps a lot. Thanks Paul.

    Chris