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.
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
var bpmnXML = ${bpmsn.textXML}; //not work
alert[bpmnXML]
// BpmnJS is the BPMN viewer instance
var viewer = new BpmnJS[{
container : '#canvas'
}];
// import a BPMN 2.0 diagram
viewer.importXML[bpmnXML, function[err] {
if [err] {
// import failed :-[
alert['could not import BPMN 2.0 diagram', err];
} else {
// we did well!
var canvas = viewer.get['canvas'];
canvas.zoom['fit-viewport'];
}
}];
Hey guys in this article, you will learn about passing data from Controller to view template in Spring MVC
Read More:
- Check the Complete Spring MVC Tutorials
- Check the Complete JSP Tutorials
- Check the
Complete Spring Boot Tutorials [100+ Examples]
- Check the Complete Spring Boot and Thymeleaf Tutorial
- Check the Complete AWS Tutorial
- Check the Complete JavaServer Faces [JSF] Tutorial
- Check
the Complete Spring Data JPA Tutorial
- Check the Complete Spring Security Tutorial
- Check the Javascript Projects for Beginners
- Check the Spring Boot JdbcTemplate Tutorials
- Overview
- 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
Overview
- We can make use of Spring Model to pass data from controller to view template
- Spring Model is a container for accessing data in spring application
- We can add anything to Spring Model in Spring Controller. We can add, string, boolean, objects, array and database records etc..,
- Inside the view template we can access the Spring Model to access the data.
theModel.addAttribute[name, value];
theModel
,
is the Spring Model. addAttribute[]
takes two parameters, name and value. Inside the view template [if you are using JSP] we can access the model data using EL expression ${name}
Complete Example
Lets create a step by step maven project to understand the Spring MVC
Create a maven project
First you need to create a maven webapp
project. You can check the below post to create new maven webapp project –
- Create a maven webapp project
Add Maven Dependencies
Open pom.xml
and add the following maven dependencies
4.0.0
in.bushansirgur
springmvc
war
1.0.0
springmvc Maven Webapp
//maven.apache.org
1.11
1.11
4.3.6.RELEASE
junit
junit
3.8.1
test
org.springframework
spring-webmvc
${spring.version}
javax.servlet
jsp-api
2.0
provided
springmvc
Configure Dispatcher Servlet
Open web.xml
and add the following content
spring-mvc-demo
dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/spring-mvc-dispatcherservlet.xml
1
dispatcher
/
Configure View Resolver
Create spring-mvc-dispatcherservlet.xml
under webapp/WEB-INF
and add the following content
Create a Controller
Inside src/main/java
create a package in.bushansirgur.springmvc.controller
, under this create HomeController.java
and add the following content
package in.bushansirgur.springmvc.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping[value = "/"]
public String showHomePage[] {
return "home-page";
}
@RequestMapping[value = "/processForm"]
public String showDashboardPage[HttpServletRequest request, Model theModel] {
String name = request.getParameter["empName"];
theModel.addAttribute["empName", name];
return "dashboard";
}
}
Create a view template
Create home-page.jsp
inside webapp/WEB-INF/view
folder and add the following content
Insert title here
Read the textbox value
Send
Create dashboard.jsp
inside webapp/WEB-INF/view
folder and add the following content
Insert title here
Read the textbox value
Employee name: ${empName}
Run the App
Start the tomcat server, navigate to the URL localhost:8080/springmvc/
—
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.
Chủ Đề