第二篇文章:浏览器向服务器发送请求,服务器向浏览器响应数据
springmvc讲的就是两件事:浏览器向服务器发送请求,服务器向浏览器响应数据
浏览器向服务器提交请求
1.当你不指定请求方式是get还是post的时候,默认就是采用Get方式提交请求
你在浏览器地址栏中输入alpha/http就可以访问这个方法http
可以看出:请求默认是get请求(而不是post请求)
@RequestMapping("/alpha")
public class Controller
{
@RequestMapping("/http")
public void http(HttpServletRequest request, HttpServletResponse response) throws IOException
{
System.out.println(request.getMethod());//请求方式 GET
System.out.println(request.getServletPath());//请求路径 /alpha/http
Enumeration<String> enumeration=request.getHeaderNames();//把请求的消息头打印出来host: localhost:8080....
while(enumeration.hasMoreElements())//把请求头的每一行name:value取出来
{
String name=enumeration.nextElement();
String value=request.getHeader(name);
System.out.println(name+": "+value);
}
System.out.println(request.getParameter("code"));
//请求传入的参数,比如你输alpha/http?code=123,表明传入的参数就是123,那code就等于123,没有参数就是nul
}
}
2.指定以get方式提交请求
@RequestMapping("/students",method= RequestMethod.GET)
@ResponseBody //表示返回的是一个字符串
//第几页,1页展示几行数据
public String getStudents(int current, int limit)
{
System.out.println(current);
System.out.println(limit);
return "some student";
}
浏览器中输入 /student?1&10就可以访问这个方法getStudents()
3.指定以post方式提交请求
由于用get方式提交请求的时候,传参,参数是写在url地址里面的,即:明着传参,这样很容易暴露隐私,尤其是当参数里面有密码的时候
而且url地址的长度是有限制的,不能太长,所以get请求把参数带在url地址里面,很有可能一部分参数由于超过url地址,没有办法传给服务器
所以我们通常更喜欢用post方式提交请求,而不是get方式提交请求
4.浏览器向服务器提交表单
表单:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>增加学生</title>
</head>
<body>
<form method="post" action="/alpha/student">
<p>
姓名:<input type="text" name="name">
</p>
<p>
姓名:<input type="text" name="age">
</p>
<p>
姓名:<input type="submit" value="保存">
</p>
</form>
</body>
</html>
此时你在浏览器中输入http://localhost:8080/html/student.html,就可以访问这个表单了:
当你点击保存的时候,就会将数据提交给这个方法(即自动跳转到http://localhost:8080/alpha/student这个页面
@Controller
@RequestMapping("/alpha")
public class AlphaController
{
@RequestMapping(path = "/student",method= RequestMethod.POST)
@ResponseBody
public String saveStudent(String name,int age)
{
System.out.println(name);
System.out.println(age);
return "success";
}
}
这样就把表单里面的姓名和年龄这两个值传给了name和age
以上说的都是浏览器向服务器提交请求,接下来介绍服务器向浏览器返回响应
1.响应一个html页面
step1.在template文件夹下面新建一个文件:叫做view.html,这个文件就是我们创建的html模板
<!DOCTYPE html>
<!--xmlns:th表示当前html文件是一个模板,这个模板来自thymeleaf官网-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Teacher</title>
</head>
<body>
<p th:text="${name}"> </p>
<p th:text="${age}"> </p>
</body>
</html>
step2:url中输入http://localhost:8080/alpha/teacher,调用这个方法,将模板html中的name,age换成我们希望换成的name和age(张三和30)
返回Model相关的数据和视图相关的数据,将这些数据提交给模板引擎进行渲染,生成一个html页面
@Controller
@RequestMapping("/alpha")
public class AlphaController
{
@RequestMapping(path="/teacher",method = RequestMethod.GET)
public ModelAndView getTeacher()
{
ModelAndView mav=new ModelAndView();
mav.addObject("name","张三");
mav.addObject("age",30);
//这里/demo/view其实是/demo/view.html
mav.setViewName("/demo/view");
return mav;
}
}
最终呈现给用户的是:
下面这种方式也可以,更简洁,更推荐使用:
@Controller
@RequestMapping("/alpha")
public class AlphaController
{ @RequestMapping(path="/teacher",method = RequestMethod.GET)
public String getTeacher(Model model)
{
model.addAttribute("name","张三");
model.addAttribute("age",30);
return "/demo/view";
}
}
2.响应是json数据
通常是异步请求的时候。会返回json数据
什么是异步请求异步请求简述_Pr Young的博客-CSDN博客_网页异步请求
json数据:是一种具有特定格式的字符串
我们先将java对象转成JSON字符串,浏览器再将JSON字符串转成JS对象
由于JSON数据是通用格式,什么语言都能很轻易将其转化成自己语言里面的对象,所以Json成为中间桥梁的作用
也就是说,在跨语言中,json就是不同语言转化的桥梁
step1:
@Controller
@RequestMapping("/alpha")
public class AlphaController
{
@RequestMapping(path="/emp",method = RequestMethod.GET)
@ResponseBody
public Map<String,Object> getEmp()
{
Map<String,Object> emp=new HashMap<>();
emp.put("name", "张三");
emp.put("age",23);
emp.put("salary",8000.00);
return emp;
}
}
step2:在浏览器中输入http://localhost:8080/alpha/emp