服务器之家

服务器之家 > 正文

Spring boot搭建web应用集成thymeleaf模板实现登陆

时间:2021-02-25 22:51     来源/作者:梦想涨价了

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

 

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

主入口main方法

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

登陆页提交表单代码,

?
1
2
3
4
5
6
7
8
<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="passwprd" />
   <button class="btn btn-lg btn-warning btn-block" type="submit">登录</button>
   <label class="checkbox">
     <input type="checkbox" value="remember-me" /> 记住我
   </label>
 </form>

Controller 代码

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

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

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

原文链接:http://blog.csdn.net/enterys/article/details/53760933

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部