Announcement Announcement Module
Collapse
No announcement yet.
Spring help with JSP - One value through Multiple Forms Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring help with JSP - One value through Multiple Forms

    Hey all,

    I'm trying to create a view using JSP that will populate a selection box with simple name-only entities. Next to the box I want three submit buttons: Edit, Add, Delete.
    I've got the Edit and Add buttons to work. The Edit uses the value in the box, and the Add doesn't need any value. The problem is that the Delete form requries the same value in the box that the Edit form requires.
    I'm rather new to JSP, but is there a way to globally use an input value throughout separate forms? Here is what I have so far:

    Code:
    <%@ include file="/WEB-INF/jsp/spring/IncludeTop.jsp" %>
    
    <P>
    <H2>Simple Entities&#58;</H2>
    <TABLE border="1">
    	<TR><FORM method="GET" action="<c&#58;url value="/editDepartment.htm"/>">
    		<TD><B>Department&#58;</B>
                <SELECT name="departmentId" id="departmentId">
                    <c&#58;forEach var="department" items="$&#123;model.departmentTypes&#125;">
                      <c&#58;if test="$&#123;model.department.id == department.id&#125;">
                        <OPTION selected="<c&#58;out value="$&#123;model.department.id&#125;"/>" 
                            value="<c&#58;out value="$&#123;department.id&#125;"/>">
                               <c&#58;out value="$&#123;department.name&#125;"/></OPTION>
                      </c&#58;if>
                      <c&#58;if test="$&#123;model.department.id != department.id&#125;">
                            <OPTION value="<c&#58;out value="$&#123;department.id&#125;"/>">
                            <c&#58;out value="$&#123;department.name&#125;"/></OPTION>
                      </c&#58;if>
                 </c&#58;forEach>
             </SELECT></TD>
      		<TD><INPUT type="submit" value="Edit"/></FORM></TD>
      		<TD><FORM method="GET" action="<c&#58;url value="/addDepartment.htm"/>">
      			<INPUT type="submit" value="Add"/></FORM></TD>
      		<TD><FORM method="GET" action="<c&#58;url value="/deleteDepartment.htm"/>">
      			<INPUT type="submit" value="Delete"/></FORM></TD>
        </TR>
    </TABLE>
    <P>
    <BR>
    
    <%@ include file="/WEB-INF/jsp/spring/IncludeBottom.jsp" %>
    I'd like to use the value of departmentId as part of the querystring for Delete? I'm sure it's pretty simple, but I don't have the knowledge on this.

    Thanks,
    James W.

  • #2
    take a look at this thread
    http://forum.springframework.org/showthread.php?t=11086

    it describes a bit of code which can be found at
    http://lostinlondon.blogs.com/lil_te...ontroller.java
    and
    http://lostinlondon.blogs.com/lil_te...eResolver.java

    which deals with doing different actions on one form

    Stuart
    Last edited by robyn; May 19th, 2006, 05:32 AM.

    Comment


    • #3
      Thanks Stuart,

      I had seen that thread, not with the completed code though. It is certainly good stuff, and it looks like I'll have to go that route. I was hoping my problem was just in the HTML/JSP. <shrug>

      Thanks,
      James

      Comment


      • #4
        So there's no way to refer to the value that is inside of an input or select box? What's I'd really like to do is create a hidden input field within the delete form and set it's value to the value in the selection box. No go?

        Thanks,
        James

        BTW - I wonder if your SimpleMultiActionController could be included within Spring. I really do like it for other forms.

        Comment


        • #5
          There's nothing to stop you having two submit buttons with different names and/or values in the same form. Then the value of your selection would be sent with both. Test in your controller which button was pressed, and take the appropriate action.

          Comment


          • #6
            Chris,
            that is what the SimpleMultiActionFormController does, it invokes a different validator and form submission method depending on which submit parameter was passed in.

            I can kind of see what JamesW is after and can be solved with javascript, but it would be easier to just use a single controller with a MultiAction parameter, set the action to "editDepartment.htm?mode=delete" and just have a single form.

            SimpleMultiActionFormController is freely available to add to spring if its useful.

            Comment


            • #7
              Hey Stuart,

              Yeah, I've had my eye on that SimpleMultiActionController since it was first posted. I think that is going to be the best way to go, since I'll have to end up using it several times. If I have any questions, would you mind if I posted them here?

              Thanks,
              James

              Comment


              • #8
                Hey Stuart,

                Okay, after looking at the previous topic again, I've decided that my form is going to be simpler. However, I don't fully understand how the controller would be written with your code. All I saw was James Smith's example of a controller extending his SimpleMultiActionController.
                Could you provide some example code of one extending your code?

                Thanks,
                James Winans

                Comment


                • #9
                  JamesGSmith code was based on mine and I have merged it into one now.

                  I dont think it is really necessary to use the SimpleMultiActionFormController because you really are not doing much with the form command objects binds you only have one parameter you are interested in. So there is easier ways to do this.

                  But as an example using MultiActionController with a submit parameter resolver, ill give it a go

                  Code:
                  public class JamesWExampleMultiActionFormController extends MultiActionFormController &#123;
                  
                      protected Object formBackingObject&#40;HttpServletRequest request&#41;
                      throws Exception &#123;
                   
                          //form whatever the backing object is
                         Object obj = new Object&#40;&#41;; 
                          
                         return obj;
                      &#125; 
                      
                      protected Map referenceData&#40;HttpServletRequest request, Object command, Errors errors&#41; throws Exception &#123;
                          Map model = new HashMap&#40;&#41;;
                      
                          Set deps = businessLogic.getAllDepartments&#40;&#41;;
                          model.put&#40;"departments", deps&#41;;
                        
                          return model;
                       &#125; 
                      
                     private IDepartmentBusiness businessLogic;
                     
                     public ModelAndView deleteDepartment&#40;HttpServletRequest request, Object command, BindException errors&#41; throws Exception &#123;
                         String departmentId = request.getParameter&#40;"departmentId"&#41;;
                         
                         businessLogic.deleteDepartment&#40;Integer.parseInt&#40;departmentId&#41;&#41;;
                         
                         return null;
                     &#125;
                     
                     public ModelAndView addDepartment&#40;HttpServletRequest request, Object command, BindException errors&#41; throws Exception &#123;
                         String departmentId = request.getParameter&#40;"departmentId"&#41;;
                         
                         businessLogic.addDepartment&#40;&#41;;
                         
                         return null;
                     &#125;
                  
                     public ModelAndView editDepartment&#40;HttpServletRequest request, Object command, BindException errors&#41; throws Exception &#123;
                         String departmentId = request.getParameter&#40;"departmentId"&#41;;
                         
                         Map model = new HashMap&#40;&#41;;
                         model.put&#40;"departmentId", departmentId&#41;;
                         
                         return new ModelAndView&#40;new RedirectView&#40;"editDepartment.htm"&#41;, model&#41;;
                     &#125;
                     
                  &#125;
                  Code:
                  <bean id="submitActionParamResolver" class="com.lakeside.springframework.web.servlet.mvc.multiaction.SubmitParameterPropertiesMethodNameResolver">
                         <property name="mappings">
                            <props>
                              <prop key="_addDepartment">addDepartment</prop>
                              <prop key="_deleteDepartment">deleteDepartment</prop>
                              <prop key="_editDepartment">editDepartment</prop>
                           </props>
                         </property>
                     </bean> 
                  
                  <bean id="exampleFormController" class="com.lakeside.springframework.web.servlet.mvc.multiaction.JamesWExampleMultiActionFormController ">
                        <property name="methodNameResolver"><ref bean="submitActionParamResolver"/></property>
                        <property name="sessionForm"><value>true</value></property>
                        <property name="commandName"><value>command</value></property>
                        <property name="formView">listDepartments<value></value></property>
                     </bean>
                  Code:
                  <%@ include file="/WEB-INF/jsp/spring/IncludeTop.jsp" %>
                  
                  <P>
                  <H2>Simple Entities&#58;</H2>
                  <TABLE border="1">
                     <TR><FORM method="POST" action="<c&#58;url value="listDepartments.htm"/>">
                        <TD><B>Department&#58;</B>
                              <SELECT name="departmentId" id="departmentId">
                                  <c&#58;forEach var="department" items="$&#123;departments&#125;">
                                    <c&#58;if test="$&#123;department.id == department.id&#125;">
                                      <OPTION selected="<c&#58;out value="$&#123;model.department.id&#125;"/>"
                                          value="<c&#58;out value="$&#123;department.id&#125;"/>">
                                             <c&#58;out value="$&#123;department.name&#125;"/></OPTION>
                                    </c&#58;if>
                                    <c&#58;if test="$&#123;model.department.id != department.id&#125;">
                                          <OPTION value="<c&#58;out value="$&#123;department.id&#125;"/>">
                                          <c&#58;out value="$&#123;department.name&#125;"/></OPTION>
                                    </c&#58;if>
                               </c&#58;forEach>
                           </SELECT></TD>
                          <TD><INPUT type="submit" name="_editDepartment" value="Edit"/></TD>
                          <TD>
                             <INPUT type="submit" name="_addDepartment" value="Add"/></TD>
                          <TD>
                             <INPUT type="submit" name="_deleteDepartment" value="Delete"/>
                  </FORM></TD>
                      </TR>
                  </TABLE>
                  <P>
                  <BR>
                  
                  <%@ include file="/WEB-INF/jsp/spring/IncludeBottom.jsp" %>
                  something like this may work for you

                  Comment


                  • #10
                    Hey,

                    The solution was actually pretty simple. All I needed to do was extend AbstractController -- adding a view for each of my actions...

                    Code:
                    public class MultiSubmitActionController extends AbstractController&#123;
                    
                        private String editView;
                        private String addView;
                        private String deleteView;
                        
                        public void setAddView&#40;String addView&#41; &#123;
                            this.addView = addView;
                        &#125;
                        public void setDeleteView&#40;String deleteView&#41; &#123;
                            this.deleteView = deleteView;
                        &#125;
                        public void setEditView&#40;String editView&#41; &#123;
                            this.editView = editView;
                        &#125;
                       
                        protected ModelAndView handleRequestInternal&#40;HttpServletRequest request, 
                              HttpServletResponse response&#41; throws Exception &#123;
                            
                            Long id = null;
                            
                            if&#40;request.getParameter&#40;"id"&#41;!=null&#41;&#123;
                                id = new Long&#40;request.getParameter&#40;"id"&#41;&#41;;
                            &#125;
                    
                            if&#40;request.getParameter&#40;"_edit"&#41;!=null&#41;&#123;
                                return new ModelAndView&#40;editView, "id", id&#41;;
                            &#125;
                            if&#40;request.getParameter&#40;"_add"&#41;!=null&#41;&#123;
                                return new ModelAndView&#40;addView&#41;;
                            &#125;
                            if&#40;request.getParameter&#40;"_delete"&#41;!=null&#41;&#123;
                                return new ModelAndView&#40;deleteView, "id", id&#41;;
                            &#125;
                            
                            return new ModelAndView&#40;"selectEntityRedirect"&#41;;
                        &#125;
                        
                        
                    &#125;
                    Then in my servlet context, I plug in a RedirectView for each action...

                    Code:
                    <bean id="departmentAction" 
                              class="com.sccc.locknload.web.spring.namedEntities.MultiSubmitActionController">
                    		<property name="editView"><value>departmentEditRedirect</value></property>
                    		<property name="addView"><value>departmentAddRedirect</value></property>
                    		<property name="deleteView"><value>departmentDeleteRedirect</value></property>
                    </bean>
                    Each RedirectView points to each of the following controllers through the UrlMapping...

                    Code:
                    <bean id="editDepartmentForm" class="com.sccc.locknload.web.spring.namedEntities.department.EditDepartmentForm">
                            <property name="formView"><value>departmentForm</value></property>
                            <property name="successView"><value>selectEntityRedirect</value></property>
                    		<property name="locknload"><ref bean="locknload"/></property>
                    	</bean>
                    	
                    	<bean id="addDepartmentForm" class="com.sccc.locknload.web.spring.namedEntities.department.AddDepartmentForm">
                    		<property name="formView"><value>departmentForm</value></property>
                    		<property name="successView"><value>selectEntityRedirect</value></property>
                    		<property name="locknload"><ref bean="locknload"/></property>
                    	</bean>
                    	
                    	<bean id="deleteDepartmentForm" class="com.sccc.locknload.web.spring.namedEntities.department.DeleteDepartmentForm">
                    		<property name="formView"><value>deleteDepartmentForm</value></property>
                    		<property name="successView"><value>selectEntityRedirect</value></property>
                    		<property name="locknload"><ref bean="locknload"/></property>
                    </bean>
                    Finally, in the JSP, which is actually composed of several of these, the form just calls the url that is mapped to its MultiSubmitActionController - In this case /departmentAction.htm ....

                    Code:
                    <TABLE border="1">
                    	<TR valign="top"><FORM method="GET" action="<c&#58;url value="/departmentAction.htm"/>">
                    		<TD align="right"><B>Departments&#58;</B></TD>
                    		<TD<SELECT name="id">
                          <c&#58;forEach var="department" items="$&#123;model.departmentTypes&#125;">
                            <c&#58;if test="$&#123;model.department.id == department.id&#125;">
                              <OPTION selected="<c&#58;out value="$&#123;model.department.id&#125;"/>" value="<c&#58;out value="$&#123;department.id&#125;"/>"><c&#58;out value="$&#123;department.name&#125;"/></OPTION>
                            </c&#58;if>
                            <c&#58;if test="$&#123;model.department.id != department.id&#125;">
                              <OPTION value="<c&#58;out value="$&#123;department.id&#125;"/>"><c&#58;out value="$&#123;department.name&#125;"/></OPTION>
                            </c&#58;if>
                          </c&#58;forEach>
                        </SELECT></TD>
                      		<TD><INPUT type="submit" name="_edit" value="Edit"/></TD>
                      		<TD><INPUT type="submit" name="_add" value="Add"/></TD>
                      		<TD><INPUT type="submit" name="_delete" value="Delete"/></TD></FORM>
                        </TR>
                    </TABLE>
                    This allows me to have any number of forms on one JSP page using multiple actions. I am using this page as a manager for all of the simple named entities that popluate drop-down boxes in my application.
                    So on one page, I can have a table that has a drop-down on each row filled with things like Department names, Title names, Status types - each allowing for add, edit, and delete funcationality.
                    I hope that helps anyone who may have a similar problem. Let me know if it wasn't clear enough. Thanks for the help everyone.

                    Take care,
                    James Winans

                    Comment


                    • #11
                      see no problem. i said there were easier ways to do it.

                      also you could use
                      Code:
                      WebUtils.hasSubmitParameter&#40;request, "_edit"&#41;
                      which can deal with image buttons as well.

                      Comment


                      • #12
                        Hey, very cool. I'll go that route. And I know that I'll still need that SimpleMultiActionController eventually.

                        -James

                        Comment

                        Working...
                        X