This presentation SPRINGMVC the implementation of adding data using restful styles. Add a link to the add.jsp page in the previous lecture, and the corresponding code looks like this:
<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="Utf-8"%><%@ 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><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>User List</title></Head><Body><ahref= "Add">Add to</a> <C:foreachItems= "${users}"var= "Um" >${um.value.username}----${um.value.nickname}----${um.value.password}----${um.value. email}<BR/> </C:foreach></Body></HTML>View Code
View Code
The URL to jump using get mode is Http://localhost:8080/springMVC001/user/add. The corresponding controller class needs to jump to the add.jsp page, where a user object needs to be initialized. The corresponding Add method is as follows:
// when the link to the Add page is a GET request, the code is accessed @ Requestmapping (value = "/add", Method = Requestmethod.get) public String Add (@ModelAttribute ("User" // open Modeldriven // Two ways to initialize a user instance to the add.jsp page: // Mode one: Use model in the incoming parameter, then create a new user object in the method, // // Model.addattribute (new User ()); return "User/add" ;}
View Code
To jump to the add.jsp page, for a form structure, you need to use the form labels provided by spring, as shown in the corresponding code:
<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><%@ taglib Prefix="SF"URI="Http://www.springframework.org/tags/form" %><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Insert Title here</title></Head><Body><!--no action is written at this time, direct submission will be submitted to/add -<!--The controller class needs to initialize a user object to be used in the foreground form -<!--add a value from the form to the Modelattribute property value in user -<Sf:formMethod= "POST"Modelattribute= "User"enctype= "Multipart/form-data">Username:<Sf:inputPath= "username"/><sf:errorsPath= "username"/><BR/>Password:<Sf:passwordPath= "Password"/><sf:errorsPath= "Password"/><BR/>Nickname:<Sf:inputPath= "nickname"/><BR/>Email:<Sf:inputPath= "Email"/><sf:errorsPath= "Email"/><BR/> <inputtype= "Submit"value= "Add User"/></Sf:form></Body></HTML>
View Code
The corresponding request is a POST request, and there is no form action, jump to the Add method that Requestmethod is post, Before the add.jsp page has a server checksum for the input, it needs to be validated against the user object using Bean-validator.jar, as shown in the following code:
Packagezttc.itat.entity;Importjavax.validation.constraints.Size;ImportOrg.hibernate.validator.constraints.Email;ImportOrg.hibernate.validator.constraints.NotEmpty; Public classUsers {PrivateString username; PrivateString password; PrivateString Nickname; PrivateString Email; PublicThe Users () {} @NotEmpty (message= "User name cannot be empty") PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } @Size (min=1,max=10,message= "The length of the password should be between 1 and 10") PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } PublicString Getnickname () {returnnickname; } Public voidSetnickname (String nickname) { This. Nickname =nickname; } @Email (Message= "The mailbox is not formatted correctly") PublicString Getemail () {returnemail; } Public voidsetemail (String email) { This. email =email; } PublicUsers (string Username, string password, string nickname, string email) {Super(); This. Username =username; This. Password =password; This. Nickname =nickname; This. email =email; }}
View Code
corresponding to the controller class need to get the POST request user object for foreground validation, using Bindingresult to obtain the results of validation, if the result is wrong, jump to the add.jsp page, the corresponding code is as follows:
// @ Requestmapping (value = "/add", Method = Requestmethod.post) public String Add (@Validated Users user, Bindingresult BR,) throws IOException {// Be sure to follow validate after you write the validation result class if (br.haserrors ()) { // If there is an error, jump directly to the Add view return "User/add" ; } users.put (User.getusername (), user); return "redirect:/user/users"
View Code
Springmvc Getting Started-04