Springmvc
它能解决什么问题?
用request接收浏览器发过来的数据,用response返回数据给浏览器,代码量小
它是什么?
是个web控制层框架
是springweb,实现url映射,接收参数,返回数据等。
mvc分层 分包:view model controller,
小型项目不需要用到controller管理,
view 发一个请求,请求先到controller,controller找到一个model处理,
2001年,View(完成model),没有controller
2005年,view+model(.java)
2010年,mvc
如何用?
创建一个springboot项目,添加spring web依赖
@restController
创建一个主类:UserController
最简单的写法:
UserController
{
@getMapping("reg")
register(String username){
}
}
我们做项目就是组装,就好比生活中:建一栋楼,你安装的门,安装的电梯,都是别人做好了的,拿过来用,安装就行。
往期文章:
JAVA进阶 springboot框架基础
JAVA进阶 Tomcat入门教学
maven 安装配置与基础概念(超详细 手把手教你学会)
表现层的三大任务
URL到controller的映射
http请求参数绑定
http响应的生成和输出
性能超群
简单易用性能佳
8.2接收参数
导入生成的项目
将网站生成的springmvc01request.zip拷贝到eclipse工作区中,解压 File→import
Get请求
分析
选中com.tedu.springmvc01request 右键创建一个主包
在com.tedu.springmvc01request.controller包中创建一个UserController.java类
后端
package com.tedu.springmvc01request.controller.copy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
//完成注册功能,浏览器发get请求
@GetMapping("reg1")
public String regString(String username,String password) {
return "注册成功";
}
}
在static中创建一个index.html
前端
action 属性规定当提交表单时,向何处发送表单数据
表单数据将通过 method 属性附加到 URL 上:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
get<br>
<!-- console关闭以前启动所有程序
application.properties server.port=1314
找到main() run as java application
console必须显示tomcat started on ports 8080
http://localhost:8080/index.html
浏览器地址栏会显示输入的username,passwordS
-->
<form action = "/reg1" method="get">
<input name = "username">
<input name = "password">
<input type = "submit" value="register">
</form>
</body>
</html>
http://localhost:8080/index.html
浏览器地址栏会显示输入的username,password
打开Springmvc01requestApplication类 Run As 运行
运行结果:
get请求的信息显示在浏览器的地址栏中,不安全
Post请求
分析
前端
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
get<br>
<!-- console关闭以前启动所有程序
application.properties server.port=1314
找到main() run as java application
console必须显示tomcat started on ports 8080
http://localhost:8080/index.html
浏览器地址栏会显示输入的username,passwordS
-->
<form action = "/reg1" method="get">
<input name = "username">
<input name = "password">
<input type = "submit" value="register">
</form>
post<br>
<form action = "/reg2" method="post">
<input name = "username">
<input name = "password">
<input type = "submit" value="register">
</form>
</body>
</html>
后端
package com.tedu.springmvc01request.controller.copy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
//完成注册功能,浏览器发get请求
@GetMapping("reg1")
public String regString(String username,String password) {
return "注册成功";
}
//完成注册功能,浏览器发Post请求
@PostMapping("reg2")
public String regString2(String username,String password) {
return "注册成功";
}
}
http://localhost:8080/index.html
运行结果:
post请求的信息不显示在浏览器的地址栏中,安全
Get请求加@postMapping的方法会报错
新建一个application.yml 更改端口号为1314
requestMapping可以处理get和post请求
在Spring MVC 中使用 @RequestMapping 来映射请求,也就是通过它来指定控制器可以处理哪些URL请求,相当于Servlet中在web.xml中配置
前端:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
get<br>
<!-- console关闭以前启动所有程序
application.properties server.port=1314
找到main() run as java application
console必须显示tomcat started on ports 8080
http://localhost:8080/index.html
浏览器地址栏会显示输入的username,passwordS
-->
<form action = "/reg1" method="get">
<input name = "username">
<input name = "password">
<input type = "submit" value="register">
</form>
post<br>
<form action = "/reg2" method="post">
<input name = "username">
<input name = "password">
<input type = "submit" value="register">
</form>
post<br>
<form action = "/reg3" method="post">
<input name = "username">
<input name = "password">
<input type = "submit" value="register">
</form>
</body>
</html>
后端
package com.tedu.springmvc01request.controller.copy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
//完成注册功能,浏览器发get请求
@GetMapping("reg1")
public String regString(String username,String password) {
return "注册成功";
}
//完成注册功能,浏览器发Post请求
@PostMapping("reg2")
public String regString2(String username,String password) {
return "注册成功";
}
//完成注册功能,能接收get和post两种请求
//index.html两个form action=reg3
@RequestMapping("reg3")
public String regString3(String username,String password) {
return "注册成功";
}
}
运行结果页面展示:
接收对象
接收参数的应用场景:
1,baidu接收关键字,返回搜索内容。
2,京东,淘宝接收关键字,返回商品。
3,微信接收添加好友的手机号,返回用户。
网站提供服务的基本流程是接收数据,返回结果。谁负责显示结果?谁负责存储数据。
创建一个User.java类
package com.tedu.springmvc01request.controller.copy;
public class User {
String username;
String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.tedu.springmvc01request.controller.copy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
//完成注册功能,浏览器发get请求
@GetMapping("reg1")
public String regString(String username,String password) {
return "注册成功";
}
//完成注册功能,浏览器发Post请求
@PostMapping("reg2")
public String regString2(String username,String password) {
return "注册成功";
}
//完成注册功能,能接收get和post两种请求
//index.html两个form action=reg3
@RequestMapping("reg3")
public String regString3(String username,String password) {
return "注册成功";
}
//给方法传参数,参数很多。创建一个类
@RequestMapping("reg4")
//console关闭程序
//main() run as tomcat started on ports
//地址栏直接输入
//http://localhost:8080/reg4?username=a&username=a&password=1
//开一个窗口
//http://localhost:8080/reg4?username=b&password=2
public String register4(User user) {
return "注册成功 username= "+user.getUsername()+"<br>"+this.toString();
}
}
浏览器地址栏直接输入:
http://localhost:8080/reg4?username=a&username=a&password=1
开一个窗口
http://localhost:8080/reg4?username=b&password=2
网页显示结果
Request,response对象
Request
package com.tedu.springmvc01request.controller.copy;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
//完成注册功能,浏览器发get请求
@GetMapping("reg1")
public String regString(String username,String password) {
return "注册成功";
}
//完成注册功能,浏览器发Post请求
@PostMapping("reg2")
public String regString2(String username,String password) {
return "注册成功";
}
//完成注册功能,能接收get和post两种请求
//index.html两个form action=reg3
@RequestMapping("reg3")
public String regString3(String username,String password) {
return "注册成功";
}
//给方法传参数,参数很多,创建一个类
@RequestMapping("reg4")
//console关闭程序
//main() run as tomcat started on ports
//地址栏直接输入
//http://localhost:8080/reg4?username=a&username=a&password=1
//开一个窗口
//http://localhost:8080/reg4?username=b&password=2
public String register4(User user) {
//通过user对象接收账号密码
return "注册成功 username= "+user.getUsername()+"<br>"+this.toString();
}
//requeset用来接收数据
//register(String username) 框架底层用的是request,前面写的框架就用到
//工作中用register(User user)
@RequestMapping("/reg5")
//http://localhost:8080/reg5?username=a&password=1
public String register5(HttpServletRequest request) {
String username=request.getParameter("username");
String password=request.getParameter("password");
String userIp=request.getRemoteAddr();
String s=username+","+password+","+userIp;
return s;
}
}
浏览器:http://localhost:8080/reg5?username=a&password=1
获取返回账号密码:a ,1
网页运行结果:
Response
package com.tedu.springmvc01request.controller.copy;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
//完成注册功能,浏览器发get请求
@GetMapping("reg1")
public String regString(String username,String password) {
return "注册成功";
}
//完成注册功能,浏览器发Post请求
@PostMapping("reg2")
public String regString2(String username,String password) {
return "注册成功";
}
//完成注册功能,能接收get和post两种请求
//index.html两个form action=reg3
@RequestMapping("reg3")
public String regString3(String username,String password) {
return "注册成功";
}
//给方法传参数,参数很多,创建一个类
@RequestMapping("reg4")
//console关闭程序
//main() run as tomcat started on ports
//地址栏直接输入
//http://localhost:8080/reg4?username=a&username=a&password=1
//开一个窗口
//http://localhost:8080/reg4?username=b&password=2
public String register4(User user) {
//通过user对象接收账号密码
return "注册成功 username= "+user.getUsername()+"<br>"+this.toString();
}
//requeset用来接收数据
//register(String username) 框架底层用的是request,前面写的框架就用到
//工作中用register(User user)
@RequestMapping("/reg5")
//http://localhost:8080/reg5?username=a&password=1
public String register5(HttpServletRequest request) {
String username=request.getParameter("username");
String password=request.getParameter("password");
String userIp=request.getRemoteAddr();
String s=username+","+password+","+userIp;
return s;
}
//用Response返回数据给浏览器
@RequestMapping("/reg6")
//http://localhost:8080/reg6
//requeset用来接收数据,功能强大
//register(String username) 框架底层用的是request
public void register6(HttpServletResponse response) throws IOException {
//告诉浏览器返回的是html还是json
response.setContentType("text/html");
//告诉浏览器返回内容的编码,避免出现乱码
response.setCharacterEncoding("UTF-8");
//PrintWriter写数据
PrintWriter printWriter=response.getWriter();//抛异常
printWriter.println("注册成功");
}
}
浏览器: http://localhost:8080/reg6
Cookie
识别用户身份
查看jd,baidu的cookie
在chrome浏览器中单击网站前的图标,可以查看cookie.
Cookie 是一些数据, 存储于你电脑上的文本文件中。
当 web 服务器向浏览器发送 web 页面时,在连接关闭后,服务端不会记录用户的信息。
Cookie 的作用就是用于解决 “如何记录客户端的用户信息”:
当用户访问 web 页面时,他的名字可以记录在 cookie 中。
在用户下一次访问该页面时,可以在 cookie 中读取用户访问记录。
其他内容不变,在后面加上此代码就行
//登录后,服务器往浏览器中写cookie
@RequestMapping("/login")
//http://localhost:8080/login?username=drqf
//
public String login(String username,String password,HttpServletResponse response) {
//创建两个cookie
Cookie cookie = new Cookie("username", username);
Cookie cookie2 = new Cookie("password", password);
//把cookie发给浏览器
response.addCookie(cookie);
response.addCookie(cookie2);
return "往浏览器写入cookie"+username;
}
网页运行效果:
读取cookie
F12打开调试面板network →response headers →刷新一下页面→如果里面能查看到服务器返回的cookie
接下来我们新创建一个类,实现添加购物车
CartController.java
package com.tedu.springmvc01request.controller.copy;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//购物车
@RestController
public class CartController {
//读取浏览器中的cookie
@RequestMapping("/add")
//f12 打开浏览器调试器
//在console中关闭服务器,重启,
//以前的cookie没了 localhost:8080/login?username=admin
//localhost:8080/listOrder
//network-->request headers cookie:username=admin
public String add(String itemName,HttpServletRequest request) {
//从request中读取cookie
Cookie[] cookies=request.getCookies();
String string="";
if(cookies!=null) {
for (Cookie cookie:cookies) {
//取cookie的名称
String name=cookie.getName();
//取cookie的内容
String content=cookie.getValue();
string=string+name+"="+content+"<br>";
}
}
//把用户名和商品名写入数据库。
return string;
}
}
网页返回效果:
F12打开调试面板networkrequest headers中能查看到浏览器发给服务器的cookie
常见错误:
- application.properties,application.yml
空格没写
server:
port: 1314
-
手动点console中的红色按钮关闭程序,点console中显示器,列出启动的所有程序,找出来以前启动的程序,按钮变红,才能结束。
启动成功后,tomcat started on ports 1314 -
404
原因:
1.地址写错了
2.服务器没启动
3.新加的方法没有生效