将模型对象从spring控制器传递给jsp

时间:2014-06-25 08:05:56

标签: java spring jsp spring-mvc

我正在尝试将模型对象从spring控制器传递给jsp。但是对象不在目标页面上呈现。

控制器

    @Path("test");
    public ModelandView gettest(@Context HttpServletRequest request) {
            ModelandView responseView = new ModelandView(new JsonView());
            //some code here
            if (somecondition) {
                responseView.setViewName("track/trackvehicle");
                responseView.addObject("JSONdata", vehicleID);
            }
            else {
                System.out.println("Not present");
            }
            return responseView;
        }

trackvehicle.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <input type="text" id="test_id" value="${JSONdata}"/>

但文本框不会使用任何数据呈现。有什么问题?

2 个答案:

答案 0 :(得分:1)

将您的代码更改为:

@Controller
public class YourController {    
    @RequestMapping("test")
    public ModelAndView gettest() {
        //some code here
        if (somecondition) {
            return new ModelAndView("track/trackvehicle", "JSONdata", vehicleID);
        }

        System.out.println("Not present");
        return new ModelAndView("track/trackvehicle");
    }
}

答案 1 :(得分:1)

你在评论部分提到它的ajax基于spring mvc ...

你如何击中这个ajax网址....

对于弹簧mvc中的ajax调用使用@ResponseBody注释并使用ajax方法点击url,如经典ajax或jquery ajax或jquery获取函数并加载值。

希望以下链接能为您提供更清晰的图片

Returning ModelAndView in ajax spring mvc

How to render a View using AJAX in Spring MVC

相关问题