Springboot如何搭建web應(yīng)用集成thymeleaf模板實(shí)現(xiàn)登陸

這篇文章將為大家詳細(xì)講解有關(guān)Spring boot如何搭建web應(yīng)用集成thymeleaf模板實(shí)現(xiàn)登陸,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

為濟(jì)南等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及濟(jì)南網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、濟(jì)南網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎ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ū)動-->
    <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("登陸失?。?quot;+e,e);
    }
    return "page/index/index";
  }


  @Autowired
  private UserServices userServices;

}

其中 UserServices 為業(yè)務(wù)接口。BaseContrller為自己封裝的Controller基類。

關(guān)于“Spring boot如何搭建web應(yīng)用集成thymeleaf模板實(shí)現(xiàn)登陸”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

本文標(biāo)題:Springboot如何搭建web應(yīng)用集成thymeleaf模板實(shí)現(xiàn)登陸
文章鏈接:http://bm7419.com/article40/jcsjeo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、企業(yè)建站電子商務(wù)、服務(wù)器托管動態(tài)網(wǎng)站、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營