1. 程式人生 > >SSM簡單引數傳遞與獲取方法

SSM簡單引數傳遞與獲取方法

一:引數獲取的幾種方法 1》

/*** * (簡單新增方式一)接受Xueyuan這個實體類的引數物件即xueyuan * @param xueyuan * @param request * @param response * @throws Exception */

@RequestMapping("/add_xueyuan")
public void save(Xueyuan xueyuan,HttpServletRequest request,HttpServletResponse response) throws Exception{
	
	PrintWriter out = null;
	try {
		out = response.getWriter();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	xueyuanService.addXueyuan(xueyuan);//呼叫service一個方法addXueyuan(xueyuan)方法
	if(xueyuan!=null){
		out.print("1");
	}else{
		out.print("2");
	}
	out.flush();
	out.close();
}

2》 /*** * (簡單新增方式二)通過HttpServletRequest接收,post方式和get方式都可以 * @param request * @param response * @throws Exception */

@RequestMapping("/save")
public void save(HttpServletRequest request,HttpServletResponse response) throws Exception{
	//new一個實體類物件
	Xueyuan xueyuan = new Xueyuan();
	//輸入輸出流初始化
	PrintWriter out = null;
	try {
		out = response.getWriter();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	//例項化物件,接受到前臺輸入引數(此處只有一個xueyuan_name)
	String xueyuan_name = request.getParameter("xueyuan_name");
	//傳遞引數
	xueyuan.setXueyuan_name(xueyuan_name);
	//呼叫service層方法(該例子為簡單的新增實現)
	xueyuanDao.addXueyuan(xueyuan);
	if(xueyuan!=null){
		out.print("1");
	}else{
		out.print("2");
	}
	out.flush();
	out.close();
}

3》 /*** * (簡單新增方式三)直接把表單的引數寫在Controller相應的方法的形參中 * @param request * @param response * @throws Exception */

@RequestMapping("/add_xueyuan")
public void add_xueyuan(String xueyuan_name,HttpServletRequest request,HttpServletResponse response) throws Exception{
	//輸入輸出流初始化
	PrintWriter out = null;
	try {
		out = response.getWriter();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	//呼叫service層方法(該例子為簡單的新增實現)
	xueyuanDao.addXueyuan(xueyuan_name);
	if(xueyuan_name!=null){
		out.print("1");
	}else{
		out.print("2");
	}
	out.flush();
	out.close();
	
}