1、前台往后台传值:
login.jsp输入框设置,主要是name属性
<h1>登录页面</h1>
<form method="post" action="">
用户名:<input type="text" name="userName"><br>
密 码:<input type="text" name="password"><br>
<input type="submit">
</form>
loginController.java 中获取(两种方式):
①
@RequestMapping(value="/add",method=RequestMethod.GET)
public String sayHello(@RequestParam("userName") String username,@RequestParam("password") String password,Model model) {
List<User> userList = new ArrayList<User>();
User user = new User();
user.setUserName(username);
user.setPassword(password);
user.setAge(password);
userList.add(user);
model.addAttribute("users",userList);
return "user";
}
②
@RequestMapping(method = RequestMethod.POST)
public String sayHello1(String userName,String password,Model model) {
List<User> userList = new ArrayList<User>();
User user = new User();
user.setUserName(userName);
user.setPassword(password);
user.setAge(password);
userList.add(user);
model.addAttribute("users",userList);
return "user";
}
2、后台往前台传值:
放到model里面即可,前台根据key可获取到value,代码如下:
<h2 style="font:"Trebuchet MS", arial, sans-serif ">用户列表</h2>
<c:forEach items="${users}" var="user">
用户名:${user.userName}
性别:${user.sex}
年龄:${user.age}
电话:${user.tel} <br/>
</c:forEach>