Code Snippet

Just another Code Snippet site

[Velocity/Spring] Bind list of data

View :

<form action="testSubmit2.html" method="POST">
            
    #springBind("wrapper")

    #if(!$wrapper) 
        no wrapper
    #end

    #if(!$wrapper.listOfData) 
        no data
    #end

    List Size : $wrapper.listOfData.size()

    #foreach($data in $wrapper.listOfData)
        #set($idx = $foreach.index)
        <h2>$idx</h2>
        #springFormInput("wrapper.listOfData[$idx]" "style=width:300px;")   
        
    #end

    <input type="submit" id="submit" value="submit" >

</form>

Controller :

    @RequestMapping(value = { "test2.html" }, method = RequestMethod.GET)
    public final String displayTest2(final ModelMap model) throws Exception {
        LOGGER.info("Add wrapper (with data) to model");

        Wrapper wrapper = new Wrapper();
        // At least one element must be present in the list to allow clone operation
        wrapper.getListOfData().add(new String("AA"));

        model.addAttribute("wrapper", wrapper);

        return "view2";
    }

Wrapper & List :

public class Wrapper {

    ArrayList<String> listOfData = new ArrayList<String>();

    public List<String> getListOfData() {
        return listOfData;
    }

    public void setListOfData(ArrayList<String> listOfData) {
        this.listOfData = listOfData;
    }
}

,


Comments are currently closed.