From f63a8530a99cfec2202586eece2849bd3dde7188 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E7=8C=BFDD?= Date: Sun, 20 Mar 2016 18:39:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8velocity=E6=B8=B2=E6=9F=93web?= =?UTF-8?q?=E8=A7=86=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Chapter3-1-4/pom.xml | 63 +++++++++++++++++++ .../main/java/com/didispace/Application.java | 22 +++++++ .../com/didispace/web/HelloController.java | 29 +++++++++ .../src/main/resources/application.properties | 0 .../src/main/resources/templates/index.vm | 11 ++++ .../java/com/didispace/ApplicationTests.java | 48 ++++++++++++++ 6 files changed, 173 insertions(+) create mode 100755 Chapter3-1-4/pom.xml create mode 100755 Chapter3-1-4/src/main/java/com/didispace/Application.java create mode 100755 Chapter3-1-4/src/main/java/com/didispace/web/HelloController.java create mode 100755 Chapter3-1-4/src/main/resources/application.properties create mode 100755 Chapter3-1-4/src/main/resources/templates/index.vm create mode 100755 Chapter3-1-4/src/test/java/com/didispace/ApplicationTests.java diff --git a/Chapter3-1-4/pom.xml b/Chapter3-1-4/pom.xml new file mode 100755 index 0000000..c7f7058 --- /dev/null +++ b/Chapter3-1-4/pom.xml @@ -0,0 +1,63 @@ + + + 4.0.0 + + com.didispace + demo + 1.0.0 + jar + + demo + Spring Boot Web project + + + org.springframework.boot + spring-boot-starter-parent + 1.3.2.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-velocity + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + + + diff --git a/Chapter3-1-4/src/main/java/com/didispace/Application.java b/Chapter3-1-4/src/main/java/com/didispace/Application.java new file mode 100755 index 0000000..f7a9e16 --- /dev/null +++ b/Chapter3-1-4/src/main/java/com/didispace/Application.java @@ -0,0 +1,22 @@ +package com.didispace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * + * @author 程序猿DD + * @version 1.0.0 + * @blog http://blog.didispace.com + * + */ +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + + SpringApplication.run(Application.class, args); + + } + +} diff --git a/Chapter3-1-4/src/main/java/com/didispace/web/HelloController.java b/Chapter3-1-4/src/main/java/com/didispace/web/HelloController.java new file mode 100755 index 0000000..66b3ea7 --- /dev/null +++ b/Chapter3-1-4/src/main/java/com/didispace/web/HelloController.java @@ -0,0 +1,29 @@ +package com.didispace.web; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * + * @author 程序猿DD + * @version 1.0.0 + * @blog http://blog.didispace.com + * + */ +@Controller +public class HelloController { + + @RequestMapping("/hello") + public String hello() { + return "Hello World"; + } + + @RequestMapping("/") + public String index(ModelMap map) { + map.addAttribute("host", "http://blog.didispace.com"); + return "index"; + } + +} \ No newline at end of file diff --git a/Chapter3-1-4/src/main/resources/application.properties b/Chapter3-1-4/src/main/resources/application.properties new file mode 100755 index 0000000..e69de29 diff --git a/Chapter3-1-4/src/main/resources/templates/index.vm b/Chapter3-1-4/src/main/resources/templates/index.vm new file mode 100755 index 0000000..e957871 --- /dev/null +++ b/Chapter3-1-4/src/main/resources/templates/index.vm @@ -0,0 +1,11 @@ + + + + + + + +Velocity模板 +

${host}

+ + \ No newline at end of file diff --git a/Chapter3-1-4/src/test/java/com/didispace/ApplicationTests.java b/Chapter3-1-4/src/test/java/com/didispace/ApplicationTests.java new file mode 100755 index 0000000..c5b2e60 --- /dev/null +++ b/Chapter3-1-4/src/test/java/com/didispace/ApplicationTests.java @@ -0,0 +1,48 @@ +package com.didispace; + +import com.didispace.web.HelloController; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockServletContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import static org.hamcrest.Matchers.equalTo; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + + +/** + * + * @author 程序猿DD + * @version 1.0.0 + * @blog http://blog.didispace.com + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = MockServletContext.class) +@WebAppConfiguration +public class ApplicationTests { + + private MockMvc mvc; + + @Before + public void setUp() throws Exception { + mvc = MockMvcBuilders.standaloneSetup( + new HelloController()).build(); + } + + @Test + public void getHello() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string(equalTo("Hello World"))); + } + +}