2014-09-06 54 views
0

我正在使用Spring MVC,我試圖將HashMap和ArrayList值傳遞給我的視圖文件。但我找不到達到此目的的方法。傳遞HashMap和ArrayList值來查看文件

你能幫我嗎?

我的控制器Mehtod

@RequestMapping(value="/do_register", method= RequestMethod.GET) 
public ModelAndView RegistrationForm(@ModelAttribute Subscriber subscriber, BindingResult result) 
{ 

    HashMap<Integer, String> interest = new HashMap<Integer, String>(); 
    interest.put(1,"Java"); 
    interest.put(2,"PHP"); 
    interest.put(3, "Both"); 

    List<City> myCities = subService.getCity(); 

// I want to pass both "myCities" and "interest" to my view File 
return new ModelAndView("regForm", "records", " "); 

} 

形式

<c:url var="action" value="/register" ></c:url> 
<form:form action="${action}" modelAttribute="subscriber" method="POST" > 

    <div> 
    <label>City</label> 
    <form:select path="city"> 
    <c:forEach items="${records}" var="city"> 
      <option value="${city.cityId}">${city.cityName}</option>    
    </c:forEach> 
    </form:select> 
    </div> 

    <div> 
    <label>Interests</label> 
    <form:checkboxes path="interest" items="${records.interests}"/>  
    </div> 

    <input type="submit" value="Submit"> 
</form:form> 

回答

2

一個ModelAndView包含Model這是一種Map。它可以包含儘可能多的對象。簡單地修改您的代碼,如:

@RequestMapping(value="/do_register", method= RequestMethod.GET) 
public ModelAndView RegistrationForm(@ModelAttribute Subscriber subscriber, BindingResult result) 
{ 

    ... 
    // I want to pass both "myCities" and "interest" to my view File 
    ModelAndView mav = ModelAndView("regForm"); 
    mav.addAttribute("interests", interest); 
    mav.addAttribute("records", myCities); 
    return mav; 
} 

而且你${records}${interests}

發現在JSP你的對象