照例,目录结构
在上一篇的基础上改动如下:
1.在pom.xml中添加依赖如下:
<!-- HTML -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
2.在application.properties中指定页面的位置和后缀
#视图层控制
spring.mvc.view.prefix=classpath:/templates/
spring.mvc.view.suffix=.html
spring.mvc.static-path-pattern=/static/**
3.在UserController.java
package com.cxs.controller;
import com.cxs.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.cxs.service.UserService;
@Controller
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/index")
public String Hello() {
return "user/index";
}
@RequestMapping(value = "/show", method = RequestMethod.GET)
@ResponseBody
public String findUserByName(@RequestParam(value = "name") String name) {
User user= userService.findUserByName(name);
if (null != user)
return "ID:"+ user.getUserId() + "--------------------用户名:" + user.getUserName()+ "--------------------密码:" + user.getPassword();
else return "null";
}
}
4.新建一个index.html,在里面写上“到此一游”!
5.访问
http://localhost:8090/demo/user/index
本来还想写一篇跳转到jsp页面的,但想一下jsp好像已经不怎么用了。springboot配上jsp大概就像时尚女郎穿着比基尼脚踏一双老北京布鞋,怎么想怎么感觉不搭。