Spring Boot使用重定向从控制器中的一个方法跳转到另一个方法
我在添加之后需要跳转到显示页面,需要从控制器中的添加完成后跳转到显示的方法中,使用从定向指定就可实现!! 这个重定向跳转可以跨控制器哦
不用关注别的代码 重定向跳转只需要 return "redirect:/target";
就可以从控制器中的一个方法跳转到另一个方法
/** * * 显示目标库数据 * @param model * @return * @throws Exception */
@RequestMapping(value="/target")
public String targetUpdate(Model model) throws IOException {
String postUrl = "http://192.168.4.10:80/api/json";
Map<String, Object> map = new HashMap<String, Object>();
map.put("msg_id", "1028");
JSONObject jsonMap = JSONObject.fromObject(map);
String jsn = PostDemo.sendPost(postUrl, jsonMap, "utf-8");
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(jsn);
//String resultJson = node.get("data").toString();
String lib_type;
int i;
List list = new ArrayList();
for(i=0;i<node.get("data").size();i++){
Map map1 = new HashMap();
if(1==Integer.parseInt(node.get("data").get(i).get("lib_type").toString())){
lib_type = "黑名单";
}else{
lib_type = "白名单"; }
map1.put("create_time",node.get("data").get(i).get("create_time").toString().replace("\"",""));
map1.put("lib_id",node.get("data").get(i).get("lib_id").toString().replace("\"",""));
map1.put("lib_name",node.get("data").get(i).get("lib_name").toString().replace("\"",""));
map1.put("lib_type",lib_type);
map1.put("picture_no",node.get("data").get(i).get("picture_no").toString().replace("\"",""));
map1.put("update_time",node.get("data").get(i).get("update_time").toString().replace("\"",""));
list.add(map1);
}
model.addAttribute("targetLibrary", list);
//System.out.println("打印数据 : "+PostDemo.sendPost(postUrl, jsonMap, "utf-8"));
return "targetLibrary";
}
/** * 添加目标库 * @param lib_name * @param lib_type * @return * @throws IOException */
@RequestMapping("/targetAdd")
public String targetAdd(@RequestParam(value = "lib_name",required = false) String lib_name,
@RequestParam(value = "lib_type",required = false) String lib_type) throws IOException{
if(null!=lib_name){
String msg_id = "1025";
String postUrl = "http://192.168.4.10:80/api/json";
Map<String, Object> map = new HashMap<String, Object>();
map.put("msg_id", msg_id);
map.put("lib_name", lib_name);
map.put("lib_type", Integer.parseInt(lib_type));
JSONObject jsonMap = JSONObject.fromObject(map);
String jsn = PostDemo.sendPost(postUrl, jsonMap, "utf-8");
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(jsn);
if("0"==node.get("code").toString()){
return "redirect:/target";
}
}
return "targetAdd";
}
还没有评论,来说两句吧...