Skip to main content
Jkyo Chen Blog

第二部分 Web 中的 Spring

构建 Spring Web 应用程序 #

Spring MVC 起步 #

跟踪 Spring MVC 的请求 #

搭建 Spring MVC #

配置 DispatcherServlet #

启用 Spring MVC #

编写基本的控制器 #

接受请求的输入 #

处理查询参数 #

    @RequestMapping(method = RequestMethod.GET)
    public List spittles(
            @RequestParam(value = "max", defaultValue = MAX_LONG_AS_STRING) long max,
            @RequestParam(value = "count", defaultValue = "20") int count
    ) {
        return spittleRepository.findSpittles(Long.MAX_VALUE, 20);
    }

通过路径参数接收输入 #

    @RequestMapping(value = "/{spittleId}", method = RequestMethod.GET)
    public String spittle(
            @PathVariable("spittleId") long spittleId, Model model
    ) {
        model.addAttribute(spittleRepository, findOne(spittleId));
        return "spittle";
    }

    // 因为方法的参数名与占位符的名称相同,可以去掉@PathVariable中的value属性
    @RequestMapping(value = "/{spittleId}", method = RequestMethod.GET)
    public String spittle(
            @PathVariable long spittleId, Model model
    ) {
        model.addAttribute(spittleRepository, findOne(spittleId));
        return "spittle";
    }

处理表单 #

渲染Web视图 #