简体   繁体   English

将模型对象从Spring控制器传递到JSP

[英]Pass model object from spring controller to jsp

I am trying to pass a model object from spring controller to jsp. 我试图将模型对象从spring控制器传递到jsp。 But the object is not rendering on the target page. 但是对象没有在目标页面上呈现。

Controller 控制者

    @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 trackvehicle.jsp

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

But the textbox is not rendered with any data. 但是该文本框不包含任何数据。 What is the problem? 问题是什么?

Change your code to: 将您的代码更改为:

@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");
    }
}

You have mentioned in the comment section its ajax based in spring mvc... 您已经在评论部分提到了基于Spring MVC的Ajax ...

how you are hitting this ajax url.... 你怎么打这个ajax网址。

for ajax calls in spring mvc use @ResponseBody annotation and hit the url using ajax methods like classic ajax or jquery ajax or jquery get functions and load the value. 对于spring mvc中的ajax调用,请使用@ResponseBody批注,并使用ajax方法(例如经典ajax或jquery ajax或jquery get函数)加载URL并加载值。

Hope this below links will give more clear picture for you 希望下面的链接能够为您提供更清晰的画面

Returning ModelAndView in ajax spring mvc 在ajax spring mvc中返回ModelAndView

How to render a View using AJAX in Spring MVC 如何在Spring MVC中使用AJAX渲染视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM