• 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代码
  • 相关阅读:
    盘古越狱工具在用户空间的行为
    hdu 5538 House Building(长春现场赛——水题)
    html 锚点定位
    OOP版电子词典
    有趣的JavaScript原生数组函数
    &lt;LeetCode OJ&gt; 121. /122. Best Time to Buy and Sell Stock(I / II)
    hadoop 出现FATAL conf.Configuration: error parsing conf file,异常
    IT痴汉的工作现状10-Sprint Planning
    2015 Astar Contest
    无法使用BIPublisher开发报表
  • 原文地址:https://www.cnblogs.com/xx0405/p/5503958.html
Copyright © 2020-2023  润新知