构建Spring web 应用程序 (三)

处理表单

一、首先写一个表单

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Register</h1>

<form method="post">
First Name: <input type="text" name="firstName"/><br/>
last Name: <input type="text" name="lastName"/><br/>
UserName: <input type="text" name="userName"/>
Password: <input type="text" name="password"/>
<input type="submit" value="register"/>
</form>
</body>
</html>

注意这里的 form 标签并没有设置 action 属性,这种情况下当表单提交时他会提交到与展现时相同的url路径上,即它会提交到 /spitter/register 上。 所以我们在该Controller中添加一个处理POST请求的 register 方法

二、然后编写对应的控制器

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import data.Repository;
import entity.Spittle;

@Controller
@RequestMapping("/spitter")
public class SpitterController {
    private Repository repository;
    
    @Autowired
    public SpitterController(Repository repository) {
        this.repository = repository;
    }

    @RequestMapping(value="/register", method=RequestMethod.GET)
    public String showRegistrationForm() {
        return "registerForm";
    }
    
    @RequestMapping(value="/register", method=RequestMethod.POST)
    public String processRegistration(Spittle spittle) {
        repository.save(spittle);
        return "redirect:/spitter/" + spittle.getUserName();
    }
    
    @RequestMapping(value="/{userName}", method=RequestMethod.GET)
    public String redirectPage(@PathVariable String userName, Model model) {
        System.out.println("6666666666666");
        Spittle spittle = repository.findByUserName(userName);
        model.addAttribute(spittle);
        return "profile";
    }
}

profile.jsp 页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Your Profile</h1>
<c:out value="${spitter.username}"/><br/>
<c:out value="${spitter.username}"/><br/>
<c:out value="${spitter.username}"/><br/>
</body>
</html>

校验表单

使用JSR-303 Validation 来校验

(1)首先改造实体类

....
@NotNull @Size(min
=5, max=16) //非空,5到16个字符 private String userName; @NotNull @Size(min=5, max=16) //非空,5到16个字符 private String firstName; @NotNull @Size(min=5, max=16) //非空,5到16个字符 private String lastName; @NotNull @Size(min=5, max=25) //非空,5到25个字符 private String password; ....

(2)然后改造控制器中的方法

    .....
@RequestMapping(value="/register", method=RequestMethod.POST) public String processRegistration(@Valid Spittle spittle, Errors errors) { System.out.println(errors.hasErrors()+""); if (errors.hasErrors()) { return "registerForm"; } repository.save(spittle); return "redirect:/spitter/" + spittle.getUserName(); }
......

需要的jar包有 classmate-1.0.0、hibernate-jpa-2.1-api-1.0.2.Final、hibernate-validator-5.1.0.Final、jboss-logging-3.3.2.Final、joda-time-2.1、jsoup-1.7.1、paranamer-2.5.5、validation-api-2.0.1.Final

当参数校验不通过的时候会返回到registerForm页面

猜你喜欢

转载自www.cnblogs.com/codefeng/p/8975037.html