攀枝花网站建设,北京西站出站口,8080端口wordpress,seo概念目录
SpringMVC
1、MVC定义
2、MVC和SpringMVC之间的关系
学SpringMVC
1、Spring MVC的创建和连接
浏览器获取前端接口和后端程序连接功能实现
2、获取参数
2.1、传递单个参数/多个参数
2.2、传递对象 2.3、传递表单参数
2.4、后端参数重命名
2.5、RequestBody接收J…目录
SpringMVC
1、MVC定义
2、MVC和SpringMVC之间的关系
学SpringMVC
1、Spring MVC的创建和连接
浏览器获取前端接口和后端程序连接功能实现
2、获取参数
2.1、传递单个参数/多个参数
2.2、传递对象 2.3、传递表单参数
2.4、后端参数重命名
2.5、RequestBody接收JSON对象
2.6、获取URL中的参数PathVariable
2.7、上传文件RequestPart
2.8、获取Cookie/Session/header
3、返回数据
3.1、返回静态页面
3.2、返回text/html
3.3、返回JSON对象
3.4、请求转发或者请求重定向 SpringMVC
1、构建在ServletAPI之上的
2、是一个Web框架具备http
3、来自于Spring webMVC模块
1、MVC定义
MVC开始是存在于桌面程序中的M是指业务模型V是指用户界面C则是控制器使用MVC的目的是将M和V的实现代码分离从而使同一个程序可以使用不同的表现形式。 M即model模型是指模型表示业务规则。在MVC的三个部件中模型拥有最多的处理任务。被模型返回的数据是中立的模型与数据格式无关这样一个模型能为多个视图提供数据由于应用于模型的代码只需写一次就可以被多个视图重用所以减少了代码的重复性。
V即View视图是指用户看到并与之交互的界面。比如由html元素组成的网页界面或者软件的客户端界面。MVC的好处之一在于它能为应用程序处理很多不同的视图。在视图中其实没有真正的处理发生它只是作为一种输出数据并允许用户操作的方式。
C即controller控制器是指控制器接受用户的输入并调用模型和视图去完成用户的需求控制器本身不输出任何东西和做任何处理。它只是接收请求并决定调用哪个模型构件去处理请求然后再确定用哪个视图来显示返回的数据。
2、MVC和SpringMVC之间的关系
springmvc提供了 前端控制器 DispatcherServlet仅针对对客户端的请求和响应进行统一 处理就是说封装了servlet是个框架 。是针对三层架构的表述层或表示层 开发提供的框架 而mvc是一种思想。对于原先的mvc思想来说springmvc只是包含了mvc思想的一部分 Controller进行实现。
当用户在浏览器中输入url之后SpringMVC项目就可以感知用户的请求
学SpringMVC
1、构建在servletAPI之上
2、是一个Web框架HTTP
3、来自于Spring webMVC模块
1、Spring MVC的创建和连接 RequestMapping(/test) //路由注册
//Controller //程序返回的是数据而非页面
//ResponseBody //只有加载的类别人才能访问
RestController
public class TestConstroller {RequestMapping(/hi)public String sayHi(){return hi,spring mvc;}
}
RequestMapping既可以修饰类也可以修饰方法。当修饰类和方法时访问的地址是类方法
方法地址localhost:8080/test/hi
浏览器获取前端接口和后端程序连接功能实现
1.1、RequestMapping( /xxx )既能修饰类又能修饰方法
特性:既支持GET方式的请求又支持POST方式的请求...
1.2、RequestMapping设置只支持GET/POST请求
RequestMapping(/test) //路由注册
//Controller //程序返回的是数据而非页面
//ResponseBody //只有加载的类别人才能访问
RestController
public class TestConstroller {//RequestMapping(/hi)RequestMapping(value /hi,method RequestMethod.POST)public String sayHi(){return hi,spring mvc;}
}
1.3、PostMapping
RequestMapping(/test) //路由注册
//Controller //程序返回的是数据而非页面
//ResponseBody //只有加载的类别人才能访问
RestController
public class TestConstroller {//RequestMapping(/hi)RequestMapping(value /hi,method RequestMethod.POST)PostMapping(/hi)public String sayHi(){return hi,spring mvc;}
}
1.4、GetMapping
RequestMapping(/test) //路由注册
//Controller //程序返回的是数据而非页面
//ResponseBody //只有加载的类别人才能访问
RestController
public class TestConstroller {//RequestMapping(/hi)RequestMapping(value /hi,method RequestMethod.POST)GetMapping(/hi)public String sayHi(){return hi,spring mvc;}
}
2、获取参数
2.1、传递单个参数/多个参数
RequestMapping(/test) //路由注册
//Controller //程序返回的是数据而非页面
//ResponseBody //只有加载的类别人才能访问
RestController
public class TestConstroller {GetMapping(/hi)public String sayHi(String name){return hiname;}
} 传参注意事项在Spring BootSpring MVC中传参一定要传包装类型而不是基础类型 原因当需要一个基础类型但又忘记传递时使用基础类型会报错500而包装类型不会错只是值为null GetMapping(/num)public String getnum(Integer num){return numnum;} GetMapping(/hi)public String sayHi(String name,Integer age){return hiname age:age;} 2.2、传递对象
后端实现
Data
public class User {private int id;private String name;private int age;
} GetMapping(/showuser)public String showu(User user){return user.toString();}
前端实现 2.3、传递表单参数
当参数较少时可以使用传递多个参数的方法
当参数较多时可以使用传递对象的方法
2.4、后端参数重命名
后端实现 GetMapping(/showtime)public String shoeTime(RequestParam(t) String startTime,RequestParam(t2) String endTime){return 开始时间 startTime 结束时间 endTime;}
前端实现 如果t值不是必须要传 GetMapping(/showtime)public String shoeTime(RequestParam(value t,required false) String startTime,RequestParam(t2) String endTime){return 开始时间 startTime 结束时间 endTime;}
2.5、RequestBody接收JSON对象
后端实现 //RequestBody接收JSON对象PostMapping(/show-json-user)public String showJSONuser(RequestBody User user){return user.toString();}
前端实现
2.6、获取URL中的参数PathVariable
后端实现 RequestMapping(/login/{username}/{password})public String login(PathVariable(username)String username,PathVariable(password)String password){return username : password;}
前端实现 2.7、上传文件RequestPart
后端实现 RequestMapping(/upfile)public String uofile(RequestPart(myfile)MultipartFile file) throws IOException {String pathD:\\baekhyun\\bobo.jpg;file.transferTo(new File(path));return path;}
前端实现 最终上传文件后端实现 RequestMapping(/finalfile)public String finalupfile(RequestPart(myfile)MultipartFile file) throws IOException {//根目录String pathD:\\baekhyun\\;//根目录【唯一的文件名】path UUID.randomUUID().toString();//根目录唯一的文件名文件的后缀pathfile.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(.));file.transferTo(new File(path));return path;}
前端实现 2.8、获取Cookie/Session/header
1、获取Cookie //获取全部CookieRequestMapping(/getck)public String getCookie(HttpServletRequest request){Cookie[] cookiesrequest.getCookies();for (Cookie item:cookies){log.error(item.getName():item.getValue());}return get cookie;}
2、获取单个Cookie CookieValue //获取单个CookieRequestMapping(/getck2)public String getCookie2(CookieValue(do) String val){return cookie valueval;}
3、获取Header RequestHeader
后端实现 //获取headerRequestMapping(/getheader)public String getheader(RequestHeader(User-Agent) String userAgent){return userAgent;}
前端实现 4、存Session
后端实现 //存SessionRequestMapping(/setsession)public String setSession(HttpServletRequest request){HttpSession sessionrequest.getSession();session.setAttribute(userinfo,userinfo);return set Session success;} 前端实现 5、获取Session
后端实现 //读SessionRequestMapping(getsession)public String getSession(HttpServletRequest request){HttpSession sessionrequest.getSession(false);if (session!null session.getAttribute(userinfo)!null){return (String) session.getAttribute(userinfo);}else {return 暂无session信息;}}
前端实现 RequestMapping(/getsession2)public String getSession2(SessionAttribute(value userinfo,required false)String userinfo){return userinfo;}
3、返回数据
3.1、返回静态页面
后端实现
RequestMapping(/resp)
Controller
public class RespController {RequestMapping(/hi)public String sayHi(){return /hello.html;}
} 前端实现 3.2、返回text/html
案例计算器的实现
ResponseBody
Controller
public class CalcController {RequestMapping(/calc)public String calc(Integer num1,Integer num2){if (num1null || num2null) return 参数错误;return 结果为 (num1num2);}
}
!DOCTYPE html
html langen
headmeta charsetUTF-8titleTitle/title
/head
bodyform action/calcdiv stylemargin-top: 100px;text-align: center;h1计算器/h1数字1input namenum1br数字2input namenum2brinput value提交 typesubmit/div/form/body
/html
3.3、返回JSON对象
后端实现 RequestMapping(/respjson)public HashMapString,String respJson(){HashMapString,String mapnew HashMap();map.put(baekhyun,baekhyun.value);map.put(do,do.value);map.put(sehun,sehun.value);return map;}
前端实现 3.4、请求转发或者请求重定向
forward和redirect forward和redirect具体区别如下: 1.请求重定向(redirect) 将请求重新定位到资源;请求转发(forward) 服务器端转发。 2.请求重定向地址发生变化,请求转发地址不发生变化。 3.请求重定向与直接访问新地址效果一致, 不存在原来的外部资源不能访问;请求转发服务器端转发有可能造成原外部资源不能访问。