Vidya
Vidya

Reputation: 698

Pass model object from spring controller to jsp

I am trying to pass a model object from spring controller to 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

<%@ 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?

Upvotes: 0

Views: 2265

Answers (2)

programmermurali
programmermurali

Reputation: 169

You have mentioned in the comment section its ajax based in spring mvc...

how you are hitting this ajax url....

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.

Hope this below links will give more clear picture for you

Returning ModelAndView in ajax spring mvc

How to render a View using AJAX in Spring MVC

Upvotes: 1

geoand
geoand

Reputation: 63991

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

Upvotes: 1

Related Questions