Spring boot builds web application and integrates thymeleaf template to achieve login. The following 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> <!--Basic Configuration of Spring boot--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.7.RELEASE</version> </parent> <!--Basic Configuration, Setting Coding, 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> <!-- Setting Compilation--> <build> <plugins> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <dependencies> </dependencies> </plugin> </plugins> </build> <dependencies> <dependencies> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--jpa's jar package, which operates the database, is similar to hibernate--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!--thymeleaf template jar--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--mysql driver--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- Add restfull support--> <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> <!--Integration of shiro and shiro--> <dependency> <groupId>com.github.theborakompanioni</groupId> <artifactId>thymeleaf-extras-shiro</artifactId> <version>1.2.1</version> </dependency> </dependencies></project>Main entry 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@EnableAutoConfigurationpublic class App extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(App.class, args); }}Submit form code on the login page,
<form role="form" th:action="@{/user/login}" th:method="post"> <input type="text" placeholder="username" required="required" name="userName" /> <input type="password" placeholder="password" required="required" name="passwprd" /> <button type="submit">Login</button> <label> <input type="checkbox" value="remember-me" /> Remember me</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;}Where UserServices is a business interface. BaseContrller encapsulates the Controller base class for itself.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.