Building a Spring Web application (iii)

Source: Internet
Author: User

Working with Forms

First of all, 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><Metahttp-equiv= "Content-type"content= "text/html; charset=iso-8859-1"><title>Insert Title here</title></Head><Body><H1>Register</H1><formMethod= "POST">First Name:<inputtype= "text"name= "FirstName"/><BR/>Last Name:<inputtype= "text"name= "LastName"/><BR/>UserName:<inputtype= "text"name= "UserName"/>Password:<inputtype= "text"name= "Password"/><inputtype= "Submit"value= "Register"/></form></Body></HTML>

Note that the form tag here does not set the Action property, in which case when the form commits he submits to the same URL path as the presentation, that is, it is submitted to/spitter/register. So we add a register method to the controller that handles the POST request

Second, then write the corresponding controller

Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.PathVariable;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;Importdata. Repository;Importentity. Spittle, @Controller @requestmapping ("/spitter") Public classSpittercontroller {PrivateRepository Repository; @Autowired PublicSpittercontroller (Repository Repository) { This. Repository =repository; } @RequestMapping (Value= "/register", method=requestmethod.get) PublicString Showregistrationform () {return"Registerform"; } @RequestMapping (Value= "/register", method=requestmethod.post) PublicString processregistration (spittle spittle) {repository.save (spittle); return"redirect:/spitter/" +Spittle.getusername (); } @RequestMapping (Value= "/{username}", method=requestmethod.get) Publicstring 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><Metahttp-equiv= "Content-type"content= "text/html; charset=iso-8859-1"><title>Insert Title here</title></Head><Body><H1>Your profile</H1><C:outvalue= "${spitter.username}"/><BR/><C:outvalue= "${spitter.username}"/><BR/><C:outvalue= "${spitter.username}"/><BR/></Body></HTML>

Verifying the form

Use JSR-303 Validation to verify

(1) First to transform the entity class

....
@NotNull @Size (min=5, max=16) // non-empty, 5 to 16 characters Private String UserName; @NotNull @Size (min=5, max=16) // non-empty, 5 to 16 characters private String FirstName; @NotNull @Size (min=5, max=16) // non-empty, 5 to 16 characters private String LastName; @NotNull @Size (min=5, max=25) // non-empty, 5 to 25 characters private String password; ....

(2) Then change 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 (); }
...

The required jar packages are 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

Returns to the Registerform page when the parameter check is not passed

Building a Spring Web application (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.