当前位置: 首页 > news >正文

一步步实现web程序信息管理系统之二--后台框架实现跳转登陆页面

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

SpringBoot

springboot的目的是为了简化spring应用的开发搭建以及开发过程。内部使用了特殊的处理,使得开发人员不需要进行额外繁锁的xml文件配置的编写,其内部包含很多模块的配置只需要添加maven依赖即可使用,这项功能可谓对开发人员提供了大大的好处。使用springboot只需要简单配置一下就可以完成之前复杂的配置过程。可以到https://start.spring.io/此网站上,下载一个最简单的springboot应用,然后一步一步实现自已的应用。

 

v2-cee6be0feb3ae4a6e1b0d334ecfa9d73_hd.jpg

 

可以看出当前的稳定版本为2.1.0,点击Generate Project 按钮,即可下载一个可用的springboot应用。

 

v2-7e4ffb531dce70ad17c4343a69386514_hd.jpg

 

这个是我下载下来后,双击后出来的。可以看出以工程是一个基于maven的项目。你可以将其解压到任何一个目录下,通过eclipse或其他IDE进行导入后运行,eclipse导入流程为File->import->maven->existing maven projects,查找到自己的项目目录。也可以基于此工程来建立自已的maven项目。

下面以建立自己的maven项目

建立自己的springboot项目

  • 创建工程

在建立项目时,可以创建一个多模块聚合项目,即在创建项目时选中

 

v2-2a502bfad2082f9fbb8ac54f6c06915e_hd.jpg

 

选择为pom。

创建后的工程结构为

 

v2-27d88239c8a5558625121b0fc02fb770_hd.jpg

 

 

v2-27d88239c8a5558625121b0fc02fb770_hd.jpg

 

  • jar包依赖
  • 打开从springboot官网中下载下来的工程目录,打开pom.xml文件
<parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>2.1.0.RELEASE</version>
 <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 <java.version>1.8</java.version>
 </properties>

将此段代码复制到 spring-boot-study工程中的pom文件中

将下面的依赖复制到spring-boot-web工程中的pom文件中

<dependencies>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>
 </dependency>
 </dependencies>
 <build>
 <plugins>
 <plugin>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 </plugin>
 </plugins>
 </build>

eclipse自动完成项目工程的配置。完成后项目中所有需要依赖的jar包自动配置完成。

  • 代码编写
  • 将application.properties文件拷贝到spring-boot-study项目的resources目录下。文件中的内容暂时先不要管,编写以下代码
@SpringBootApplication
@RestController
public class WebApplication {
 @RequestMapping("/hello")
 public String helloWorld() {
 return "Hello World";
 }
 
 public static void main(String[] args) {
 SpringApplication.run(WebApplication.class, args);
 }
 
}

HelloWold就已经完成后。可以在浏览器中输入localhost:8080/hello即可看到效果

 

v2-c8a63362e7d3a63612748b659fcc5f71_hd.jpg

 

springboot默认启动后的端口为8080,但可以在application.properties文件中进行修改。

server.port=9001

将端口修改为9001,重新启动项目后,在浏览器中输入入localhost:9001/hello同样可以看到相同的结果。

  • 整合login界面
  • 现在后台已经有转发功能,具备web浏览功能。但我们需要访问URL为“/”时跳转到登陆界面,即创建好的登陆界面。本人也是在学习过程中,在网上学习好久才发现使用html的话就使用thymeleaf模板就好了。下面就详细来说说如何使用thymeleaf开发html。
  • 在spring-boot-web项目中的pom文件中加上thymeleaf的依赖。
<!-- 加入thymeleaf的支持 -->
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-thymeleaf</artifactId>
 </dependency>

但在Spring Boot项目中,一般src/main/resources/static目录用于存放各类静态资源文件,例如css、js和image等。src/main/resources/templates用于存放页面文件,例如html,jsp等。所以在spring-boot-web中的resources目录下创建static目录与templates目录,并将相应的资源文件放置在各自的目录下。

配置thymeleaf

#thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.cache=false

html文件修改,增加xmlns:th="http://www.thymeleaf.org" 属性,资源文件的引入要修改。

<link href="../static/css/style.css" th:href="@{/css/style.css}" rel="stylesheet" />
<link href="../static/css/login.css" th:href="@{/css/login.css}" rel="stylesheet" />

然后编写 java代码

@Controller
public class IndexController {
 @RequestMapping("/")
 public String index() {
 return "login";
 }
 
}

重新启动程序,访问localhost:9001/就可成功跳转至login.html登陆界面上。

注:thymeleaf对html标签要求很严格,每一个标签都需要成对出现。

调试过程中遇到下面异常信息

org.thymeleaf.exceptions.TemplateInputException: Error resolving template [login], template might not exist or might not be accessible by any of the configured Template Resolvers
 at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) [thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) [thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362) [thymeleaf-spring5-。。。。。。。。。。。

因为错将templates写成templatse导致。

至此实现从后端服务访问到登陆界面的搭建,还没有具体登陆逻辑实现。

有兴趣可以加一下854630135这个群去交流一下噢

转载于:https://my.oschina.net/u/3990817/blog/2873854

相关文章:

  • 基于CentOS设置和使用Yum软件仓库
  • 深入浏览器事件循环的本质
  • 镶锆石、侧边指纹、双屏翻盖机,三星的这款2万块手机,只有土豪能懂
  • 2018自媒体运营吸粉3大途径
  • 闭包--闭包作用之保存(一)
  • 智能监控在袋鼠云中的应用
  • 一个UML类图示例
  • Google 的 QUIC 华丽转身成为下一代网络协议: HTTP/3.0
  • eclipse 设置python 界面为默认展示
  • HTTP那些事
  • Java浅Copy的一些事
  • Java Log4j 配置文件
  • C++ 编译器
  • Haskell写的Parser
  • Java String.getBytes()编码
  • classpath对获取配置文件的影响
  • css的样式优先级
  • extjs4学习之配置
  • iOS 系统授权开发
  • Java小白进阶笔记(3)-初级面向对象
  • Java知识点总结(JDBC-连接步骤及CRUD)
  • Median of Two Sorted Arrays
  • node.js
  • PAT A1017 优先队列
  • php面试题 汇集2
  • python3 使用 asyncio 代替线程
  • Sublime Text 2/3 绑定Eclipse快捷键
  • SwizzleMethod 黑魔法
  • Work@Alibaba 阿里巴巴的企业应用构建之路
  • yii2中session跨域名的问题
  • 机器人定位导航技术 激光SLAM与视觉SLAM谁更胜一筹?
  • 机器学习 vs. 深度学习
  • ------- 计算机网络基础
  • 面试题:给你个id,去拿到name,多叉树遍历
  • 算法之不定期更新(一)(2018-04-12)
  • 微信小程序实战练习(仿五洲到家微信版)
  • 在 Chrome DevTools 中调试 JavaScript 入门
  • nb
  • Oracle Portal 11g Diagnostics using Remote Diagnostic Agent (RDA) [ID 1059805.
  • 7行Python代码的人脸识别
  • 进程与线程(三)——进程/线程间通信
  • 如何用纯 CSS 创作一个菱形 loader 动画
  • ​​​​​​​​​​​​​​汽车网络信息安全分析方法论
  • ​​快速排序(四)——挖坑法,前后指针法与非递归
  • ​中南建设2022年半年报“韧”字当头,经营性现金流持续为正​
  • ( )的作用是将计算机中的信息传送给用户,计算机应用基础 吉大15春学期《计算机应用基础》在线作业二及答案...
  • (C++17) optional的使用
  • (SpringBoot)第二章:Spring创建和使用
  • (八)Flask之app.route装饰器函数的参数
  • (蓝桥杯每日一题)平方末尾及补充(常用的字符串函数功能)
  • (学习日记)2024.04.04:UCOSIII第三十二节:计数信号量实验
  • (一)pytest自动化测试框架之生成测试报告(mac系统)
  • (原创)boost.property_tree解析xml的帮助类以及中文解析问题的解决
  • ***利用Ms05002溢出找“肉鸡
  • .NET CORE 第一节 创建基本的 asp.net core