Spring Boot使用重定向从控制器中的一个方法跳转到另一个方法

雨点打透心脏的1/2处 2024-04-18 19:39 128阅读 0赞

我在添加之后需要跳转到显示页面,需要从控制器中的添加完成后跳转到显示的方法中,使用从定向指定就可实现!! 这个重定向跳转可以跨控制器哦

不用关注别的代码 重定向跳转只需要 return "redirect:/target";就可以从控制器中的一个方法跳转到另一个方法

  1. /** * * 显示目标库数据 * @param model * @return * @throws Exception */
  2. @RequestMapping(value="/target")
  3. public String targetUpdate(Model model) throws IOException {
  4. String postUrl = "http://192.168.4.10:80/api/json";
  5. Map<String, Object> map = new HashMap<String, Object>();
  6. map.put("msg_id", "1028");
  7. JSONObject jsonMap = JSONObject.fromObject(map);
  8. String jsn = PostDemo.sendPost(postUrl, jsonMap, "utf-8");
  9. ObjectMapper mapper = new ObjectMapper();
  10. JsonNode node = mapper.readTree(jsn);
  11. //String resultJson = node.get("data").toString();
  12. String lib_type;
  13. int i;
  14. List list = new ArrayList();
  15. for(i=0;i<node.get("data").size();i++){
  16. Map map1 = new HashMap();
  17. if(1==Integer.parseInt(node.get("data").get(i).get("lib_type").toString())){
  18. lib_type = "黑名单";
  19. }else{
  20. lib_type = "白名单"; }
  21. map1.put("create_time",node.get("data").get(i).get("create_time").toString().replace("\"",""));
  22. map1.put("lib_id",node.get("data").get(i).get("lib_id").toString().replace("\"",""));
  23. map1.put("lib_name",node.get("data").get(i).get("lib_name").toString().replace("\"",""));
  24. map1.put("lib_type",lib_type);
  25. map1.put("picture_no",node.get("data").get(i).get("picture_no").toString().replace("\"",""));
  26. map1.put("update_time",node.get("data").get(i).get("update_time").toString().replace("\"",""));
  27. list.add(map1);
  28. }
  29. model.addAttribute("targetLibrary", list);
  30. //System.out.println("打印数据 : "+PostDemo.sendPost(postUrl, jsonMap, "utf-8"));
  31. return "targetLibrary";
  32. }
  33. /** * 添加目标库 * @param lib_name * @param lib_type * @return * @throws IOException */
  34. @RequestMapping("/targetAdd")
  35. public String targetAdd(@RequestParam(value = "lib_name",required = false) String lib_name,
  36. @RequestParam(value = "lib_type",required = false) String lib_type) throws IOException{
  37. if(null!=lib_name){
  38. String msg_id = "1025";
  39. String postUrl = "http://192.168.4.10:80/api/json";
  40. Map<String, Object> map = new HashMap<String, Object>();
  41. map.put("msg_id", msg_id);
  42. map.put("lib_name", lib_name);
  43. map.put("lib_type", Integer.parseInt(lib_type));
  44. JSONObject jsonMap = JSONObject.fromObject(map);
  45. String jsn = PostDemo.sendPost(postUrl, jsonMap, "utf-8");
  46. ObjectMapper mapper = new ObjectMapper();
  47. JsonNode node = mapper.readTree(jsn);
  48. if("0"==node.get("code").toString()){
  49. return "redirect:/target";
  50. }
  51. }
  52. return "targetAdd";
  53. }

发表评论

表情:
评论列表 (有 0 条评论,128人围观)

还没有评论,来说两句吧...

相关阅读

    相关 如何定向一个网页?

    问题描述: 这个问题的答案是社区的努力。编辑现有答案以改进这篇文章。它目前不接受新的答案或交互。 如何使用 jQuery 或纯 JavaScript 将用户从一个页面重