[4] Spring MVC Learning Notes

Source: Internet
Author: User

User management based on spring MVC

In-depth understanding of spring MVC based on practical small projects

1. First, the user class

public class User {private int id;private string Username;private string Password;private string Nickname;private string E Mail;        Omitting constructors and get, set methods        }

2. Then the preparation of Usercontroller

Some notes:

(1). Two ways to jump, server-side jump (with parameters) return "Redirect:/user/users"; and return the relevant error message and the client jumps (understanding redirect) return "User/add";

(2). Returns information to the view layer through the model object;

(3). Get and post requests are handled separately;

(4). Related validate verification, need to import Bean-validate.jar package, the following javabean in detail.

Package Com.qzp.web;import Java.util.hashmap;import Java.util.map;import Org.springframework.stereotype.Controller ; Import Org.springframework.ui.model;import Org.springframework.validation.bindingresult;import Org.springframework.validation.annotation.validated;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import Com.qzp.model.User; @Controller @requestmapping ("/    User ") public class Usercontroller {private Map<string, user> users = new hashmap<string, user> ();        Public Usercontroller () {users.put ("Sdy", New User (1, "Sdy", "123", "Song Dongno", "[email protected]"));        Users.put ("LDH", New User (2, "LDH", "123", "Andy Lau", "[email protected]"));        Users.put ("GFC", New User (3, "GFC", "123", "Aaron Kwok", "[email protected]"));        Users.put ("LM", New User (4, "LM", "123", "Dawn", "[email protected]"));    Users.put ("Zxy", New User (5, "Zxy", "123", "Jacky", "[email protected]")); }//List page   @RequestMapping (value= "/users", method = requestmethod.get) public String list (model model) {Model.addattribut        E ("Users", users);    return "User/list";         }//link to the Add page will use this code @RequestMapping (value= "/add", method = Requestmethod.get) public String Add (model model) {        Open Modeldriven Model.addattribute (New User ());    return "User/add";    }//In the specific add user, is a POST request, access the following code @RequestMapping (value= "/add", method = Requestmethod.post)//must follow validate write authentication class Public String Add (@Validated User user,bindingresult br) {//If there is an error directly jump to the Add view if (Br.haserrors ()) {return "User    /add ";    } users.put (User.getusername (), user);           return "Redirect:/user/users"; }  }
3. Given the additional information, there should be a server-side validation, so import the Bean-validate.jar package here and modify the user file

Package Com.qzp.model;import Javax.validation.constraints.size;import Org.hibernate.validator.constraints.Email; Import Org.hibernate.validator.constraints.notempty;public class User {private int id;private String username;private String Password;private string Nickname;private string Email;public User () {super ();} public User (int ID, string username, string password, string nickname,string email) {super (); this.id = Id;this.username = Username;this.password = Password;this.nickname = Nickname;this.email = email;} public int getId () {return ID;} public void setId (int id) {this.id = ID;} @NotEmpty (message= "username cannot be empty! ") Public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;} @Size (min=1,max=10,message= "password is greater than 1 bit less than 10 bits!) ") Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;} @Email (message= "Mailbox is not formatted correctly! ") Public String Getemail () {return email;} public void Setemail (String email) {This.email = email;} Public String Getnickname () {return nickname;} public void Setnickname (String nickname) {this.nickname = nickname;}}
4. View layer, first list page

Jstl Tag Library to introduce, here is an iteration tag, spring MVC does not own a set of tag library, so this side is still used Strust2

Users is a map object, so you get the value through his key.

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><% @taglib prefix= "C" uri= "/HTTP/ Java.sun.com/jsp/jstl/core "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
(Display user Name list page) execution results are as follows:

5. Next to the Add page

Also import the Spring form tag Library, if the verification information does not pass, will prompt the information

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%@ taglib prefix= "SF" uri= "/HTTP/ Www.springframework.org/tags/form "%>

The result of the Add page, if added successfully, will jump to the User/list page.



[4] Spring MVC Learning Notes

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.