【发布时间】:2012-09-13 05:05:15
【问题描述】:
我认为最初的问题令人困惑。
我有一个 HashMap,它需要是来自数据库的集合,我想通过 Spring 控制器发送到视图。我不想将这个 HashMap 放在 model.addAttribute() 中,因为 Spring Model 对象返回一个 Map,而我的 JSP 需要集合是 Collection<Object>。如果我在 request.setAttribute 中设置我的 HashMap.values(),如果我的方法返回一个字符串,我该如何将该请求变量分派到视图?
@RequestMapping(method = RequestMethod.GET)
public String home(Locale locale, Model model, HttpServletRequest request) {
model.addAttribute("surveys", mySurveys); //this is a map and I need a Collection<Object>
//So I'd like to do this, but how do I get to the "evaluations" object in a view if I'm not dispatching it (like below)??
request.setAttribute("evaluations", mySurveys);
//RequestDispatcher rd = request.getRequestDispatcher("pathToResource");
//rd.forward(request, response);
return "home";
}
编辑:Spring Tag 库不能用于此特定用例。
谢谢。
【问题讨论】:
-
你使用 Spring MVC 并且不能使用 Spring Taglibs,这是 in Spring MVC jar - 为什么不呢?
-
我可以使用它,但是我使用的 TagLib 适合我们正在使用的系统的接口。用户希望它看起来像某种方式。
-
所以如果你使用 addAttribute 将它添加到模型中,你不能简单地使用 home.jsp 中的 ${surveys} 访问它吗?
-
我得到一个异常,因为标签(构建数据表)需要一个 Collection 对象而不是地图。
-
你能展示声明和实例化 mSurveys 的代码吗?
标签: java jsp servlets spring-mvc controller