Spring boot搭建web應(yīng)用集成thymeleaf模板實(shí)現(xiàn)登陸
Spring boot 搭建web應(yīng)用集成了thymeleaf模板實(shí)現(xiàn)登陸
下面是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 的基本配置 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.7.RELEASE</version> </parent> <!--基本配置,設(shè)置編碼,入口,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> <!-- 設(shè)置編譯 --> <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包 ,操作數(shù)據(jù)庫的,類似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驅(qū)動(dòng)--> <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 數(shù)據(jù)源連接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.25</version> </dependency> <!-- 添加權(quán)限認(rèn)證--> <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方法
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); } }
登陸頁提交表單代碼,
<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 代碼
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 為業(yè)務(wù)接口。BaseContrller為自己封裝的Controller基類。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring Boot thymeleaf模板引擎的使用詳解
- SpringBoot使用Thymeleaf模板引擎訪問靜態(tài)html的過程
- springBoot加入thymeleaf模板的方式
- spring boot 項(xiàng)目中使用thymeleaf模板的案例分析
- Spring boot項(xiàng)目使用thymeleaf模板過程詳解
- SpringBoot使用thymeleaf模板過程解析
- Spring Boot集成Thymeleaf模板引擎的完整步驟
- SpringBoot中的Thymeleaf模板
- Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖的方法
- 詳解spring Boot 集成 Thymeleaf模板引擎實(shí)例
- springboot用thymeleaf模板的paginate分頁完整代碼
- springboot中thymeleaf模板使用詳解
- Springboot Thymeleaf模板文件調(diào)用Java類靜態(tài)方法
- Java基礎(chǔ)總結(jié)之Thymeleaf詳解
相關(guān)文章
Java使用新浪微博API開發(fā)微博應(yīng)用的基本方法
這篇文章主要介紹了Java使用新浪微博API開發(fā)微博應(yīng)用的基本方法,文中還給出了一個(gè)不使用任何SDK實(shí)現(xiàn)Oauth授權(quán)并實(shí)現(xiàn)簡單的發(fā)布微博功能的實(shí)現(xiàn)方法,需要的朋友可以參考下2015-11-11Java Arrays.sort和Collections.sort排序?qū)崿F(xiàn)原理解析
這篇文章主要介紹了Java Arrays.sort和Collections.sort排序?qū)崿F(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02java使用poi在excel單元格添加超鏈接設(shè)置字體顏色的方法
這篇文章主要介紹了java使用poi在excel單元格添加超鏈接,設(shè)置字體顏色,poi功能還是很強(qiáng)大的,基本能想到的功能都能通過poi實(shí)現(xiàn),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09Java?超詳細(xì)講解類的定義方式和對象的實(shí)例化
Java是一門純面向?qū)ο蟮恼Z言(Object?Oriented?Program,繼承OOP),在面對對象的世界里面,一切皆為對象。面向?qū)ο笫墙鉀Q問題的一種思想,主要依靠對象之間的交互完成一件事情2022-03-03Java實(shí)現(xiàn)KFC點(diǎn)餐系統(tǒng)過程解析
這篇文章主要介紹了Java實(shí)現(xiàn)KFC點(diǎn)餐系統(tǒng)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10springboot開啟Bean數(shù)據(jù)校驗(yàn)功能
這篇文章主要介紹了springboot開啟Bean數(shù)據(jù)校驗(yàn)功能,通過啟用Bean屬性校驗(yàn)導(dǎo)入JSR303與Hibernate校驗(yàn)框架坐標(biāo),使用@Validated注解啟用校驗(yàn)功能,需要的朋友可以參考下2023-10-10SpringBoot+Vue項(xiàng)目打包部署完整步驟教程
這篇文章主要介紹了SpringBoot+Vue項(xiàng)目打包部署的相關(guān)資料,包括Vue項(xiàng)目的打包設(shè)置、SpringBoot的配置修改、跨域問題處理、使用Nginx配置反向代理以及最終的項(xiàng)目啟動(dòng),教程假定開發(fā)者已具備完整的前后端分離項(xiàng)目和配置好環(huán)境的服務(wù)器,需要的朋友可以參考下2024-10-10