Add code for issue BAEL-2574 (#6249)
* Add code for issue BAEL-2574 This is a code for article "Access Spring MVC Model object in JS" Set up the project Add a test * Rename the test class
This commit is contained in:
parent
56a832273e
commit
191039ffc6
@ -72,6 +72,16 @@
|
||||
<version>${spring.fox.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat.embed</groupId>
|
||||
<artifactId>tomcat-embed-jasper</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -0,0 +1,13 @@
|
||||
package com.baeldung.accessparamsjs;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(App.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.baeldung.accessparamsjs;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
/**
|
||||
* Sample rest controller for the tutorial article
|
||||
* "Access Spring MVC Model object in JavaScript".
|
||||
*
|
||||
* @author Andrew Shcherbakov
|
||||
*
|
||||
*/
|
||||
@RestController
|
||||
public class Controller {
|
||||
|
||||
/**
|
||||
* Define two model objects (one integer and one string) and pass them to the view.
|
||||
*
|
||||
* @param model
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("/index")
|
||||
public ModelAndView index(Map<String, Object> model) {
|
||||
model.put("number", 1234);
|
||||
model.put("message", "Hello from Spring MVC");
|
||||
return new ModelAndView("/index");
|
||||
}
|
||||
|
||||
}
|
@ -1 +1,3 @@
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
spring.mvc.view.prefix=/WEB-INF/jsp/
|
||||
spring.mvc.view.suffix=.jsp
|
27
spring-boot-mvc/src/main/webapp/WEB-INF/jsp/index.jsp
Normal file
27
spring-boot-mvc/src/main/webapp/WEB-INF/jsp/index.jsp
Normal file
@ -0,0 +1,27 @@
|
||||
<!DOCTYPE html>
|
||||
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Access Spring MVC params</title>
|
||||
<script src="/js/jquery.js"></script>
|
||||
<script src="/js/script-async.js"></script>
|
||||
<script src="/js/script-async-jquery.js"></script>
|
||||
<script>
|
||||
var number = <c:out value="${number}"></c:out>;
|
||||
var message = "<c:out value="${message}"></c:out>";
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<h2>Data from the external JS file (due to loading order)</h2>
|
||||
<div id="number-ext"></div>
|
||||
<div id="message-ext"></div>
|
||||
<h2>Asynchronous loading from external JS file (plain JS)</h2>
|
||||
<div id="number-async"></div>
|
||||
<div id="message-async"></div>
|
||||
<h2>Asynchronous loading from external JS file (jQuery)</h2>
|
||||
<div id="number-async-jquery"></div>
|
||||
<div id="message-async-jquery"></div>
|
||||
|
||||
</body>
|
||||
<script src="/js/script.js"></script>
|
||||
</html>
|
2
spring-boot-mvc/src/main/webapp/js/jquery.js
vendored
Normal file
2
spring-boot-mvc/src/main/webapp/js/jquery.js
vendored
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,6 @@
|
||||
$(function() {
|
||||
var node1 = document.createTextNode("message = " + message);
|
||||
var node2 = document.createTextNode("number = " + number);
|
||||
document.getElementById('message-async-jquery').append(node1);
|
||||
document.getElementById('number-async-jquery').append(node2);
|
||||
});
|
6
spring-boot-mvc/src/main/webapp/js/script-async.js
Normal file
6
spring-boot-mvc/src/main/webapp/js/script-async.js
Normal file
@ -0,0 +1,6 @@
|
||||
window.onload = function() {
|
||||
var node1 = document.createTextNode("message = " + message);
|
||||
var node2 = document.createTextNode("number = " + number);
|
||||
document.getElementById('message-async').append(node1);
|
||||
document.getElementById('number-async').append(node2);
|
||||
};
|
4
spring-boot-mvc/src/main/webapp/js/script.js
Normal file
4
spring-boot-mvc/src/main/webapp/js/script.js
Normal file
@ -0,0 +1,4 @@
|
||||
var node1 = document.createTextNode("message = " + message);
|
||||
var node2 = document.createTextNode("number = " + number);
|
||||
document.getElementById('message-ext').append(node1);
|
||||
document.getElementById('number-ext').append(node2);
|
@ -0,0 +1,28 @@
|
||||
package com.baeldung.accessparamsjs;
|
||||
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
public class ControllerUnitTest {
|
||||
@Autowired
|
||||
private MockMvc mvc;
|
||||
|
||||
@Test
|
||||
public void whenRequestIndex_thenStatusOk() throws Exception {
|
||||
mvc.perform(MockMvcRequestBuilders.get("/index")
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user