1、pom依赖
<!-- springboot模板 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.15</version>
</dependency>
2、application配置
thymeleaf:
cache: false #关闭缓存
prefix: classpath:/template/
suffix: .html
# mode: LEGACYHTML5
encoding: UTF-8
content-type: text/html
#thymeleaf end
3、在resources下面新建template目录
4、新建login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>hello world</title>
</head>
<body>
<form class="form-signin" role="form" th:action="@{/user/login}" th:method="post">
<input type="text" class="form-control" placeholder="用户名" required="required" name="userName" />
<input type="password" class="form-control" placeholder="密码" required="required" name="password" />
<button class="btn btn-lg btn-warning btn-block" type="submit">登录</button>
</form>
</body>
</html>
5、controller代码
import javax.servlet.http.HttpServletResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import cn.sunline.edsp.tsp.portal.constant.Const;
import cn.sunline.edsp.tsp.portal.entity.User;
@Controller
public class PortalController {
//用户名
private final static String USERNAME = Const.USERNAME.toString();
//密码
private final static String PASSWORD = Const.PASSWORD.toString();
private final Logger logger = LogManager.getLogger(PortalController.class);
/**
* 登录界面
* @param response
* @return
*/
@RequestMapping(value = "login")
public String login(HttpServletResponse response) {
return "login";
}
/**
* 登录处理
* @return
*/
@RequestMapping(value = "/user/login",method= RequestMethod.POST)
public String toLogin(User user) {
if(!USERNAME.equals(user.getUserName())) {
logger.error("用户名错误");
return "login";
}
if(!PASSWORD.equals(user.getPassword())) {
logger.error("密码错误");
return "login";
}
return "redirect:/user/index";
}
@RequestMapping(value = "/user/index")
public String toIndex() {
return "index";
}
}