controller level
method-level
package spittr.web;
import static org.springframework.web.bind.annotation.RequestMethod.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HomeController {
@RequestMapping(value="/", method=GET)
public String home() {
return "home";
}
}
class-level
@Controller
@RequestMapping("/") //@RequestMapping({"/", "/homepage"})
public class HomeController {
@RequestMapping(method=GET)
public String home() {
return "home";
}
}
passing model data to the view
- 数据流: controller --> view resolver --> view
- 数据的类型: Model,本质上是一个map
- controller函数中始终返回的是view 名称,Model 数据的传递由框架来实现
- 在前端的jsp中,可以通过key来访问数据
传递模型数据的几种形式
返回view name, 数据保存在model中
- 返回的view名称"spittles", 传递的数据保存在Model(map)中
- Model 不明确指定key,默认自动推导可以为"spittleList"(List<Spittle> 推导而来)
@Controller
@RequestMapping("/spittles")
public class SpittleController {
private SpittleRepository spittleRepository;
@Autowired
public SpittleController(
SpittleRepository spittleRepository) {
this.spittleRepository = spittleRepository;
}
@RequestMapping(method=RequestMethod.GET)
public String spittles(Model model) {
model.addAttribute(spittleRepository.findSpittles(Long.MAX_VALUE, 20));//model.addAttribute("spittleList", spittleRepository.findSpittles(Long.MAX_VALUE, 20));
return "spittles";
}
}
返回view name, 数据使用java.util.Map保存
@RequestMapping(method=RequestMethod.GET)
public String spittles(Map model) {
model.put("spittleList",
spittleRepository.findSpittles(Long.MAX_VALUE, 20));
return "spittles";
}
返回数据,view name让框架自动推导(最好别这么做)
@RequestMapping(method=RequestMethod.GET)
public List<Spittle> spittles() {
return spittleRepository.findSpittles(Long.MAX_VALUE, 20));
}
- 返回的数据会保存到Model中,Model key也是自动推导
- view name 根据request path推到而来, 比例:http://xxx/spittles, view name就是spittles
request input
获取参数
@RequestMapping(method=RequestMethod.GET)
public List<Spittle> spittles(
@RequestParam(value="max", defaultValue=MAX_LONG_AS_STRING) long max,
@RequestParam(value="count", defaultValue="20") int count) {
return spittleRepository.findSpittles(max, count);
}
resource-orientation 资源化的url
@RequestMapping(value="/{spittleId}", method=RequestMethod.GET)
public String spittle(
@PathVariable("spittleId") long spittleId,
Model model) {
model.addAttribute(spittleRepository.findOne(spittleId));
return "spittle";
}
- @PathVariable 可以省略参数,默认和函数的参数相同, 即spittleId (long spittleId)
processing forms
form controller
@RequestMapping(value="/register", method=POST)
public String processRegistration(Spitter spitter) {
spitterRepository.save(spitter);
return "redirect:/spitter/" +
spitter.getUsername();
}
validating forms
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class SpittleForm {
@NotNull
@Size(min=1, max=140)
private String message;
@Min(-180)
@Max(180)
private Double longitude;
@Min(-90)
@Max(90)
private Double latitude;
.....
}
- controller 对应的处理函数中必须增加一个org.springframework.validation.Errors 参数
@RequestMapping(value="/register", method=POST)
public String processRegistration(
@Valid Spitter spitter,
Errors errors) {
if (errors.hasErrors()) {
return "registerForm";
}
spitterRepository.save(spitter);
return "redirect:/spitter/" + spitter.getUsername();
}