------------恢复内容开始------------
(一)获取servletAPI
(二) controller的返回值类型
返回ModelAndView
返回字符串(jsp页面)(普通字符串)(转发和重定向)
package stu.adam.springmvc.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.util.Map; /** * @program: SpringMVC-2 * @description: * @author: adam * @create: 2021-12-17 15:15 **/ @Controller @RequestMapping("user") public class ServletApiTest { @GetMapping("getservaip") public ModelAndView getservaip(HttpServletRequest request, HttpServletResponse response, HttpSession session) { ModelAndView modelAndView=new ModelAndView(); modelAndView.setViewName("success"); System.out.println("request===="+request); HttpSession session1 = request.getSession(); System.out.println("request 获得的 session==="+session1); System.out.println("response===="+response); System.out.println("session===="+session); String value = request.getHeader("Connection"); System.out.println("Connection=="+value); String host = request.getHeader("Host"); System.out.println("host==="+host); String userAgent = request.getHeader("User-Agent"); System.out.println("userAgent==="+ userAgent); System.out.println(request.getRequestURI()); System.out.println(session); //获取contextPath ServletContext context = request.getServletContext().getContext("/"); String contextPath1 = context.getContextPath(); System.out.println("request.getServletContext().getContext======="+contextPath1); String contextPath = request.getContextPath(); System.out.println("request.getContextPath()===="+contextPath); return modelAndView; } //model传送字符串 @RequestMapping("string") public String gojsp(Model model){ model.addAttribute("msg","hello model"); // jsp路径(由springmvc)拦截得到 return "user"; } //map传输 @RequestMapping("map") public String gojsp2(Map map){ map.put("msg","hello map!"); return "user"; } //字符串传输(就是直接传送一坨字符串过去) @RequestMapping("CommonStr") //将对象转换为Json //@ResponseBody public String gojsp3(){ //页面就是显示此字符串 return "我是你姐"; } //= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = //补充 :转发和重定向的区别 /* * * 1、请求次数 浏览器-》服务器->新的服务器(客户端行为) (服务器行为)服务器->服务器 重定向是浏览器向服务器发送一个请求并收到响应后再次向一个新地址发出请求,转发是服务器收到请求后为了完成响应跳转到一个新的地址;重定向至少请求两次,转发请求一次; 2、地址栏不同 重定向地址栏会发生变化,转发地址栏不会发生变化; 3、是否共享数据 重定向两次请求不共享数据,转发一次请求共享数据(在request级别使用信息共享,使用重定向必然出错); 4、跳转限制 重定向可以跳转到任意URL,转发只能跳转本站点资源; 5、发生行为不同 重定向是客户端行为,转发是服务器端行为; * */ //字符串 转发和重定向 //forward 转发 @RequestMapping("forward") public String forward() { System.out.println("forward~"); return "forward:map"; } //forward 重定向 @RequestMapping("redirect") private String redirect(){ System.out.println("redirect"); return "redirect:string"; } }
(三):Rest风格规范
对于请求方式规范 不同的操作 使用不同的请求方式
-
get: 请求一般是获取数据
-
post: 登录和添加数据
-
put: 一般是修改
-
delete : 删除操作
用路径拼接参数
效果:
第二种写法:(没啥意义)
部分状态码认识:
200 成功 300 重定向 400 客户端错误 401 用户没有权限令牌 403 用户得到授权,但是访问是被禁止的 404 访问的资源找不到 500 服务器内部错误
201 表示创建成功
(四)文件上传的几种方式:
part上传:
代码:
1.编写Jsp
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2021/12/17 Time: 10:44 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <h1 style="background-color: pink">文件上传</h1> <form action="submit" method="post" enctype="multipart/form-data"> 上传文件:<input type="file" name="imgfile"><br> <input type="submit" value="上传"> </form> $END$ </body> </html>
2.控制层编写
@Controller public class UploadController { @RequestMapping("submit") public String submit(HttpServletRequest request) throws IOException, ServletException { //准备上传路径 ServletContext servletContext = request.getServletContext(); String path=servletContext.getRealPath("/upload/img1"); File file = new File(path); //不存在就创建 if(!file.exists()) { file.mkdirs(); } //获取part Part filePart=request.getPart("imgfile"); //获取文件名 String filename=filePart.getSubmittedFileName(); //上传 filePart.write(path+filename); return "upload success!"; } }
3.把控制文件大小的代码粘到web.xml下面
页面:
上传结果(文件名出现乱码,不过上传成功了):
4.修改controller给图片改名(修改后control层)
@Controller public class UploadController { @RequestMapping("submit") public String submit(HttpServletRequest request) throws IOException, ServletException { //准备上传路径 ServletContext servletContext = request.getServletContext(); String path=servletContext.getRealPath("/upload/img1"); File file = new File(path); //不存在就创建 if(!file.exists()) { file.mkdirs(); } //获取part Part filePart=request.getPart("imgfile"); //获取文件名 String filename=filePart.getSubmittedFileName(); //《===改名开始===》 String filenameExtension = StringUtils.getFilenameExtension(filename); //生成随机名 String uuid = UUID.randomUUID().toString(); String uuidFileName=uuid.replaceAll("-",""); //替换 String newFileName=uuidFileName+"."+filenameExtension; //《===改名结束===》 //上传 filePart.write(path+newFileName); return "success"; } }
5.效果
----part上传单文件(完)
----part上传多文件(开始)
jsp改写:
<h1 style="background-color: pink">多文件上传</h1> <form action="submits" method="post" enctype="multipart/form-data"> 上传文件:<input type="file" name="imgfiles" multiple><br> <input type="submit" value="上传"> </form>
controller层改写:
@RequestMapping("submits") public String submits(HttpServletRequest request) throws IOException, ServletException { //准备上传路径 ServletContext servletContext = request.getServletContext(); String path=servletContext.getRealPath("/upload/imgs"); File file = new File(path); //不存在就创建 if(!file.exists()) { file.mkdirs(); } //获取part Collection<Part> imgfiles = request.getParts(); imgfiles.forEach( part -> { //获取文件名 String filename=part.getSubmittedFileName(); //《===改名开始===》 String filenameExtension = StringUtils.getFilenameExtension(filename); //生成随机名 String uuid = UUID.randomUUID().toString(); String uuidFileName=uuid.replaceAll("-",""); //替换 String newFileName=uuidFileName+"."+filenameExtension; //《===改名结束===》 //上传 // file.write(path+newFileName); try { part.write(path+newFileName); } catch (IOException e) { e.printStackTrace(); } } ); return "success"; }
效果:
----part上传多文件(完)
----fileupload+io上传文件开始
1.导入jar包
2.刚才写part方式就配过了(part.3),这边就不配了 /第二种方式(附录1)
3.jsp
4.controller层
注意事项:
----fileupload+io上传文件结束
附录1:
在spring文件下面加这句话即可:
笔记完