Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring MVC和请求属性_Java_Jsp_Servlets_Spring Mvc_Controller - Fatal编程技术网

Java Spring MVC和请求属性

Java Spring MVC和请求属性,java,jsp,servlets,spring-mvc,controller,Java,Jsp,Servlets,Spring Mvc,Controller,我认为最初的问题令人困惑 我有一个HashMap,它需要是一个来自数据库的集合,我想通过Spring控制器将它发送到视图。我不想将此HashMap放在model.addAttribute()中,因为Spring模型对象返回一个映射,而我的JSP需要集合是一个集合。如果我在request.setAttribute中设置了HashMap.values(),那么如果我的方法返回字符串,如何将该请求变量分派到视图中 @RequestMapping(method = RequestMethod.GET)

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

我有一个HashMap,它需要是一个来自数据库的集合,我想通过Spring控制器将它发送到视图。我不想将此HashMap放在model.addAttribute()中,因为Spring模型对象返回一个映射,而我的JSP需要集合是一个
集合
。如果我在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";
}
@RequestMapping(method=RequestMethod.GET)
公共字符串主页(区域设置、模型模型、HttpServletRequest){
model.addAttribute(“surveys”,mySurveys);//这是一张地图,我需要一个集合
//所以我想这样做,但是如果我不分派它(如下面所示),如何在视图中访问“evaluations”对象呢??
setAttribute(“评估”,mySurveys);
//RequestDispatcher rd=request.getRequestDispatcher(“pathToResource”);
//转发(请求、响应);
返回“家”;
}
编辑:Spring标记库不能用于此特定用例


谢谢。

我想你对什么是
ModelMap
感到困惑

您可以通过
@modeldattribute
在视图中注释想要访问的任何变量,Spring将自动实例化该变量,并将其添加到
ModelMap
。在视图中,您可以像这样使用它:

<form:form modelattribute="myAttribute">
    <form:input path="fieldInAttribute">
</form:form>


希望这能回答您的问题

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

在下面的代码中,surveys是对象的集合,可以通过${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";
}
@RequestMapping(method=RequestMethod.GET)
公共字符串home(ModelMap ModelMap、HttpServletRequest){
Map mySurveys=getMySurveys();
addAttribute(“surveys”,mySurveys.values());
返回“家”;
}

我不打算使用Spring标记,但我正在使用另一个拥有自己的api和标记库的框架。我必须使用${variable}语法来显示它。我发布了我想做的事情,你可以使用Spring MVC,但不能使用Spring Taglibs,它在Spring MVC jar中-为什么不可以?我可以使用它,但我使用的TagLib适合我们正在使用的系统接口。用户希望它以某种方式显示。因此,如果您使用addAttribute将其添加到模型中,您不能仅通过home.jsp中的${surveys}访问它吗?我得到一个异常,因为标记(构建数据表)需要一个集合对象而不是映射。您能显示声明和实例化mSurveys的代码吗?谢谢@Matt。我在你们发帖前一个小时意识到了这一点,是的,我需要的是模型,而不是模型地图。抢手货