1) SpringMVC的控制层主要是通过使用@Controller的注解来操作。在WEB的申请中,控制的角色将会以HTTP请求来响应。对应的类文件注解了 @Controller ,Spring会抓取,并且给出将要响应的转变操作。
2) 注册@RequestMapping注解,控制器将会以不同的HTTP方式来请求,如:GET或POST的方式,和他们请求的URL地址。控制器会以是直接用返回响应的结果,还是返回要新的视图页面。
3) 如果直接用RESful请求,那么将会通过HTTP响应JSON或XML格式的显示。我们使用的注解是 @ResponseBody。在web的申请中,通常会连接JavaScript的框架,像Backbone.js、AngularJS、或React。
我们都知道,视图是会使用某种语言的模板,它会驾驶在数据之间。我们常用的视图显示模板是JSP、FreeMarker、或Thymeleaf。在我们接下来学习中,我们使用的是Thymeleaf.
Thymeleaf是一个模板引擎,它的优点中有友好的语言(它的语言有点像Html)和更容易去扩展。如下是提供可扩展的应用。
读者可以通过网址http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html 学习Thymeleaf的相关知识。
下面我们将通过一个应用的程序来实现SpringMVC的注解功能,和Thymeleaf的应用。在应用的程序最后会显示Hello thymeleaf的页面。
1) 相信通过前面的章节,读者已经知道了如何创建一个SprinMVCV的项目,和如何使用Gradle来管理我们的架包。对应的我们也要添加Thymeleaf相关的架包。在build.gradle的文件中添加。
dependencies { compile 'org.springframework.boot:spring-boot-starter-web' compile 'org.springframework.boot:spring-boot-starter-thymeleaf' testCompile 'org.springframework.boot:spring-boot-starter-test' }
2) 我们现在在我们的应用中添加第一个页面。它应该在本地的src/main/resources/templates的路径下,我们将这个文件取名为:resultPage.html.
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head lang="en"> <meta charset="UTF-8"/> <title>Hello thymeleaf</title> </head> <body> <span th:text="|Hello thymeleaf|">Hello html</span> </body> </html>
在上面的代码中,我们看到子th:text的值是在两条竖线之间,这个意味着所有的值都将联系起来。也就是显示竖线的所有文字。
Thymeleaf对于web设计来说有很在的好处,在它的模板中,每一件事都是动态的,如果没有请求到对应的服务器,那么显示的值是可以回滚的。也就是说,我们的每一个请求都会连接到http://www.thymeleaf.org中寻找对应的资源。就像上面的“Hello html”,如果没有连接到资源库,那么就会显示,只要连接到了资源库,则会显示“Hello thymeleaf”。如果你用浏览器直接打开,那么也会显示“Hello html”.
3) 最后,我们在本地的src/main/java下添加下面的代码,这个类要求加入注解@Controller,当然请求的映射@RequestMapping().
package masterSpringMVC.controller; @Controller public class HelloController { @RequestMapping("/") public String hello() { return "resultPage"; } }
4) 让我们开始应用程序,访问:http://localhost:8080.你将会看到:
接着我们上面的例子,我们显示是Hellothymeleaf。然而,如果我们要显示是从Controll中获取呢?也就是后台逻辑处理的情况,应该怎么调用。
1) 我们修改resultPage.html的代码如下:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head lang="en"> <meta charset="UTF-8"/> <title>Hello thymeleaf</title> </head> <body> <span th:text="${message}">Hello html</span> </body> </html>
2) 修改HelloController代码。
@Controller public class HelloController { @RequestMapping("/") public String hello(Model model) { model.addAttribute("message", "Hello from the controller"); return "resultPage"; } }
3) 你将会看到的结果如下。
我们已经有能力去从服务器上获取数据到视图页面。然而,我们应该怎么去获取用户民输入的请求参数呢?这里有很多方法,可是最简单的方法,就是通过URL来获取参数。
再次修改我们的HelloController的类。
@Controller public class HelloController { @RequestMapping("/") public String hello(@RequestParam("name") String userName, Model model) { model.addAttribute("message", "Hello, " + userName); return "resultPage"; } }
我们这个时候在浏览器上输入的地址是:localhost:8080/?name=Geoffroy。你将会看到的结果如下:
从上面的访问可以看出,我们的请求参数是固定,也就是说如果你直接访问localhost:8080,那么页面将会报错。为了解决这个问题,我们可能添加@RequestParam代码。我们可以添加两个属性值:required和defaultValue。代码如下:
@Controller public class HelloController { @RequestMapping("/") public String hello(@RequestParam(defaultValue = "world") String name, Model model) { model.addAttribute("message", "Hello, " + name); return "resultPage"; } }