講解一下RequestMapping,Request請求參數和ModelAndView返回模型和視圖

@Controller
@RequestMapping("/student")
public class StudentController {

    private static List<Student> studentList=new ArrayList<Student>();

    static{
        studentList.add(new Student(1,"ÕÅÈý",11));
        studentList.add(new Student(2,"ÀîËÄ",12));
        studentList.add(new Student(3,"ÍõÎå",13));
    }

    @RequestMapping("/list")
    public ModelAndView list(){
        ModelAndView mav=new ModelAndView();
        mav.addObject("studentList", studentList);
        mav.setViewName("student/list");
        return mav;
    }

    @RequestMapping("/preSave")
    public ModelAndView preSave(@RequestParam(value="id",required=false) String id){
        ModelAndView mav=new ModelAndView();
        if(id!=null){
            mav.addObject("student", studentList.get(Integer.parseInt(id)-1));
            mav.setViewName("student/update");
        }else{
            mav.setViewName("student/add");           
        }
        return mav;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章