一.模型数据传递到jsp
mvc模式中,控制器需要把模型数据传到视图中,首先定义一个用户数据:
user.java
package spittr.data;
import java.util.Date;
public class User {
private final long id;
private final String name;
private final Date time;
public User(long id, String name, Date time){
this.id = id;
this.name = name;
this.time = time;
}
public long getId(){
return id;
}
public String getName(){
return name;
}
public Date getDate(){
return time;
}
}
控制器增加如下方法:
@RequestMapping(value="/show", method=GET)
public String showUser(
@RequestParam("userid") long userid,
Model model){
User user = new User(1,"aaaa", new Date());
model.addAttribute(user);
}
return "userinfo";
}
model中是key-value对,model.addAttribute(user)
会添加key为user,value为User对象的数据,key是根据对象的类型名得到的;
增加userinfo.jsp
<html>
<head>
<title>user:${user.id}</title>
</head>
<body>
<font face="Comic Sans MS" size=4>
<blockquote>
<center>
<h1><font color=red>user:${user.id}</font></h1>
</center>
<center>
<h1><font color=red>name:${user.name}</font></h1>
</center>
<center>
<h1><font color=red>name:${user.date}</font></h1>
</center>
</blockquote>
</font>
</body>
</html>
测试结果:
user:1
name:aaaa
name:Wed Nov 23 21:37:34 CST 2016
二.查询参数和路径参数
如果要根据http请求的用户id参数来显示信息,则需要处理请求中的参数;
userid在查询参数中的url,如:http://127.0.0.1:8080/spittr/show?userid=2
userid在路径参数中的url, 如:http://127.0.0.1:8080/spittr/show_paraminpath/2
查询参数
控制器中的showUser方法修改如下:
@RequestMapping(value="/show", method=GET)
public String showUser(
@RequestParam("userid") long userid,
Model model){
ArrayList<User> userList= new ArrayList<User>();
userList.add(new User(1,"aaaa", new Date()));
userList.add(new User(2,"bbbb", new Date()));
for(int i=0; i < userList.size(); ++i){
if(userList.get(i).getId() == userid){
model.addAttribute(userList.get(i));
model.addAttribute(new String("OK"));
break;
}
}
return "userinfo";
}
浏览器中输入:http://127.0.0.1:8080/spittr/show?userid=2
显示:
user:"2"
name:bbbb
name:Wed Nov 23 22:09:21 CST 2016
路径参数
控制器中的showUser方法修改如下:
@RequestMapping(value="/show/{userid}", method=GET)
public String showUser(
@PathVariable("userid") long userid,
Model model){
ArrayList<User> userList= new ArrayList<User>();
userList.add(new User(1,"aaaa", new Date()));
userList.add(new User(2,"bbbb", new Date()));
for(int i=0; i < userList.size(); ++i){
if(userList.get(i).getId() == userid){
model.addAttribute(userList.get(i));
model.addAttribute(new String("OK"));
break;
}
}
return "userinfo";
}
浏览器中输入:http://127.0.0.1:8080/spittr/show/2
显示:
user:"2"
name:bbbb
name:Wed Nov 23 22:12:13 CST 2016
二.表单中的参数
web应用中,用户经常需要填充表单,提交表单内容给服务器;使用表单,需要展现表单页面和处理用户通过表单提交的数据;
添加注册页面:
register.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>spittr</title>
</head>
<body>
<h1>Register</h1>
<form method="POST">
Name: <input type="text" name="name" /><br/>
Password: <input type="password" name="password" /><br/>
<input type="submit" value="Register" />
</form>
</body>
</html>
控制器中增加方法:
@RequestMapping(value="/register", method=GET)
public String register(){
return "register";
}
浏览器中输入:http://127.0.0.1:8080/spittr/register
增加注册处理逻辑:
@RequestMapping(value="/register", method=POST)
public String registerUser(String name, String password){
return "redirect:/registerok/"+name;
}
此处,方法参数name和password会自动根据http传过来的参数名来构造;
增加注册成功后的页面:
@RequestMapping(value="/registerok/{username}", method=GET)
public String registerOk(@PathVariable("username") String username,
Model mode){
mode.addAttribute(username);
return "registerok";
}
注册成功: