1. 程式人生 > >spring-mvc 引數傳遞

spring-mvc 引數傳遞

//方法一:使用最原始的servlet request獲取資料

	//方法一:使用最原始的servlet request獲取資料
	@RequestMapping(value="/testParam1",method=RequestMethod.POST)
	public String testParam1(HttpServletRequest request) throws Exception{
		
		String username = new String(request.getParameter("username"));
		String password = new String(request.getParameter("password"));
		
		System.out.println("username:"+ username + "password:" + password);
		
		return "sucess";
	}
	<form action="${pageContext.request.contextPath}/testParam1.action" method="post">
		使用者:<input tpye="text" name= "username">
		密碼:<input tpye="text" name= "password">
		<input type="submit" value="Submit">
	</form>

//方法二,包裝javabean,獲取資料

	@RequestMapping(value="/testParam2",method=RequestMethod.POST)
	public String testParam2(User user) throws Exception{
		
		String username = user.getUsername();
		String password = user.getPasswd();
		String age = user.getAge();
		
		System.out.println("username:"+ username + "password:" + password + "age:" + age);
		
		return "sucess";
	}
	<form action="${pageContext.request.contextPath}/testParam2.action" method="post">
		使用者:<input tpye="text" name= "username">
		密碼:<input tpye="password" name= "password">
		年齡:<input tpye="text" name= "age">
		<input type="submit" value="Submit">
	</form>
	

    //方法三,使用路徑傳遞引數

	@RequestMapping(value="/testParam3/{username}/{password}",method=RequestMethod.POST)
	public String testParam3(@PathVariable(value="username")String username, @PathVariable(value="password")String password) throws Exception{
		
		System.out.println("username:"+ username + "password:" + password);
		
		return "sucess";
	}
	
	<h3>路徑傳引數</h3>
	<form action="${pageContext.request.contextPath}/testParam3/username/password.action" method="post">
		<input type="submit" value="Submit">
	</form>