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代码

 

posted @ 2016-05-18 09:53  小小小飞鱼  阅读(3044)  评论(0编辑  收藏  举报