Building Spring web applications (3)

Process the form

First, write a form

<%@ 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>

Note that the form tag here does not set the action attribute. In this case, when the form is submitted, it will be submitted to the same url path as when it was displayed, that is, it will be submitted to /spitter/register. So we add a register method to the Controller that handles POST requests

2. Then write the corresponding controller

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

<%@ 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>

validation form

Validate with JSR-303 Validation

(1) First transform the entity class

....
@NotNull @Size(min
=5, max=16) // Not empty, 5 to 16 characters private String userName; @NotNull @Size(min =5, max=16) // Not empty, 5 to 16 characters private String firstName; @NotNull @Size(min =5, max=16) // Not empty, 5 to 16 characters private String lastName; @NotNull @Size(min =5, max=25) // Not empty, 5 to 25 characters private String password; ....

(2) Then transform the method in the controller

    .....
@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

When the parameter verification fails, it will return to the registerForm page

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325074551&siteId=291194637