Announcement Announcement Module
No announcement yet.
Send model attribute as list of objects Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Send model attribute as list of objects

    Hi everyone,

    is there a way to send to a controller a list of objects directly?

    I mean without having a container object that carries the list.

    I would like to do something like that (which does not work)

    public class RemoteFile{
    	private String originalFile;
    	private String filename;
    	private String url;
    in the jsp I get a list of remote files from the get method and I would like to be able to send this list to another controller when the user clicks a button:

    <form:form modelAttribute="downloadAsZip" action="" method="POST">
        <c:forEach items="${remoteFiles}" var="file" varStatus="loopStatus">
          <input type="hidden" name="list[${loopStatus.index}].originalFile" value="${file.originalFile }"/>
          <input type="hidden" name="list[${loopStatus.index}].filename" value="${file.filename }"/>
          <input type="hidden" name="list[${loopStatus.index}].url" value="${file.url }"/>
        <input type="submit" value="<spring:message code="download.downloadzip" />" />
    the important thing is here, I'd like to have this
    public class DownloadController {
    	@RequestMapping(method = RequestMethod.POST)
    	 public void onSubmit(@ModelAttribute("downloadAsZip") List<RemoteFile> list) {
    if someone has a solution htat would be great

  • #2
    If I understood you right, you want to take parameters and attributes from HttpServletRequest? You can do it by overriding handleRequestInternal(HttpServletRequest request, HttpServletResponse response)


    • #3
      I know I can get it from the request but I assume there is a way in spring to do it easily.

      The thing is, I want to get this to work

      onSubmit(@ModelAttribute("downloadAsZip") List<RemoteFile> list)

      I just want to know how I have to populate downloadAsZip in my jsp for it to work. And as an example, my data are in ${remoteFiles}


      • #4

        I am new to Spring. I have same issue as listed by Toinou87.

        I would like to know how to send list of objects from the controller to jsp and back from jsp to Controller either directly as listed by Toinou87 or by using container object. My jsp displays list of CustomerQuestionPacks, but I get the following error.


        org.springframework.web.util.NestedServletExceptio n: Request processing failed; nested exception is org.springframework.beans.InvalidPropertyException : Invalid property 'list[0]' of bean class [com.rheumreview.prototype.model.ListObject]: Index of out of bounds in property path 'list[0]'; nested exception is java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

        Model CustomerQuestionPack :
        @Table(name = "customer_question_pack")
        public class CustomerQuestionPack extends BaseObject {
                static final long serialVersionUID = 3L;
        	private Long id;
        	private QuestionPack questionPack;
        	private String name;
        	//For UI
        	private boolean selected = false;
        Model ListObject :
        public class ListObject<E> extends BaseObject {
        static final long serialVersionUID = 5L;
        	private List<E> list;
        	public ListObject() {}
        	public List<E> getList() {
        		return list;
        	public void setList(List<E> list) {
        		this.list = list;
        public class CustomerQPackFormController {
        	public String setupForm( Model model) {
        		String id = Constants.QUESTION_PACK_ID;
        		ListObject <CustomerQuestionPack> listObject = new ListObject <CustomerQuestionPack>();
        		List <CustomerQuestionPack> customerQuestionPacks = customerQuestionPackManager.getAllCustomerQuestionPack(id);
        		return "users/subscription";	 
        	public String processSave(Model model, @ModelAttribute("listObject") ListObject <CustomerQuestionPack> listObject) {
                List <CustomerQuestionPack> custQPacks = listObject.getList();
                for (Iterator <CustomerQuestionPack> iter = custQPacks.iterator(); iter.hasNext();) {
                    	CustomerQuestionPack custQPack =  (CustomerQuestionPack);
                    	if(custQPack.isSelected()) {
                                System.out.println("custQPack is selected for id: " + custQPack.getName());
        		return "redirect:/payment.html";
        <spring:bind path="listObject.*">
            <c:if test="${not empty status.errorMessages}">
            <div class="error">	
                <c:forEach var="error" items="${status.errorMessages}">
                    <c:out value="${error}" escapeXml="false"/><br/>
        <form:form modelAttribute="listObject"  method="post" action="subscription.html">
            <table width="70%" border="0" cellspacing="8" cellpadding="0" >
        		<c:forEach var="customerQuestionPack" items="${listObject.list}" varStatus="itemstatus">
        	            <spring:bind path="listObject.list[${itemstatus.index}].selected">
        				<input type="hidden" name="_<c:out value="${status.expression}"/>">
        				<input type="radio" name="<c:out value="${status.expression}"/>" value="true"
        				<c:if test="${status.value}">checked</c:if>/>
        		                <c:out value="${}"/>
                    <tr height="60">
                        <td><input type="submit" class="button" value="Payment >>" style="width:70px;" /></td>