查看: 2440|回复: 0

[Java语言] Spring boot搭建web应用集成thymeleaf模板实现登陆

发表于 2018-1-13 09:30:53

Spring boot 搭建web应用集成了thymeleaf模板实现登陆
下面是pom.xml的配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>exam</groupId>
  7. <artifactId>examSystem</artifactId>
  8. <packaging>jar</packaging>
  9. <version>1.0-SNAPSHOT</version>
  10. <!--spring boot 的基本配置 -->
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>1.2.7.RELEASE</version>
  15. </parent>
  16. <!--基本配置,设置编码,入口,jdk版本 -->
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <start-class>com.study.App</start-class>
  20. <java.version>1.7</java.version>
  21. <shiro.version>1.3.0</shiro.version>
  22. </properties>
  23. <!-- 设置编译 -->
  24. <build>
  25. <plugins>
  26. <plugin>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-maven-plugin</artifactId>
  29. <dependencies>
  30. </dependencies>
  31. </plugin>
  32. </plugins>
  33. </build>
  34. <dependencies>
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-web</artifactId>
  38. </dependency>
  39. <!--jpa的jar包 ,操作数据库的,类似hibernate-->
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-starter-data-jpa</artifactId>
  43. </dependency>
  44. <!--thymeleaf模板jar-->
  45. <dependency>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  48. </dependency>
  49. <!--mysql驱动-->
  50. <dependency>
  51. <groupId>mysql</groupId>
  52. <artifactId>mysql-connector-java</artifactId>
  53. </dependency>
  54. <!-- 添加restfull的支持 -->
  55. <dependency>
  56. <groupId>javax.ws.rs</groupId>
  57. <artifactId>javax.ws.rs-api</artifactId>
  58. <version>2.0.1</version>
  59. </dependency>
  60. <dependency>
  61. <groupId>net.bull.javamelody</groupId>
  62. <artifactId>javamelody-core</artifactId>
  63. <version>1.53.0</version>
  64. </dependency>
  65. <!-- 添加 druid 数据源连接池-->
  66. <dependency>
  67. <groupId>com.alibaba</groupId>
  68. <artifactId>druid</artifactId>
  69. <version>1.0.25</version>
  70. </dependency>
  71. <!-- 添加权限认证-->
  72. <dependency>
  73. <groupId>org.apache.shiro</groupId>
  74. <artifactId>shiro-core</artifactId>
  75. <version>${shiro.version}</version>
  76. </dependency>
  77. <dependency>
  78. <groupId>org.apache.shiro</groupId>
  79. <artifactId>shiro-spring</artifactId>
  80. <version>${shiro.version}</version>
  81. </dependency>
  82. <dependency>
  83. <groupId>org.apache.shiro</groupId>
  84. <artifactId>shiro-web</artifactId>
  85. <version>${shiro.version}</version>
  86. </dependency>
  87. <dependency>
  88. <groupId>org.apache.shiro</groupId>
  89. <artifactId>shiro-ehcache</artifactId>
  90. <version>${shiro.version}</version>
  91. </dependency>
  92. <!--thymeleaf 和 shiro 的整合 -->
  93. <dependency>
  94. <groupId>com.github.theborakompanioni</groupId>
  95. <artifactId>thymeleaf-extras-shiro</artifactId>
  96. <version>1.2.1</version>
  97. </dependency>
  98. </dependencies>
  99. </project>
复制代码

主入口main方法

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.context.web.SpringBootServletInitializer;
  5. import org.springframework.context.annotation.ComponentScan;
  6. import org.springframework.context.annotation.Configuration;
  7. /**
  8. * Created by on 2016/12/8.
  9. */
  10. @Configuration
  11. @ComponentScan
  12. @EnableAutoConfiguration
  13. public class App extends SpringBootServletInitializer {
  14. public static void main(String[] args) {
  15. SpringApplication.run(App.class, args);
  16. }
  17. }
复制代码

登陆页提交表单代码,

  1. <form class="form-signin" role="form" th:action="@{/user/login}" th:method="post">
  2. <input type="text" class="form-control" placeholder="用户名" required="required" name="userName" />
  3. <input type="password" class="form-control" placeholder="密码" required="required" name="passwprd" />
  4. <button class="btn btn-lg btn-warning btn-block" type="submit">登录</button>
  5. <label class="checkbox">
  6. <input type="checkbox" value="remember-me" /> 记住我
  7. </label>
  8. </form>
复制代码

Controller 代码

  1. package com.study.system.contrller;
  2. import com.study.model.contrller.BaseContrller;
  3. import com.study.model.po.User;
  4. import com.study.system.services.UserServices;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import java.util.List;
  11. /**
  12. *
  13. * 用户管理
  14. * Created by on 2016/12/12.
  15. */
  16. @Controller
  17. @RequestMapping(value = "/user")
  18. public class UserContrller extends BaseContrller {
  19. @RequestMapping(value="/login",method= RequestMethod.POST)
  20. public String login(User user){
  21. try{
  22. if(userServices.hasUser(user)){
  23. return "redirect:/user/index";
  24. }else{
  25. return "redirect:/";
  26. }
  27. }catch (Exception e){
  28. logger.error("登陆失败:"+e,e);
  29. }
  30. return "redirect:/";
  31. }
  32. @RequestMapping(value="/index",method= RequestMethod.GET)
  33. public String index(){
  34. try{
  35. }catch (Exception e){
  36. logger.error("登陆失败:"+e,e);
  37. }
  38. return "page/index/index";
  39. }
  40. @Autowired
  41. private UserServices userServices;
  42. }
复制代码

其中 UserServices 为业务接口。BaseContrller为自己封装的Controller基类。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报