Results 1 to 9 of 9

Thread: checkbox element

  1. #1
    Join Date
    Aug 2006
    Posts
    9

    Default checkbox element

    Dear all,
    I develope my project using spring and this is the first time to use it and i have a problem with checkbox.
    the situation as follow...
    i have jsp page displayes the result of certain search so the result is dynamic may be on or more elements each element have a checkbox. Then the user can check one or more elments to save these elements to his contacts(on database).
    how to get the checked elements ?


    thanks

    Moh

  2. #2
    Join Date
    Apr 2006
    Posts
    17

    Default

    Hi Moh,

    You need to put a hidden value in your JSP with an underscore before its name e.g.
    Code:
    <input type="hidden" name="_${status.expression}" />
    The controller then knows to look for a parameter of the same name without the underscore and will populate your command bean as normal for that name.

    Then, wherever you retrieve the data, you can just iterate around the collection and check which of the items are set to true.

  3. #3
    Join Date
    Aug 2006
    Posts
    9

    Default

    Hi all,
    I have the following code and it gives me an exception can any one help me!
    this is the bean for my checkbox.
    Code:
    package com.egysol.beans;
    public class Checkbox {
    	private boolean checkbox;
    	public boolean isCheckbox() {
    		return checkbox;
    	}
    	public void setCheckbox(boolean checkbox) {
    		this.checkbox = checkbox;
    	}
    }
    //command for all checkboxs ..
    package com.egysol.beans;
    public class MyCheckBoxs {
    	private Checkbox [] checkbox;
    	public Checkbox[] getCheckbox() {
    		return checkbox;
    	}
    	public void setCheckboxs(Checkbox[] checkbox) {
    		this.checkbox = checkbox;
    	}
    }
    in the Application-servlet.xml i define the the controller bean and add command to its properties as
    Code:
    <property name="commandName">
    			<value>combobox</value>
    		</property>
    		<property name="commandClass">
    			<value>com.egysol.beans.MyCheckBoxs</value>
    		</property>
    and in the jsp file i bind the command as follow..
    Code:
    <c:forEach var="eachElement"  items="${data}" varStatus="status">
    
    	<tr>
    	 <spring:bind path="combobox">
    	  <td class="style5">
    	  	<input type="hidden" name="_<c:out value='${status.expression}'/>" />
    		<input type="checkbox" name="<c:out value='${status.expression}'/>" value="true"  <c:if test="${status.value}">checked</c:if> />
    	  </td>
    	  </spring:bind>
      </tr>
    	
    </c:forEach>
    but when running this code it gives me the follwing exception

    2006-08-07 12:53:42,828 ERROR [org.springframework.web.servlet.tags.BindTag] - <Neither Errors instance nor plain target object for bean name 'combobox' available as request attribute>
    javax.servlet.jsp.JspTagException: Neither Errors instance nor plain target object for bean name 'combobox' available as request attribute
    at org.springframework.web.servlet.tags.BindTag.doSta rtTagInternal(BindTag.java:118)
    at org.springframework.web.servlet.tags.RequestContex tAwareTag.doStartTag(RequestContextAwareTag.java:6 8)
    .
    .
    .
    .
    .
    what is the problem?
    and is this the right way for using spring ?

    Thanks
    M Elgamal

  4. #4
    Join Date
    Mar 2006
    Location
    Hamburg, Germany
    Posts
    24

    Default

    Hi,

    try the following:
    Code:
    <c:forEach items="${combobox.checkbox}" var="item" varStatus="counter">
      <tr><td class="style5">
        <spring:bind path="combobox.checkbox[${counter.index}].checkbox">
          <input type="hidden" name="_${status.expression}"/>
          <input type="checkbox" name="${status.expression]" value="true"<c:if test="${status.value}"> checked="checked"</c:if>/> 
        </spring:bind>
      </td></tr>
    </c:forEach>
    1. You assigned the "combobox" to the "commandName" property. So if you want to get the Checkbox array you need to refer to it by "combobox.checkbox
    2. For Spring to bind correctly you need a path like "combobox.checkbox[2].checkbox" and the new bind tag does just that.
    3. You should really think about your class and property names. The Checkbox class is unneccesary in this case as a array of booleans woud do as well


    I hope I got it all right.

  5. #5
    Join Date
    Aug 2006
    Posts
    9

    Default

    Thanks dodger for your replay,

    may I want to itrate for the map ${data} which contain my result and of cource number of checkboxs.
    please if u have any tutorials about spring binding send me it..

    regards..

  6. #6
    Join Date
    Mar 2006
    Location
    Hamburg, Germany
    Posts
    24

    Default

    I'm terribly sorry but I don't understand this part:

    may I want to itrate for the map ${data} which contain my result and of cource number of checkboxs.

  7. #7
    Join Date
    Aug 2006
    Posts
    9

    Default

    Hi,
    I tryed this code may it didnt work, it gives the same error as above
    Code:
    <c:forEach var="eachElement" items="${data.members}" varStatus="counter">
    	<tr>
    		<td class="style5">
    		<spring:bind path="combobox.checkbox[${counter.index}].checkbox">
    			<input type="hidden" name="_${status.expression}"/>
         		<input type="checkbox" name="${status.expression}" value="true"<c:if test="${status.value}"> checked="checked"</c:if>/> 
    		</spring:bind>
    		</td>
    		<c:forEach var="vElement" items="${eachElement.value}" >
    			<td class="style5">&nbsp;	<c:out value="${vElement}"/> </td>
    		</c:forEach>
    	</tr>
     </c:forEach>

  8. #8
    Join Date
    Mar 2006
    Location
    Hamburg, Germany
    Posts
    24

    Default

    Quote Originally Posted by java
    Hi, I tryed this code may it didnt work, it gives the same error as above
    Okay but you are not helping here...


    Why do you want to use ${data.members} what did you put there and when? Why not ${combobox} (as you named your command object "combobox") and what happens when you use the code I posted?

  9. #9
    Join Date
    Aug 2006
    Posts
    9

    Default

    ok ..
    this map ${data.members} contains my data in two keys one is "members" its value is the members names,phone,address,.... etc, and the other is "contacts" used in other pace of the page. i want to display data.members on the jsp page with checkboxs, so that i can select some members and add them to database.
    also this map passes from the controller to the view page as follow
    Code:
    return new ModelAndView ("MyJspPage","data",data)
    the nomber fo checkbox is dynamic, according to the no of elements on ${data.members}, so i used forEach loop with items=${data.members} to determin checkboxs nomber and there.

    i hope that i can describe the problem.
    thanks for your patient.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •