How to pass value from controller to javascript in spring mvc

I am trying to get a variable for a javascript function from an object that sends it to view by the controller.

The object with which I am working is Bpmsn.

How to pass value from controller to javascript in spring mvc

Through the controller I injected the object into view.

My method of the controller is:

@RequestMapping(value = "/display")
public ModelAndView index2(@RequestParam int bpmsnId) {
    ModelAndView result;
    Bpmsn bpmsn;

    bpmsn = bpmsnService.findOne(bpmsnId);

    result = new ModelAndView("editor/display");
    result.addObject("bpmsn", bpmsn);

    return result;
}

This method I use to make a display of the object.

In the jsp view I inject the attributes of the object, except the textXML that I will use in a javascript script.

  • :
  • :
  • :
  • :
  • :
  • :

With the textXML attribute I want to create a javascript function to import that xml into a bpmn modeler, but I do not know how to get the textXML attribute of the object injected into the view from the javascript script

I tried to call the attribute as in the view but it does not work


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>




Insert title here


	

Read the textbox value


Employee name: ${empName}

Run the App


Start the tomcat server, navigate to the URL localhost:8080/springmvc/

How to pass value from controller to javascript in spring mvc


How to pass value from controller to javascript in spring mvc

That’s it for this post, if you like this post, share this with your friends and colleagues or you can share this within your social media platform. Thanks, I will see you in our next post.

Post Views: 5,191

About the author

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

How to pass data from controller to JS?

Controller jsonresult code: public JsonResult GetReleasedDates(string Genre) { var relDates = service. GetDates(Genre)//code to get the dates here return Json(relDates, JsonRequestBehavior . AllowGet); //relDates will have the dates needed to pass to the datepicker control. }

How pass data from controller view in Spring MVC?

Complete Example. Create a maven project. Add Maven Dependencies. Configure Dispatcher Servlet. Configure View Resolver. Create a Controller. Create a view template..
Run the App..

Which component helps controller send the data to view in spring?

The DispatcherServlet The Controller takes the request and calls the appropriate service methods based on used GET or POST method. The service method will set model data based on defined business logic and returns view name to the DispatcherServlet.