Announcement Announcement Module
Collapse
No announcement yet.
sending form data from one jsp to another Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • sending form data from one jsp to another

    I'm developing a social networking portal using spring portal mvc.

    I have a scenario wherein i submit one form in a jsp and go to next jsp. I use the data submitted in first jsp for fetching more details from DB and send that data to second jsp. This data is used to pre-populate the form fields during initial display. User has the choice to update that data and resubmit which will update the data in db.

    This is basically for editing social group data. When he click on 'Edit social group' menu item, a form is displayed with a drop down containing the social group he is owning. User selects the social group name from the drop down box and click on 'Edit Social Group Data' button which takes him to next screen where in by default all social group group details from DB are pre-populated (for ex: social group name, social group description , members within that social group etc..). User has choice to edit them. Once he edits the data and submits the form, the social group details will be updated to DB.

    How can i do this in spring? I'm stuck at sending selected social group id from first form to second form. Also, if that fixed, i don't how to prepopulate the form with existing social group details when the second form is displayed for the first time.

    Hope i'm not confusing. IF anyone need any further detials, pls let me know.

    Any help is highly appreciated.

    Thank you.

  • #2
    Can anybody help me in this regard as it's bit urgent?

    Any help is highly appreciated.

    Thanks so much.

    Comment


    • #3
      Take a look at the AbstractWizardFormController - based on what you are describing this will probably be the easiest approach.

      Comment


      • #4
        Thanks Chris. Now i'm using AbstractWizardFormController.

        When i move from one page to other, i don't know how to pass data from page one to page 2.

        Basically i get social group id from 1st page and then in second page , based on the sg id selected, i pull the data from db and show it on second page.

        I don't know how to achieve that using AbstractWizardFormController.

        Below is my controller:

        Code:
        public class EditSocialGroupController extends AbstractWizardFormController {
            
            
            private RegistrationService regService;
            
        
            public void setRegService(RegistrationService regService) {
                this.regService = regService;
            }
            
        
            public EditSocialGroupController() { }
        
            @Override
            protected Object formBackingObject(PortletRequest request) throws Exception {
                //return super.formBackingObject(arg0);
                SocialGroup sg = new SocialGroup();
                sg.setSgId(request.getParameter("sgId"));
                sg.setSgDesc("description");
                return sg;
            }
        
            @Override
            protected void onBind(PortletRequest request, Object command, BindException errors) throws Exception {
                System.out.println("Entering EditSocialGroupController -> onBind");     
                String sgName = request.getParameter("sgObjArr");
                
                if(( sgName == null) || ("".equalsIgnoreCase(sgName.trim())) || ("-1".equalsIgnoreCase(sgName.trim()))) {
                    errors.rejectValue("sgName", "required.socialGroupName");
                }
                System.out.println("Leaving EditSocialGroupController -> onBind");     
            }
        
            @Override
            protected Map referenceData(PortletRequest request, int page) throws Exception {
                //return super.referenceData(arg0, arg1);
                System.out.println("Entering EditSocialGroupController -> referenceData");        
                
                Registration regObj = (Registration) getPortletContext().getAttribute("regObj");        
                
                SocialGroup[] sgArr = regService.getSocialGroups(regObj);
                Map referenceData = new HashMap();
                referenceData.put("sgObjArr",sgArr);                
                
                System.out.println("Leaving EditSocialGroupController -> referenceData");
                return referenceData;    
            }
        
            @Override
            protected void passRenderParameters(ActionRequest request, ActionResponse response) {
                //super.passRenderParameters(arg0, arg1);
                request.setAttribute("sgId", request.getParameter("sgObjArr"));
            }        
            
        }
        Here is the declaration of controller in xml file:

        Code:
                <bean id="editSocialGroupController" class="com.mosonex.portal.controller.EditSocialGroupController">
        		<property name="sessionForm" value="true"/>
        		<property name="commandName" value="sgObj"/>
        		<property name="commandClass" value="com.mosonex.portal.sdo.SocialGroup"/>
        		<property name="pages">
                            <list>
                                <value>editSocialGroup</value>
                                <value>editSocialGroupDetails</value>
                            </list>
                        </property>                          
                        
                        <property name="allowDirtyForward">
                            <value>false</value>
                        </property>
        
                        <property name="allowDirtyBack">
                            <value>false</value>
                        </property>
        
                        <property name="bindOnNewForm">
                            <value>true</value>
                        </property>
                        
                        <property name="regService">
                            <ref bean="Registration" />
                        </property>
                </bean>
        here is the 1st jsp (editSocialGroup.jsp).

        Code:
        	<h2>Edit Social Group</h2>
        
        	<br>
        <font size="2.5">Fields marked with asterisk (<font color="red">*</font>) are mandatory. <br><br>
        </font>
        <form:form commandName="sgObj" method="post" action="${aURL}" enctype="application/x-www-form-urlencoded">  
        
        <br>
                     <font color="red"><form:errors path="sgName" /></font>
        	<br>
                    <br>
        	<table border="0" cellpadding="4">
        		<tr>
                             <td class="label">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="red">*</font> Group Name:</td>
        <!--                 <td><input type="text" class="input" name="sgName" value=""/></td>-->
                             <td>
                                 <form:select path="sgObjArr">
                                       <form:option value="-1" label="--Select--"/>
                                       <form:options items="${sgObjArr}"  itemValue="sgId" itemLabel="sgName"/>
                                </form:select>
                             </td>
                        </tr>               
                        
                        <tr align="center">
                            <td colspan="2"><input type="submit" name="_target1" value="Edit Social Group"/></td>                    
                        </tr>
        	</table>
        </form:form>
        Here is my second jsp (editSocialGroupDetails.jsp)

        Code:
        	<h2>Edit Social Group</h2>
        
        	<br>
        <font size="2.5">Fields marked with asterisk (<font color="red">*</font>) are mandatory. <br><br>
        </font>
        <form:form commandName="sgObj" method="post" action="${aURL}" enctype="application/x-www-form-urlencoded">  
        
        <br>
                     <font color="red"><form:errors path="sgName" /></font>
        	<br>
                    <br>
        	<table border="0" cellpadding="4">
        		<tr>
                                 <td class="label">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="red">*</font> Group Name:</td>
        			<td><form:input path="sgName" /></td>
                        </tr>               
                        <tr>
                             <td class="label">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Description:</td>
                             <td><form:textarea path="sgDesc" rows="8" cols="50" /></td>
                        </tr>
                        <tr align="center">
                            <td colspan="2"><input type="submit" name="_finish" value="Update Social Group" /></td>                    
                        </tr>
        	</table>
        </form:form>
        Please help. I appreciate your help.

        Comment


        • #5
          I could able to get data from page 1 to page 2 using the modified controller as below:

          Code:
          
          public class EditSocialGroupController extends AbstractWizardFormController {
              
              
              private RegistrationService regService;
              
          
              public void setRegService(RegistrationService regService) {
                  this.regService = regService;
              }
              
          
              public EditSocialGroupController() { }
          
              @Override
              protected Object formBackingObject(PortletRequest request) throws Exception {
                  //return super.formBackingObject(arg0);
                  SocialGroup sg = new SocialGroup();
                  return sg;
              }
          
              @Override
              protected void onBind(PortletRequest request, Object command, BindException errors) throws Exception {
                  SocialGroup sg = (SocialGroup) command;
                  
                  System.out.println("Entering EditSocialGroupController -> onBind");     
                  String sgName = request.getParameter("sgObjArr");
                  
                  if(( sgName == null) || ("".equalsIgnoreCase(sgName.trim())) || ("-1".equalsIgnoreCase(sgName.trim()))) {
                      errors.rejectValue("sgName", "required.socialGroupName");
                  } else {
                      
                      SocialGroup sg1 = regService.getSocialGroup(sgName);
                      sg.setSgDesc(sg1.getSgDesc());
                      sg.setSgId(sg1.getSgId());
                      sg.setSgName(sg1.getSgName());
                      System.out.println("sgId -----------> " + sg.getSgId());
                      System.out.println("sgName -----------> " + sg.getSgName());
                      System.out.println("sgDesc -----------> " + sg.getSgDesc());
                  }
                  System.out.println("Leaving EditSocialGroupController -> onBind");     
              }
          
              
              @Override
              protected void validatePage(Object request, Errors errors, int page) {
                  //super.validatePage(arg0, arg1, arg2);
                  if(page == 1) {
                      System.out.println("Entered page == 1 EditSocialGroupController -> validatePage");     
                      ValidationUtils.rejectIfEmptyOrWhitespace(errors, "sgName", "required.socialGroupName");
                  }
                  
              }
          
              @Override
              protected void processFinish(ActionRequest request, ActionResponse response, Object command, BindException errors) throws Exception {
                  //super.processFinish(arg0, arg1, arg2, arg3);
                  System.out.println("Entered EditSocialGroupController -> processFinish ==============================");     
                  SocialGroup sg = (SocialGroup) command;
                  
                  regService.updateSocialGroup(sg);
                  
              }
          
              @Override
              protected void processCancel(ActionRequest arg0, ActionResponse arg1, Object arg2, BindException arg3) throws Exception {
                  //super.processCancel(arg0, arg1, arg2, arg3);
                  System.out.println("Entered EditSocialGroupController -> processCancel ==============================");  
              }
          
              @Override
              protected Map referenceData(PortletRequest request, int page) throws Exception {
                  //return super.referenceData(arg0, arg1);
                  System.out.println("Entering EditSocialGroupController -> referenceData");        
                  
                  Registration regObj = (Registration) getPortletContext().getAttribute("regObj");        
                  
                  SocialGroup[] sgArr = regService.getSocialGroups(regObj);
                  Map referenceData = new HashMap();
                  referenceData.put("sgObjArr",sgArr);                
                  
                  System.out.println("Leaving EditSocialGroupController -> referenceData");
                  return referenceData;    
              }
          
          }
          Now i've a new problem.

          In the second JSP, i've two buttons ..... 'Update Data' and 'Cancel'. Whenever i click on any of the buttons, i get a HTTP Status 404 error .... page not found error. The page it's referring is -> /spring-portlet-sample/WEB-INF/jsp/editSocialGroupDetails.jsp.

          Here is my editSocialGroupDetails.jsp.

          Code:
          	<h2>Edit Social Group</h2>
          
          	<br>
          <font size="2.5">Fields marked with asterisk (<font color="red">*</font>) are mandatory. <br><br>
          </font>
          <form:form commandName="sgObj" method="post" enctype="application/x-www-form-urlencoded">  
          
          <br>
                       <font color="red"><form:errors path="sgName" /></font>
          	<br>
                      <br>
          	<table border="0" cellpadding="4">
          		<tr>
                                   <td class="label">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="red">*</font> Group Name:</td>
          			<td><form:input path="sgName" /></td>
                          </tr>               
                          <tr>
                               <td class="label">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Description:</td>
                               <td><form:textarea path="sgDesc" rows="8" cols="50" /></td>
                          </tr>
                          <tr align="center">
                              <td colspan="1"><input type="submit" name="_finish" value="Update Social Group" /></td>
                              <td colspan="1"><input type="submit" name="_cancel" value="Cancel" /></td>
                          </tr>
          	</table>
          </form:form>
          When i click either 'Update Social Group' or Cancel button, it's not going to controller .. i mena ... it's not calling controllers processFinish or processCancel methods.

          Can somebody help me in this regard. IT will be a GREAT help to me.

          THank you.

          Comment


          • #6
            Originally posted by laxmiraghu View Post

            Code:
            <form:form commandName="sgObj" method="post" enctype="application/x-www-form-urlencoded">
            There is no action set on the form tag.

            Just as a couple of pointers:

            A good place to add the logic to update the command object with the social group is the postProcessPage method.

            One of the page specific onBindAndValidate methods would be a good place to put the validation logic for checking if a social group id has been set.

            I see you are storing a registration object in the PortletContext. not sure exactly what this object contains, but do you realise this is shared by all portlets and servlets in the portlet application and is global for all users? If this contains user specific registration information then its not the place to store it!

            Hope this helps,

            Chris

            Comment


            • #7
              Thanks Chris for the pointers and best practices. I will surely implement them.

              One question though. Regsitration object is basically user object which contains logged-in user details. I need it in other places other than login page to refer to some data. So, i stored it in protlet context. What is the best place to store ..... is it session ? or any other way of storing it so that i can retrieve it whenever i want.

              Please advice.

              I appreciate all your help. Thanks again.

              Comment


              • #8
                The PortletSession is probably going to be the best place to store it. However, remember that this is only shared by portlets that are in the same portlet application. If you need to share the object further than this you will need to consider other mechanisms. It is possible that your portal may provide something that can help with this - for example, in Websphere Portal there is a distributed object cache that can be used for this purpose.

                Comment


                • #9
                  Thanks Chris for the info. I implemented that in my code.

                  Thanks again.

                  Comment


                  • #10
                    sending data from one jsp to another

                    Hello plz help me out,

                    I have a jsp Pople.jsp with checkboxes mapped to People Controller
                    and another CopyMove.jsp mapped to CopyMove Controller.

                    From People.jsp user will select the checkboxes . The value(Employee Id) associated with checkboxes I need in second JSP i.e CopyMove.jsp.

                    In Controller for CopyMove .jsp , I wil do the required database part. I have
                    finished my back end work but I am not getting how to get the values from
                    People.jsp to CopyMove.jsp.


                    Any help is highly appreciated.

                    Thanks

                    Comment


                    • #11
                      regarding data from one jsp page to another

                      what i say might look very trivial but it works fine.

                      In the OnSubmit event of the controller just use the the follwing code

                      CODE

                      request.getSession().setAttribute("Att_name", value);


                      WHERE Att_name is the name of the attribute and value is the actual value

                      On the other jsp page just use

                      request.getSession().getAttribute("Att_name")

                      thus u can get the value which u sent there

                      The main thing is that form should have a post method in the first jsp page

                      Comment

                      Working...
                      X