佛山做网站-准度科技公司,大数据精准客户,自己ip做网站,公司做网页要多少钱接收参数的常用注解1.RequestParam注解作用#xff1a;把请求中的指定名称的参数传递给控制器中的形参赋值属性#xff1a;value#xff1a;请求参数中的名称#xff0c;指定请求参数名与方法参数名的映射关系#xff08;解决名称不一致的问题#xff09;#xff1b;req…接收参数的常用注解1.RequestParam注解作用把请求中的指定名称的参数传递给控制器中的形参赋值属性value请求参数中的名称指定请求参数名与方法参数名的映射关系解决名称不一致的问题required请求参数中是否必须提供此参数默认值是true表示必须提供请求参数若未传会抛异常设为false则允许为空defaultValue如果没有传请求参数使用该默认值。Controller RequestMapping(/dept) public class DeptController { /** * RequestParam注解 */ RequestMapping(/save1.do) public String save(RequestParam(value username, required false, defaultValue abc) String name) { System.out.println(姓名 name); return suc; } }2.RequestBody注解作用将整个请求体的内容作为字符串接收注意get方法不可以required属性表示是否必须有请求体默认值是truerequestBody注解常用的使用方式有两种将json格式的数据绑定到对应的实体类中后端RequestBody注解对应的类在将HTTP的输入流(含请求体)装配到目标类(即:RequestBody后面 的类)时会根据json字符串中的key来匹配对应实体类的属性如果匹配一致且json中的该key对应的值符合(或可转换为)实体类的对应属性的类型要求时会调用实体类的setter方法将值赋给该属性将json格式的数据按照key值分别赋值在对应的字符串中使用methodpost提交时参数不会拼在 URL 后面而是被封装到请求体(Request Body)中格式为 keyvaluekeyvalue。h3请求参数绑定RequestBody注解方式一/h3 form action/dept/save2.do methodpost 姓名input typetext namesname /br/ 年龄input typetext nameage /br/ input typesubmit value提交 nameaaa/br/ /form h3请求参数绑定RequestBody注解方式二/h3 form action/dept/save3.do methodpost 姓名input typetext namesname /br/ 年龄input typetext nameage /br/ input typesubmit value提交 nameaaa/br/ /formController RequestMapping(/dept) public class DeptController { /** * RequestBody注解绑定到对应的实体类中 */ RequestMapping(/save2.do) public String save2(RequestBody String body){ System.out.println(请求体内容body); return suc; } /** * RequestBody注解别赋值在对应的字符串中 */ RequestMapping(/save3.do) public String getUser(RequestBody String sname,RequestBody String age){ System.out.println(sname); System.out.println(age); return suc; } }打印结果3.RequestHeader注解作用获取指定请求头的信息value属性请求头的名称servlet 中获取指定请求头信息的方式Controller RequestMapping(/user) public class UserController { /** * servlet提供的获取请求头信息 */ RequestMapping(/save7.do) public String save7(HttpServletRequest request, HttpServletResponse response) { System.out.println(request.getParameter(username)); EnumerationString headerNames request.getHeaderNames(); while (headerNames.hasMoreElements()) { String headerName (String) headerNames.nextElement(); String headerValue request.getHeader(headerName); System.out.println(headerName : headerValue); } return suc; } }request 是 HttpServletRequest 类型的对象代表客户端发送的 HTTP 请求Enumeration headerNames request.getHeaderNames(); 即通过 getHeaderNames() 获取所有 HTTP 请求头名称的枚举对象while (headerNames.hasMoreElements()) { ... } 循环遍历枚举对象其中 hasMoreElements() 表示判断是否还有未处理的请求头名称headerNames.nextElement(); 获取下一个请求头的名称request.getHeader(headerName); 根据名称获取对应的请求头值。h3获取请求头信息servlet提供/h3 form action/user/save7.do methodpost 姓名input typetext nameusername /br/ 年龄input typetext nameage /br/ input typesubmit value提交 /br/ /form打印结果Controller RequestMapping(/user) public class UserController { /** * RequestHeader 注解 */ RequestMapping(/save8.do) public String save8(RequestHeader(cookie) String headerValue) { System.out.println(headerValue); return suc; } }其中参数cookie表示获取请求头中名为Cookie的字段注意是获取整个Cookie请求头的原始字符串h3获取请求头信息RequestHeader 注解/h3 form action/user/save8.do methodpost 姓名input typetext nameusername /br/ 年龄input typetext nameage /br/ input typesubmit value提交 /br/ /form打印结果4.CookieValue注解作用用于从请求携带的Cookie中直接获取指定名称Cookie的值value属性cookie的名称Controller RequestMapping(/user) public class UserController { /** * CookieValue注解 */ RequestMapping(/save9.do) public String save9(CookieValue(value JSESSIONID) String cookie){ System.out.println(值cookie); return suc; } }CookieValue 注解会自动从所有Cookie中提取指定名称value JSESSIONID的单个Cookie的具体值h3获取请求头cookie信息CookieValue 注解/h3 form action/user/save9.do methodpost 姓名input typetext nameusername /br/ 年龄input typetext nameage /br/ input typesubmit value提交 /br/ /form打印结果5.PathVaribale注解作用获取 URL 路径中的占位符参数适用于 RESTful 风格的 URL例如url中有/delete/{id}{id}就是占位符value属性指定url中的占位符名称若方法参数名与占位符名一致可省略Restful 风格的 URL请求路径一样可以根据不同的请求方式去执行后台的不同方法GetMapping映射 GET 请求用于查询资源PostMapping映射 POST 请求用于创建新资源PutMapping映射 PUT 请求用于全量更新资源DeleteMapping映射 DELETE 请求用于删除资源restful风格的URL优点结构清晰、符合标准、易于理解、扩展方便Controller //RequestMapping(/emp) public class EmpController{ /** * 保存 */ RequestMapping(path /emp,method RequestMethod.POST) public String save(){ System.out.println(保存员工...); return suc; } /** * 查询所有 */ RequestMapping(path /emp, method RequestMethod.GET) public String findAll(){ System.out.println(查询员工...); return suc; } /** * 查询所有 */ RequestMapping(path /emp/{id},method RequestMethod.GET) public String findById(PathVariable(value id) Integer id){ System.out.println(通过id查询员工...id); return suc; } }h3Restful风格的URL/h3 form action/emp methodpost input typesubmit value保存员工br/ /form form action/emp methodget input typesubmit value查询员工br/ /form form action/emp/1 methodget input typesubmit value通过id查询员工br/ /form打印结果响应数据和结果视图1.返回StringController RequestMapping(/redict) public class ReController { /** * 返回字符串常用 */ RequestMapping(/save1.do) public String save1(){ System.out.println(执行了save1...); return suc; } }2.返回值是void如果控制器的方法返回值编写成voidSpringMVC 不会自动进行视图解析或数据处理默认查找JSP页面没有找到执行程序报404的异常解决方式可以使用请求转发或者重定向跳转到指定的页面而非依赖 Spring MVC 的视图解析器自动跳转需要使用 Servlet 规范中的原生 API 获取请求信息和处理响应三种常见的响应方式通过 HttpServletRequest 的 getRequestDispatcher() 方法实现请求转发可以访问/WEB-INF目录下的资源以及可以携带request域中的数据到目标资源通过 HttpServletResponse 的 sendRedirect() 方法实现重定向需要注意的是不能直接访问/WEB-INF目录下的资源且request域中的数据会丢失通过 HttpServletResponse 的输出流直接向客户端输出数据不经过其他资源跳转。Controller RequestMapping(/redict) public class ReController { /** * 返回值是void */ RequestMapping(/save2.do) public void save2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println(执行了save2...); //请求转发 request.getRequestDispatcher(/WEB-INF/pages/suc.jsp).forward(request,response); //重定向 request.getContextPath() 用于获取上下文路径 //response.sendRedirect(request.getContextPath() /index.jsp); // 使用response对象直接向客户端输出响应数据 //response.getWriter().print(hello); return; } }3.返回值是ModelAndView对象ModelAndView 是 SpringMVC 提供的一个对象可同时封装模型数据和视图信息适合需要在一个对象中同时处理数据和视图的场景Controller RequestMapping(/redict) public class ReController { /** * 返回ModelAndView对象 */ RequestMapping(/save3.do) public ModelAndView save3(){ System.out.println(执行了save3...); // 创建mv对象 ModelAndView mv new ModelAndView(); // 把一些数据存储到mv对象中 mv.addObject(msg,用户名或者密码已经存在); // 设置逻辑视图的名称 mv.setViewName(suc); // 也支持redirect/forward // mv.setViewName(redirect:/index.jsp); return mv; } }补充另一种拆开的方式返回值是String视图解析器跳转至对应视图参数列表中携带model来返回数据内容代码示例Controller RequestMapping(/mtype) public class MtypeController { RequestMapping(/list) public String selectpage(MtypeQuery mq, Model model){ PageMtype page mtypeService.selectObjectByCondition(mq); model.addAttribute(page, page); return mtype; } }4.SpringMVC 框架提供的forward请求转发Controller RequestMapping(/redict) public class ReController { /** * 返回String * SpringMVC框架提供的请求转发 */ RequestMapping(/save4.do) public String save4(){ System.out.println(执行了save4...); return forward:/WEB-INF/pages/suc.jsp; } }5.SpringMVC 框架提供的redirect请求转发Controller RequestMapping(/redict) public class ReController { /** * 返回String * SpringMVC框架提供的重定向 */ RequestMapping(/save5.do) public String save5(){ System.out.println(执行了save5...); return redirect:save1.do; // 也可重定向到外部URL但无法访问WEB-INF下的资源 //return redirect:https://www.baidu.com; } }前端编写h3请求转发与重定向/h3 form action/redict/save1.do methodget input typesubmit value执行br/ /form form action/redict/save2.do methodget input typesubmit value执行br/ /form form action/redict/save3.do methodget input typesubmit value执行br/ /form form action/redict/save4.do methodget input typesubmit value执行br/ /form form action/redict/save5.do methodget input typesubmit value执行br/ /form打印结果6.ResponseBody响应json数据重要首先需要导入坐标依赖引入JSON 序列化依赖dependency groupIdcom.fasterxml.jackson.core/groupId artifactIdjackson-databind/artifactId version2.9.0/version /dependency dependency groupIdcom.fasterxml.jackson.core/groupId artifactIdjackson-core/artifactId version2.9.0/version /dependency dependency groupIdcom.fasterxml.jackson.core/groupId artifactIdjackson-annotations/artifactId version2.9.0/version /dependencyResponseBody的使用本质是 “标记返回值需转为响应体”ResponseBody响应json数据后台代码编写有三种方式使得返回值会被序列化为 JSON第一种方式 ResponseBody 注解加到返回值前Controller RequestMapping(/redict) public class ReController { /** * ResponseBody响应json数据重要 * 异步的数据交互 */ RequestMapping(/save6.do) public ResponseBody User save6(RequestBody User user){ System.out.println(user); // 模拟调用业务层代码 user.setUsername(hello); user.setAge(100); // 把user对象转换成json字符串再响应response.getWriter().print() return user; } }第二种方式 ResponseBody 注解加到方法上Controller RequestMapping(/redict) public class ReController { // 方法上单独使用ResponseBody RequestMapping(/save7.do) ResponseBody // 仅当前方法返回数据不跳转视图 public User save7(RequestBody User user) { System.out.println(user); user.setUsername(hello); user.setAge(100); return user; // 返回的User对象会被转换为JSON响应 } }第三种方式 ResponseBody 注解加到类上即类上使用RestController等价于Controller ResponseBodyRestController RequestMapping(/test) public class TestController { // 方法上无需再添加ResponseBody RequestMapping(/save1.do) public User save6(RequestBody User user) { System.out.println(user); user.setUsername(hello); user.setAge(100); return user; // 返回的User对象会被转换为JSON响应 } }前端jsp代码编写% page contentTypetext/html;charsetUTF-8 languagejava % html head title请求参数绑定/title %--引入jq使用阿里云的jQuery CDN无integrity校验稳定可靠 --% script srchttps://cdn.jsdelivr.net/npm/jquery3.6.0/dist/jquery.min.js typetext/javascript/script script // 页面加载 $(function(){ // 单击事件 $(#btn1).click(function(){ // 发送ajax的请求 $.ajax({ type: post, url: /redict/save6.do, contentType:application/json;charsetUTF-8, data:{username:haha,age:20}, dataType: json, success:function(d){ // 编写很多代码 alert(d.username - d.age); } }); }); $(#btn2).click(function(){ // 发送ajax的请求 $.ajax({ type: post, url: /redict/save7.do, contentType:application/json;charsetUTF-8, data:{username:haha,age:20}, dataType: json, success:function(d){ // 编写很多代码 alert(d.username - d.age); } }); }); $(#btn3).click(function(){ // 发送ajax的请求 $.ajax({ type: post, url: /test/save1.do, contentType:application/json;charsetUTF-8, data:{username:haha,age:20}, dataType: json, success:function(d){ // 编写很多代码 alert(d.username - d.age); } }); }); }); /script /head body h2响应数据和结果视图/h2 h3返回值是String/h3 a href/redict/save1.do 返回String/abr/ h3返回值是void/h3 a href/redict/save2.do 返回void/abr/ h3返回值是ModelAndView/h3 a href/redict/save3.do 返回ModelAndView/abr/ h3请求转发返回值是String/h3 a href/redict/save4.do 返回值是String/abr/ h3重定向返回值是String/h3 a href/redict/save5.do 返回值是String/abr/ h3异步的数据交互方式1/h3 input typebutton valueajax交互 idbtn1br/ h3异步的数据交互方式2/h3 input typebutton valueajax交互 idbtn2br/ h3异步的数据交互方式3/h3 input typebutton valueajax交互 idbtn3br/ /body /html需要注意的是 DispatcherServlet 会拦截到所有的资源导致静态资源也会被拦截到从而不能被使用需要配置静态资源不进行拦截在 springmvc.xml 配置文件添加如下配置!-- 设置静态资源不过滤 -- mvc:resources location/css/ mapping/css/**/ !-- 样式 -- mvc:resources location/images/ mapping/images/**/ !-- 图片 -- mvc:resources location/js/ mapping/js/**/ !-- javascript --location元素表示webapp目录下的包下的所有文件mapping元素表示以/static开头的所有请求路径响应结果后端打印结果