日期:2014-05-17 浏览次数:20778 次
package com.zed.model; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; public class User { @NotNull(message="用户名不能为空") private String uname; @Size(min=4,max=8, message="密码长度应为{min}-{max}位") private String password; public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package com.zed.web; import javax.validation.Valid; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.zed.model.User; @Controller public class UserController { @RequestMapping(value="/userLogin", method=RequestMethod.POST) public String userLogin(@Valid User user, BindingResult result) { if(result.hasErrors()) { return "home"; } else { return "success"; } } }
<context:component-scan base-package="com.zed" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean>
<form action="userLogin" method="post"> <label>user name</label> <input name="uname" type="text"> <label>password</label> <input name="password" type="text"> <input type="submit" value="submit"> </form>