Spring boot搭建web应用集成thymeleaf模板实现登陆
程序员文章站
2023-12-09 16:21:57
spring boot 搭建web应用集成了thymeleaf模板实现登陆
下面是pom.xml的配置
spring boot 搭建web应用集成了thymeleaf模板实现登陆
下面是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> <!--基本配置,设置编码,入口,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方法
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 为业务接口。basecontrller为自己封装的controller基类。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。