SpringBoot入门系列HelloWorld
根据咱们程序员学习的惯例,学习一门新技术都是从HelloWorld开始的。 感觉编程是一件非常富有意义的事情,程序员也是一群可爱的人,渴望被关怀和关注,因为我们总在和世界say Hi. 好了进入正题
创建项目
首先创建一个项目,可看我上一篇文章写得 IntelliJ IDEA创建第一个Spring boot项目 接下来运行这个项目,你将会看到如下页面 image.png 提示我们当前没有准确的映射,所以找不到对应的页面也就是404。莫慌,接下来咱们处理一下
创建HelloController控制器
在项目名/src/main/java/包名下,新建一个config包,包下面创建HelloController
@Controller
public class HelloController {
@RequestMapping(value = "/hello",method = RequestMethod.GET)
@ResponseBody
public String hello(){
return "Hello World";
}
}
注解说明:
@Controller: 可让项目扫描自动检测到这个类,处理http请求
@ RequestMapping 请求的路由映射,访问的路径就是:http://localhost:8080/hello
value: 路由名
method: 请求方式,GET,POST,PUT,DELETE等
重新启动项目
访问:http://localhost:8080/hello, 就看到Hello World了
image.png 看到如上图所示,就表示我们的hello world成功了。
目录结构:
image.png
src/main/java: Java代码的目录
src/main/resources: 资源目录
src/test/java: 测试代码的目录
src/test/resources: 测试资源目录
文件说明
pom.xml文件
父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
管理Spring Boot应用里面所依赖的版本
管理依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Spring Boot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来,要用什么功能就导入什么场景的启动器
主程序类,入口类
image.png @SpringBootApplication : Spring Boot应用标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main方法来启动SpringBoot应用;
def set_pwd(request):
if request.method==www.michenggw.com"POST":
oldpassword = request.POST.get(www.michenggw.com"oldpassword")
newpassword = request.POST.get("newpassword")
#得到当前登录的用户,判断旧密码是不是和当前的密码一样
username = request.user #打印的是当前登录的用户名
user = User.www.yongshiyule178.com objects.get(username=username) #查看用户
ret = user.check_password(oldpassword) #检查密码是否正确
if ret:
user.set_password(newpassword) #如果正确就给设置一个新密码
user.save() #保存
return redirect( www.dasheng178.com "/login/")
else:
info = "输入密码有误"
return render(request,www.fengshen157.com/"set_pwd.html",{"info":info})
return render(request,"set_pwd.html")
复制代码
复制代码
注册:
复制代码
复制代码
def reg(request):
if request.method=="POST":
username = request.POST.get("username")
password = request.POST.get("password")
#得到用户输入的用户名和密码创建一个新用户
User.objects.create_user(username=username,password=password) #User是以个对象
s = "恭喜你注册成功,现在可以登录了"
return redirect("/login/")
return render(request,"reg.html")
复制代码
复制代码
注销:
def log_out(request):
auth.logout(request)
return redirect("/login/")
SpringBoot入门系列HelloWorld的更多相关文章
- SpringBoot入门系列(转)
SpringBoot入门系列:第一篇 Hello World http://blog.csdn.net/lxhjh/article/details/51711148
- SpringBoot入门系列(十一)统一异常处理的实现
前面介绍了Spring Boot 如何整合定时任务已经Spring Boot 如何创建异步任务和定时任务.不清楚的朋友可以看看之前的文章:<Spring Boot 入门系列文章> 接下来主 ...
- SpringBoot入门系列(十二)统一日志收集
前面介绍了Spring Boot 异常处理,不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/1657780.html. 今 ...
- SpringBoot入门 (一) HelloWorld
一 什么是springboot springboot是一个全新的框架,它设计的目的简化spring项目的初始环境的搭建和开发,主要有以下几个特点: 1.简化初始配置 ,可与主流框架集成: 2.内置Se ...
- SpringBoot入门系列(一)如何快速创建SpringBoot项目
这段时间也没什么事情,所以就重新学习整理了Spring Boot的相关内容.今天开始整理更新Spring Boot学习笔记,感兴趣的朋友可以关注我的博客:https://www.cnblogs.com ...
- SpringBoot入门系列(五)Thymeleaf的常用标签和用法
前面介绍了Spring Boot 中的整合Thymeleaf .不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/16577 ...
- SpringBoot入门(0) HelloWorld的实现与原理分析
SpringBoot(0) HelloWorld的实现与原理分析 一.环境准备 1.1 环境约束 –jdk1.8:Spring Boot 推荐jdk1.7及以上:java version “1.8.0 ...
- SpringBoot入门之HelloWorld
1.SpringBoot简介 百度百科:Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而 ...
- SpringBoot入门系列(二)如何返回统一的数据格式
前面介绍了Spring Boot的优点,然后介绍了如何快速创建Spring Boot 项目.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/ ...
随机推荐
- JUC——线程同步锁(ReentrantReadWriteLock读写锁)
读写锁简介 所谓的读写锁值得是两把锁,在进行数据写入的时候有一个把“写锁”,而在进行数据读取的时候有一把“读锁”. 写锁会实现线程安全同步处理操作,而读锁可以被多个对象读取获取. 读写锁:ReadWr ...
- WebRtc与SIP
最近研究一下 webrtc ,看了几篇paper,之前也尝试运行验证了几个demo,现在把我的理解总结到这里. WebRTC 简介 WebRTC,名称源自网页实时通信(Web Real-Time Co ...
- 查看、生成 SSH 密钥用于安全登陆
SSH 可以用来登陆服务器,远程执行命令,并用强加密算法编码保护通信安全,目前广泛应用于远程命令控制.文件加密传输等方面.SSH 登陆服务器的方法一般有两种:密码登陆和密钥登陆. 在受信任的设备上使用 ...
- 第二次作业(homework-02)成绩公布
学位后三位和对应成绩: 057 0008 4011 4012 7014 5015 5017 6018 0019 0026 2027 7036 0038 7.5046 7048 6.5051 0061 ...
- 如何使用g++编译调用dll的c++代码
本文将有以下4个部分来讲如何使用g++编译调用dll的c++代码. 1.如何调用dll 2.动态链接和静态链接的区别 3.g++的编译参数以及如何编译调用dll的c++代码 4.总结 1.如何调用dl ...
- kafka启动报错:另一个程序正在使用此文件,进程无法访问。
在Windows上启动kafka_2.12-1.1.0报以下错误:[2018-05-08 10:24:51,777] ERROR Failed to clean up log for __consum ...
- java的(PO,VO,TO,BO,DAO,POJO)类名包名解释
VO:值对象.视图对象 PO:持久对象 QO:查询对象 DAO:数据访问对象——同时还有DAO模式 DTO:数据传输对象——同时还有DTO模式 PO:全称是persistant object持久对象最 ...
- 1001. A+B Format (20)题解
git链接 作业描述 Calculate a + b and output the sum in standard format -- that is, the digits must be sepa ...
- Internet History, Technology and Security (Week8)
Week 8 This week we start two weeks of Internet Security. It is a little technical but don't worry - ...
- 解决亚马逊云服务器上安装nginx后无法访问的问题
在亚马逊云服务器上装了Ubuntu系统,使用docker环境搭建nginx,启动nginx容器后,在浏览器输入地址后,显示连接超时. 在网上查了一下说有可能是服务器安全组的设置问题 然后在云服务器的安 ...