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";
			}
		}
	
	}




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