Announcement Announcement Module
Collapse
No announcement yet.
How actually form:select and form:options render 'selected' attribute? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How actually form:select and form:options render 'selected' attribute?

    I've got two DTO beans: Route and Worker. The relation is one to one, but i have to provide possibility of 'null' selection (No worker assigned to work on route).

    Init binder looks like this:

    Code:
    @InitBinder
        public void initBinder(WebDataBinder binder) {
    		final WorkerDao workerDao0 = workerDao;
    		binder.registerCustomEditor(Worker.class, 
    				new PropertyEditorSupport() 
    		{
    	        @Override
    	        public void setAsText(String text) {
    	        	Object value = null;
    	        	try {
    	        		Long id = Long.valueOf(text);
    	        		value = workerDao0.findById(id);
    	        	} catch (Exception e) {
    	        		/*swallow*/
    				}
    	        	setValue(value);
    	        }
    	        
    	        @Override
    	        public String getAsText() {
    	        	Object value = getValue();
    	        	String retVal = null;
    				if (value != null) {
    					retVal = "" + ((Worker)value).getId();
    				} else {
    					retVal = "";
    				}
    				return retVal;
    	        }
    	    });
    	}
    This select control is basically working (I can change foreign key value in the db), but it does not render a 'selected' attribute. Also, I'd like to provide 'null' choice in this control.

    Code:
    <form:select path="worker" itemValue="id" itemLabel="description" items="${workerList}"/>

  • #2
    I've went through 'select' tag rendering workflow with debugger and got some understanding on field comparation algorithm used in tag renderer. I've overrided "equals" method in DTO beans and made jsp code look like this.

    Code:
    <form:form method="post" modelAttribute="route" action="${confirmAction}">
        <form:select path="worker">
           <form:option value="" label="--//--" />
           <form:options items="${workerList}" itemLabel="firstName" />
        </form:select>
    </form:form>
    Now it renders 'selected' tag. However, i'd like to use expression in itemLabel. Say, firstName + " " + lastName. Is it possible?

    Comment


    • #3
      Got it working... finally. Dozens of 'forEach' attempts were failed yesterday, so i've overlooked this construct.

      Code:
      <form:select path="worker">
      	<form:option value="" label="--//--"></form:option>
      	<c:forEach items="${workerList}" var="w">
      		<form:option value="${w}" label="${w.firstName} ${w.lastName}" />
              </c:forEach>
      </form:select>
      Thanks for your assistance.

      Comment

      Working...
      X