之前已经实现了spring mvc的入门例子及如何处理带参数的请求Controller编写。本文主要记录:

1)重定向请求

2)处理路径中含有变量的请求

3)使用JSR-303进行校验

 

① 首先,编写一个方法实现一个表单的展现:

//展示表单
    
//params 可限制该方法只对包含特定参数的请求进行处理
    @RequestMapping(method=RequestMethod.GET,params="new")
    public String createForm(Model model){
        model.addAttribute("user", new User());
        return "/user/edit";
    }

 注意到,params="new",这意味着该方法处理的请求Url:xxx/home?new (home是定义Controller的请求路径)

这是跳转的页面定义(使用了velocity模板):

<form method="post">
    <div>
        <label for="userName">userName:</label>
        <input name="userName" value="$!user.userName">
        <span>$!br.getFieldError("userName").getDefaultMessage()</span>
    </div>
    
    <div>
        <label for="email">Email:</label>
        <input name="email" value="$!user.email">
    </div>
    
    <div>
        <label for="age">age:</label>
        <input name="age" value="$!user.age">
    </div>
    <button type="submit">Submit</button>
</form>
View Code

相关文章:

  • 2021-12-02
  • 2022-12-23
  • 2022-01-20
  • 2022-12-23
  • 2021-09-28
  • 2022-12-23
  • 2022-12-23
  • 2021-07-18
猜你喜欢
  • 2021-11-28
  • 2022-12-23
  • 2021-12-27
  • 2021-07-02
相关资源
相似解决方案