Results 1 to 7 of 7

Thread: form:option problem

  1. #1
    Join Date
    Oct 2007
    Posts
    10

    Default form:option problem

    Hi,
    I am using form:option tag to show some option values in Drop Down menu.
    While iterating over collection of Entry type objects.(java.util.Map.Entry)

    where Entry<Key,Value> is <Integer,Object>.

    Code:
    <c:forEach items="${collection}" var="tmp" >
       <form:option value="${tmp.key}"  label="${tmp.value.desc}"/>
    </c:forEach>
    it show no value for options.
    Code:
    <option value="">Option Label 1</option>
    <option value="">Option Label 2</option>
    <option value="">Option Label 3</option>
    <option value="">Option Label 4</option>
    But in this case.
    Code:
    <c:forEach items="${collection}" var="tmp" >
       <c:out value="${tmp.key}" />:<c:out  value="${tmp.value.desc}"/>
    </c:forEach>
    Result is OK as expected.
    Code:
    1:Option Label 1
    2:Option Label 2
    3:Option Label 3
    4:Option Label 4
    If anybody can explain why it is showing different behavior.

    Thanks in advance
    Last edited by matrix; Jan 3rd, 2008 at 12:35 PM.

  2. #2

    Default

    The form option KEY looks for the .toString() method of the key value if the source is a map. You shouldn't need to specify the key or the value if you have implemented the map the way spring wants you to.

  3. #3
    Join Date
    May 2005
    Location
    California, US
    Posts
    735

    Default

    I didn't know you could do that with a hash map. This is how I got it to work (with spring 2.5 annotations):
    controller:
    PHP Code:
    @Controller
    @RequestMapping("/post6.zug")
    public final class 
    Post6Controller {

    (
    stuff omitted)

        @
    ModelAttribute("referenceData6")
        public 
    Set<Map.Entry<StringInteger>> referenceData() {
            final 
    Map<StringIntegermap = new HashMap<StringInteger>();

            for (
    int i 16i++) {
                final 
    String optionLabel String.format("option%02d"i);

                
    map.put(optionLabeli);
            }

            
    log.debug("map: {}"map);

            return (
    map.entrySet());
        }

    (
    stuff omitted
    jsp:
    PHP Code:
    (stuff omitted)
                    <
    form:select path="selectPath">
                        <
    form:option label="select an item" value="" />
                        <
    form:options
                            items
    ="${referenceData6}"
                            
    itemValue="value"
                            
    itemLabel="key"
                        
    />
                    </
    form:select>
    (
    stuff omitted
    generated html:
    PHP Code:
                    <select id="selectPath" name="selectPath">
                        <
    option value="">select an item</option>
                        <
    option value="5">option05</option><option value="3">option03</option><option value="4">option04</option><option value="1">option01</option><option value="2" selected="selected">option02</option>
                    </
    select

  4. #4
    Join Date
    May 2005
    Location
    California, US
    Posts
    735

    Default

    I'm not sure, but this may be more like what you're doing, but I'm not using a HashMap:
    PHP Code:
        @ModelAttribute("referenceData5")
        public 
    ReferenceData5 referenceData() {
            final 
    Collection<FormOptionformOptions = new ArrayList<FormOption>();

            for (
    int i 16i++) {
                final 
    String optionLabel String.format("option%02d"i);

                final 
    FormOption formOption = new FormOption(optionLabeli);

                
    formOptions.add(formOption);
            }

            
    log.debug("formOptions: {}"formOptions);

            final 
    ReferenceData5 referenceData5 =
                new 
    ReferenceData5(formOptions);

            return (
    referenceData5);
        } 
    PHP Code:
    public final class ReferenceData5 {
        private final 
    Collection<FormOptionformOptions;

        ...

        public 
    Collection<FormOptiongetFormOptions() {
            return (
    formOptions);
        } 
    PHP Code:
    public final class FormOption {
        private final 
    String selectLabel;
        private final 
    int selectValue;

        public 
    FormOption(final String _selectLabel, final int _selectValue) {
            
    this.selectLabel _selectLabel;
            
    this.selectValue _selectValue;
        }

    (
    usual getters and setters omitted
    PHP Code:
                    <form:select path="selectPath">
                        <
    form:option label="select an item" value="" />

                        <
    form:options
                            items
    ="${referenceData5.formOptions}"
                            
    itemValue="selectValue"
                            
    itemLabel="selectLabel"
                        
    />
                    </
    form:select

  5. #5
    Join Date
    Oct 2007
    Posts
    10

    Smile Thanks for help...

    Thanks a lot...

  6. #6

    Default

    Examples above helped me with my use-case... there's another example with enum type.

    enum type declared with getLongName(), getKey() methods that will be used by the <form> tag:
    Code:
    public enum LandZoningType{
    	CONSERVATION_PASSIVE_RECREATION("CP", "Conservation/Passive Recreation"), 
    	SINGLE_FAMILITY_RESIDENTIAL_R1("R1", "Single Family Residential, large lot size"),
    	SINGLE_FAMILITY_RESIDENTIAL_R2("R2", "Single Family Residential, smaller than R-1"),
    	
    	final public String longName;
    	final public String code;
    	
    	public String getLongName(){
    		return longName;
    	}
    	
    	public String getCode(){
    		return code;
    	}
    	
    	public String getKey(){
    		return toString();
    	}
    		
    	LandZoningType(String code, String longName){
    		this.code = code;
    		this.longName = longName;
    	}
    }
    service layer creating a collection of enum type instances:
    Code:
    public  Collection<LandZoningType> getLandZoneTypes(){
    	final Collection<LandZoningType> types = new ArrayList<LandZoningType>();
    	for (LandZoningType st : LandZoningType.values()){
    		types.add(st);
    	}
    	return types; 
    }
    jsp:
    Code:
    <form:select path="landZoneType" items="${landZoneTypes}" itemValue="key" itemLabel="longName"/>

  7. #7
    Join Date
    May 2005
    Location
    California, US
    Posts
    735

    Default

    Nice.

    Nothing's more fun that optimizing someone else's code; could you do this instead
    Code:
    private final Set<LandZoningType> zoningType = EnumSet.allOf(LandZoningType.class);
    
    public  Collection<LandZoningType> getLandZoneTypes(){
        return(zoningType);
    }
    Or would it be an EnumMap?
    Code:
    private final Map<LandZoningType> zoningTypes = new EnumMap(LandZoningType.class);
    ...
    public  Collection<LandZoningType> getLandZoneTypes(){
        return(zoningTypes);
    }
    I can't remember if I've ever used an EnumMap so I don't remember how to use them so the above (either of them) may not compile (the Map is obviously missing something). And I'm not sure if the return for the latter one should instead be
    Code:
        return(zoningTypes.entrySet());
    Enums aren't used enough as far as I'm concerned.

Posting Permissions

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