Results 1 to 6 of 6

Thread: <form:options - access to Map not work

Hybrid View

  1. #1
    Join Date
    Aug 2009
    Posts
    167

    Default <form:options - access to Map not work

    my controller:
    Code:
    	public ModelAndView create(HttpServletRequest request,
    			HttpServletResponse response) throws Exception {
    		ModelAndView mav = new ModelAndView("user", "commandName",
    				new UserView());
    		branchMap = branchManager.getBranchMap();
    		mav.addObject("branchMap", branchMap);
    		profileMap = profileManager.getProfileMap();
    		mav.addObject("profileMap", profileMap);
    		permissionMap = permissionManager.getPermissionMap();
    		mav.addObject("permissionMap", permissionMap);
    		return mav;
    	}
    my jsp:
    HTML Code:
    				<tr>
    					<td><spring:message code="branch" />*</td>
    					<td><label><form:select path="branchId">
    						<form:option value="0" label="Select" />
    						<form:options items="${branchMap}" itemValue="id" itemLabel="code" />
    					</form:select></label></td>
    				</tr>
    Error:
    Code:
    Aug 30, 2009 4:57:16 PM org.springframework.web.servlet.tags.RequestContextAwareTag doStartTag
    SEVERE: Invalid property 'id' of bean class [java.lang.Integer]: Bean property 'id' is not readable or has an invalid getter method: Does th
    e return type of the getter match the parameter type of the setter?
    org.springframework.beans.NotReadablePropertyException: Invalid property 'id' of bean class [java.lang.Integer]: Bean property 'id' is not r
    eadable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
            at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:540)
            at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:532)
            at org.springframework.web.servlet.tags.form.OptionWriter.renderFromMap(OptionWriter.java:164)
            at org.springframework.web.servlet.tags.form.OptionWriter.writeOptions(OptionWriter.java:135)
            at org.springframework.web.servlet.tags.form.OptionsTag.writeTagContent(OptionsTag.java:157)
            at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:90)
            at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:77)
            at org.apache.jsp.WEB_002dINF.jsp.user_jsp._jspx_meth_form_005foptions_005f0(user_jsp.java:1307)
            at org.apache.jsp.WEB_002dINF.jsp.user_jsp._jspService(user_jsp.java:300)
            at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
            at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
            at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
            at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
            at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
            at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
            at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
            at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
            at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:236)
            at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:257)
            at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1183)
            at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:902)
            at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
            at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
            at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
            at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
            at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
            at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
            at java.lang.Thread.run(Thread.java:619)

  2. #2
    Join Date
    Aug 2009
    Posts
    167

    Default

    if I use this:
    HTML Code:
    				<tr>
    					<td><spring:message code="branch" />*</td>
    					<td><label><form:select path="branchId">
    						<form:option value="0" label="Select" />
    						<form:options items="${branchMap}" />
    					</form:select></label></td>
    				</tr>
    then no errors, but in the combo box show all values of key. But I want to show only value "code" in the combo box

  3. #3
    Join Date
    Aug 2009
    Posts
    167

    Default

    this work:
    HTML Code:
    <form:select path="branchId">
    	<c:forEach items="${branchMap}" var="branch">
    		<form:option value="${branch.value.id}"
    			label="${branch.value.code}" />
    	</c:forEach>
    </form:select>
    but is this a good solution?

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    The key of the map is automatically used as the value and the object in the map (belonging to the key) is used as the label. You could specify a property to use. So only specifying a label should be enought to get the form:options tag to work.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Aug 2009
    Posts
    167

    Default

    Quote Originally Posted by Marten Deinum View Post
    The key of the map is automatically used as the value and the object in the map (belonging to the key) is used as the label. You could specify a property to use. So only specifying a label should be enought to get the form:options tag to work.
    Code:
    <td><spring:message code="branch" />*</td>
    					<td><label><form:select path="branchId">
    						<form:options items="${branchMap}"
    							itemLabel="${branchMap.value.code}" />
    					</form:select></label></td>
    error:
    Code:
    org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/jsp/user.jsp at line 41
    
    38: 				<tr>
    39: 					<td><spring:message code="branch" />*</td>
    40: 					<td><label><form:select path="branchId">
    41: 						<form:options items="${branchMap}"
    42: 							itemLabel="${branchMap.value.code}" />
    43: 					</form:select></label></td>
    44: 				</tr>
    
    
    Stacktrace:
    	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
    	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
    	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:236)
    	org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:257)
    	org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1183)
    	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:902)
    	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
    	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
    	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    
    
    root cause 
    java.lang.IllegalArgumentException: 'itemLabel' must not be empty
    	org.springframework.util.Assert.hasText(Assert.java:162)
    	org.springframework.web.servlet.tags.form.OptionsTag.setItemLabel(OptionsTag.java:107)
    	org.apache.jsp.WEB_002dINF.jsp.user_jsp._jspx_meth_form_005foptions_005f0(user_jsp.java:1293)
    	org.apache.jsp.WEB_002dINF.jsp.user_jsp._jspx_meth_form_005fselect_005f0(user_jsp.java:1259)
    	org.apache.jsp.WEB_002dINF.jsp.user_jsp._jspx_meth_form_005fform_005f0(user_jsp.java:361)
    	org.apache.jsp.WEB_002dINF.jsp.user_jsp._jspService(user_jsp.java:146)
    	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:236)
    	org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:257)
    	org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1183)
    	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:902)
    	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
    	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
    	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Sigh... I again suggest a book...

    Code:
    <td><spring:message code="branch" />*</td>
    <td>
    <label>
    	<form:select path="branchId">
    		<form:options items="${branchMap}" itemLabel="code" />
    	</form:select>
    </label>
    </td>
    Quote Originally Posted by Reference Guide
    Alternatively, you may specify a Map of items, in which case the map keys are interpreted as option values and the map values correspond to option labels. If itemValue and/or itemLabel happen to be specified as well, the item value property will apply to the map key and the item label property will apply to the map value.
    It is all in the reference guide which for some reason you refuse to read...
    Last edited by Marten Deinum; Aug 31st, 2009 at 04:05 AM. Reason: Added quote from reference guide
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

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