Spring返回方式总结
1.返回字符串,这时候会跳转到对应的页面。完成系统跳转功能。
#这个跳转到hello.jsp页面
@RequestMapping(value="/hello", method= RequestMethod.GET)
public String printHello(ModelMap model){
model.addAttribute("message","Hello Spring MVC Framework!");
return "hello";
}
2.将内容作为response的body返回,这时候页面会显示hello
#这个跳转到hello.jsp页面
@ResponseBody
@RequestMapping(value="/hello", method= RequestMethod.GET)
public String printHello(ModelMap model){
model.addAttribute("message","Hello Spring MVC Framework!");
return "hello";
}
3.返回ModelAndView,这里能返回数据,并且调用setViewName来设定返回页面的地址。
@RequestMapping(value="/getDenpend")
public ModelAndView getDependTasks(@RequestParam("task_code") String task_code){
ModelAndView mv = new ModelAndView();
System.out.println(task_code);
List<TaskSeq> list = taskService.getDependTasks(task_code);
int index = -1;
for (TaskSeq taskSeq : list) {
if(taskSeq.getTask_code().equals(task_code)){
index = list.indexOf(taskSeq);
break;
}
}
if(index != -1){
list.remove(index);
}
list.add(new TaskSeq(task_code,"select_key"));
mv.addObject("task_seq", list);
mv.setViewName("TaskSeq");
return mv;
};