• Servlet 前端后台交互


    一. URL地址传值

      1.1、 地址传值   

      http://localhost:8080/xj/123/name.json 
      servlet 对应接受方法  
    1     @RequestMapping(value="/{name}/list.json",method = RequestMethod.GET)
    2     public @ResponseBody Object list(HttpServletRequest request, @PathVariable("name") String name) {
    3         
    4         List<School> schools = schoolService.findByNameLike(name);
    5         return schools;
    6     }
    
    
    1.2、 地址参数传值
      http://localhost:8080/xj/sch/name.json?name=123
      servlet 对应接受方法
    1     @RequestMapping(value="/name/list.json",method = RequestMethod.GET)
    2     public @ResponseBody Object list1(HttpServletRequest request, 
    3             @RequestParam(name="name", required=true) String name) {
    4         
    5         List<School> schools = schoolService.findByNameLike(name);
    6         return schools;
    7     }

     

    二. servlet 返回类型

      2.1、 Servlet 返回界面  

    1     @RequestMapping(value="/check",method = RequestMethod.GET)
    2     public String check(HttpServletRequest request, Model model) {
    3         model.addAttribute("name", "张三");
    4         return "credit/check";
    5     }
    
    
      返回类型为:String 
      返回界面需要的值放在 model 中
     1    @RequestMapping(value="/v/a",method = RequestMethod.POST)
     2     public String checkAll(HttpServletRequest request, RedirectAttributes attr,
     3                @Valid @ModelAttribute("ei") EducationInfo educationInfo,
     4                @Valid @ModelAttribute("di") DriverInfo driverInfo, BindingResult result,
     5                @AuthenticationPrincipal User user) {
     6         
     7         if(educationInfo != null && educationInfo.getPersonInfo() != null){
     8             try {
     9                 Map<String, Object> map = checkService.checkAll(user, educationInfo, driverInfo);
    10                 initData(attr, map);
    11             } catch (CreditException e) {
    12                 logger.info("远程服务请求失败:"+e.getMessage());
    13                 attr.addFlashAttribute("msg", e.getMessage());
    14             }
    15         }
    16         return "redirect:/check";
    17     }
      通过地址重定向进行绑定,保证了返回的界面地址栏上没有多余的参数;

      2.2. servlet 返回对象

      返回类型:@ResponseBody Object
      同例1.1或1.2代码
  • 相关阅读:
    Javascript跨域后台设置拦截
    Hello ReactJS
    Redis 常用监控信息命令总结
    MySQL架构与业务总结图
    MySQL垂直拆分和水平拆分的优缺点和共同点总结
    MySQL实用工具汇总
    MySQL查看数据库表容量大小
    MySQL到底能支持多大的数据量?
    微信小程序wxss的background本地图片问题
    微信小程序中显示与隐藏(hidden)
  • 原文地址:https://www.cnblogs.com/xx0405/p/5503958.html
Copyright © 2020-2023  润新知