SpringMvc傳值方式

1.首先前臺頁面

登錄頁面

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">
	username:<input type="text" name="username">
	<br/>
	password:<input type="text" name="password">
	<br/>
	<input type="submit" value="登錄">
</form>
</body>
</html>

登陸成功頁面

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Success! welcome:${username}
</body>
</html>
登陸失敗頁面

error.jsp


2.後臺代碼Contorller層接收前臺的兩種方式

方式一:

利用@RequestParam這個註解

傳值能在路徑中看見數據值(個人觀點:類型post傳參方式)


	@Controller
	public class Login {
	
		//方式一
		@RequestMapping("/login")
		public String login(@RequestParam("username") String username,
			@RequestParam("password") String password,Model model){
			if (username.equals(password)) 
			{
				model.addAttribute("username", username);
				return "success.jsp";
			} else {
				return "error.jsp";
			}
		}
	}

方式二:


傳值能在路徑中看見數據值(個人觀點:類型get傳參方式)

@Controller
	public class Login {
		//方式二
		@RequestMapping("/login")
		public String login(String username,String password,Model model){
			if (username.equals(password)) 
			{
				model.addAttribute("username", username);
				return "success.jsp";
			} else {
				return "error.jsp";
			}
		}
	
	}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章