gpt4 book ai didi

java - Spring MVC 和请求属性

转载 作者:行者123 更新时间:2023-11-30 06:28:28 25 4
gpt4 key购买 nike

我认为最初的问题令人困惑。

我有一个 HashMap,它需要是来自数据库的集合,我想通过 Spring Controller 将其发送到 View 。我不想将此 HashMap 放入 model.addAttribute() 中,因为 Spring Model 对象返回一个 Map 而我的 JSP 需要集合为 Collection<Object> .如果我在 request.setAttribute 中设置我的 HashMap.values(),如果我的方法返回一个字符串,我该如何将该请求变量分派(dispatch)给 View ?

@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 库不能用于这个特定的用例。

谢谢。

最佳答案

如果 mySurveys 是一个 map ,那么也许您可以将 mySurveys.values() 放入 ModelMap 而不是 mySurveys(另外,您是否打算使用 ModelMap 而不是模型?)

在下面的代码中,调查将是一个对象集合,并且可以通过 ${surveys}

在 jsp 中访问
@RequestMapping(method = RequestMethod.GET)
public String home(ModelMap modelMap, HttpServletRequest request) {

Map<String,Object> mySurveys = getMySurveys();
modelMap.addAttribute("surveys", mySurveys.values());
return "home";
}

关于java - Spring MVC 和请求属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12519276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com