根据咱们程序员学习的惯例,学习一门新技术都是从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
猜你喜欢
转载自www.cnblogs.com/qwangxiao/p/10387214.html
今日推荐
周排行