Announcement Announcement Module
Collapse
No announcement yet.
Send model attribute as list of objects Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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)

    model
    Code:
    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:

    jsp
    Code:
    <form:form modelAttribute="downloadAsZip" action="download.do" 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 }"/>
        </c:forEach>
        <input type="submit" value="<spring:message code="download.downloadzip" />" />
        </form:form>
    the important thing is here, I'd like to have this
    Code:
    @Controller
    @RequestMapping("/download.do")
    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)

    Comment


    • #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}

      Comment


      • #4
        Hello,

        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.

        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 :
        Code:
        @Component
        @Entity
        @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 :
        Code:
        @Component
        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;
        	}
        }
        Controller:
        Code:
        @Controller
        @RequestMapping("/subscription.html")
        public class CustomerQPackFormController {
        	@RequestMapping(method=RequestMethod.GET)
        	public String setupForm( Model model) {
        		String id = Constants.QUESTION_PACK_ID;
        		ListObject <CustomerQuestionPack> listObject = new ListObject <CustomerQuestionPack>();
        		List <CustomerQuestionPack> customerQuestionPacks = customerQuestionPackManager.getAllCustomerQuestionPack(id);
        		listObject.setList(customerQuestionPacks);
        		model.addAttribute(listObject);
        		return "users/subscription";	 
        	}
        	
        	@RequestMapping(method=RequestMethod.POST)
        	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) iter.next();
                    	if(custQPack.isSelected()) {
                                System.out.println("custQPack is selected for id: " + custQPack.getName());
                    	}
                }
        		return "redirect:/payment.html";
        	}
        JSP:
        Code:
        <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/>
                </c:forEach>
            </div>
            </c:if>
        </spring:bind>
        
        <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">
        	        <tr>
        	            <spring:bind path="listObject.list[${itemstatus.index}].selected">
        	                <td>
        				<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>/>
        			</td>
        			<td>
        		                <c:out value="${customerQuestionPack.name}"/>
        		        </td>
        		        </spring:bind>
        	        </tr>
                    </c:forEach>
                    <tr height="60">
                        <td><input type="submit" class="button" value="Payment >>" style="width:70px;" /></td>
                    </tr>
            </table>	               
        </form:form>

        Comment

        Working...
        X