Spring 3.1 MVC入门(二):简单用户注册示例
编写一个简单的用户注册的示例程序,暂时不写入数据库。
引入相关jar包
由于在这里要使用validation验证,所以需要引入javax.validation的相关jar包,在pom.xml里面添加如下内容:
在properties里面添加
<org.slf4j.version>1.6.4</org.slf4j.version>
在dependencies里面添加如下内容。
<!-- Logging --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${org.slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${org.slf4j.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${org.slf4j.version}</version> <scope>runtime</scope> </dependency> <!-- JSR 303 with Hibernate Validator --> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.0.0.GA</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>4.1.0.Final</version> </dependency>
编写java代码
创建一个简单的user模型类,代码如下,我们使用validation的注解,标注userName不能为空,passowrd不能为空,且长度必须在6到16位之间。
public class User { private Long id; @NotEmpty private String userName; @NotEmpty @Size(min=6,max=16) private String password; private boolean sex; private int age; @Email private String email; //中间的get set方法省略 Long assignId() { this.id = idSequence.incrementAndGet(); return id; } private static final AtomicLong idSequence = new AtomicLong();
编写UserController类,代码如下:
@Controller @RequestMapping(value="/user") public class UserController { private Map<Long, User> users = new ConcurrentHashMap<Long, User>(); @RequestMapping(method = RequestMethod.GET) public String toRegister(Model model){ model.addAttribute(new User()); return "user_register"; } @RequestMapping(value="register",method = RequestMethod.POST) public String register(Model model,@Valid User user,BindingResult result){ if (result.hasErrors()) { return "user_register"; } this.users.put(user.assignId(), user); return "redirect:/user/"+user.getId(); } @RequestMapping(value="{id}", method=RequestMethod.GET) public String getView(@PathVariable Long id, Model model) { User user = this.users.get(id); if (user == null) { //@TODO } model.addAttribute(user); return "user_view"; } }
编写HTML页面
user_register.jsp
页面中的form部分如下,页面中的CSS样式引用了bootstrap,为方便使用spring的数据绑定,这里的form使用的是spring的form标签,modelAtrribute里面的值对应于要绑定的模型类的名称。
<form:form modelAttribute="user" cssClass="form-horizontal" action="${pageContext.request.contextPath}/user/register" method="post"> <div class="control-group"> <form:label for="userName" path="userName" cssClass="control-label">用户名:</form:label> <div class="controls"> <form:input path="userName" /><form:errors path="userName" cssClass="help-inline"/> </div> </div> <div class="control-group"> <form:label for="password" path="password" cssClass="control-label" >登录密码:</form:label> <div class="controls"> <form:password path="password" /><form:errors path="password" cssClass="help-inline" /> </div> </div> <div class="control-group"> <form:label for="age" path="age" cssClass="control-label">年龄:</form:label> <div class="controls"> <form:input path="age" /><form:errors path="age" cssClass="help-inline" /> </div> </div> <div class="control-group"> <form:label for="sex" path="sex" cssClass="control-label" cssErrorClass="error">性别:</form:label> <div class="controls"> <form:radiobutton path="sex" value="1" />男 <form:radiobutton path="sex" value="0" />女 </div> </div> <div class="form-actions"> <input type="submit" value="申请注册" class="btn btn-primary" /> <input type="reset" value="重置" class="btn" /> </div> </form:form>
user_view.jsp
此页面的内容只是简单的使用EL表达试将内容输出。
您的注册信息如下:<hr> 用户名:${user.userName}<br> 用户密码:${user.password}
在浏览器里面输入http://localhost:8080/SpringMVC/user,即可查看到如下内容:
留言是种美德,写点什么…