Spring boot builds web application integration thymeleaf template to login
- 2020-12-07 04:02:59
- OfStack
Spring boot builds web application and integrates thymeleaf template to realize login
Here is the configuration of pom.xml
<?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 Basic configuration -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7.RELEASE</version>
</parent>
<!-- Basic configuration, setting code, entry, jdk version -->
<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>
<!-- Set the compile -->
<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 the jar package , operating on a database, something like that hibernate-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--thymeleaf The template jar-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--mysql drive -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- add restfull The support of -->
<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>
<!-- add druid Data source connection pool -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.25</version>
</dependency>
<!-- Add permission authentication -->
<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 and shiro The integration of -->
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
</project>
The main entry main method
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);
}
}
The login page submits the form code,
<form class="form-signin" role="form" th:action="@{/user/login}" th:method="post">
<input type="text" class="form-control" placeholder=" The user name " required="required" name="userName" />
<input type="password" class="form-control" placeholder=" password " required="required" name="passwprd" />
<button class="btn btn-lg btn-warning btn-block" type="submit"> The login </button>
<label class="checkbox">
<input type="checkbox" value="remember-me" /> Remember that I
</label>
</form>
Controller code
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;
/**
*
* User management
* 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(" Login failed: "+e,e);
}
return "redirect:/";
}
@RequestMapping(value="/index",method= RequestMethod.GET)
public String index(){
try{
}catch (Exception e){
logger.error(" Login failed: "+e,e);
}
return "page/index/index";
}
@Autowired
private UserServices userServices;
}
UserServices is the business interface. BaseContrller is the Controller base class that encapsulates itself.