我正在使用Spring MVC,并且试图将HashMap和ArrayList值都传递给我的视图文件。但是我找不到实现这一目标的方法。
请你帮助我好吗?
我的控制器方法
@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>
最佳答案
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中找到对象关于java - 同时传递HashMap和ArrayList值以查看文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25697558/