Project structure;
The code is as follows:
Bookcontroller
Package Com.mstf.controller;import Javax.servlet.http.httpservletresponse;import Org.apache.commons.logging.Log; Import Org.apache.commons.logging.logfactory;import Org.codehaus.jackson.map.objectmapper;import Com.mstf.domain.book;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestbody;import org.springframework.web.bind.annotation.RequestMapping; @Controller @requestmapping ("/json") public class Bookcontroller {private static final Log logger = Logfactory.getlog (bookcontroller.class);//@RequestMapping based on JSON data, go Replace with the corresponding object@requestmapping (value= "/testrequestbody") public void Setjson (@RequestBody book Book,httpservletresponse Response) throws Exception {//Objectmapper class is the main class of the Jackson library. He provides some functionality to convert the Java object to the corresponding Jsonobjectmapper mapper = new Objectmapper ();//Convert the book object to a JSON output logger.info (mapper.writevalueas String (book)); Book.setauthor ("Wang"); Response.setcontenttype ("Text/html;charset=utf-8");//Convert book object to JSON Write to Client Response.getwriter (). println (mapper.writevalueasstring (book));}}
Usercontroller
Package Com.mstf.controller;import Java.util.arraylist;import Java.util.list;import Org.apache.commons.logging.Log ; Import Org.apache.commons.logging.logfactory;import Com.mstf.domain.user;import Org.springframework.stereotype.controller;import Org.springframework.ui.model;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import org.springframework.web.bind.annotation.requestparam;//controller annotations are used to indicate that the class is a controller that can handle multiple request actions at the same time @controller// Requestmapping can be used to annotate a controller class, at which point all methods are mapped to requests relative to the class level//indicates that the controller handles all requests that are mapped to the path indicated by the Value property @requestmapping (value= "/ User ") public class Usercontroller {//Static list<user> collection, where the database is used to hold registered user information private static list<user> userlist ;//Usercontroller class constructor, initialize list<user> collection public Usercontroller () {super (); userlist = new arraylist<user> () ;} Static Log class Logfactoryprivate static final Log logger = Logfactory.getlog (usercontroller.class);//The method maps the request to http:// localhost:8080/context/uSer/register, this method supports GET request @requestmapping (value= "/register", method=requestmethod.get) public String registerform () { Logger.info ("Register get method called ...");//Jump to registration page return "register";} The request for the method map supports the POST request @requestmapping (value= "/register", Method=requestmethod.post)//assigns the value of the LoginName parameter in the request to LoginName variables, password and username also handle the public string register (@RequestParam ("LoginName") string loginName, @RequestParam ("Password ") string PassWord, @RequestParam (" UserName ") string userName) {logger.info (" register post method called ... ");//create User Object user user = new User (); user.setloginname (loginName); User.setpassword (PassWord); User.setusername (userName);//Analog database storage User Information Userlist.add (user);//Jump to login page return "login"; The method maps the request to http://localhost:8080/requestmappingtest/user/login@requestmapping ("/login") public String Login (// Assigning the value of the LoginName parameter in the request to the LoginName variable, PassWord also handles the @requestparam ("LoginName") String LoginName, @RequestParam ("PassWord" String Password,model Model) {logger.info ("Login name:" +loginname + ")Password: "+ PassWord);//To the collection to find whether the user exists, which is used to impersonate the database validation for (User user:userlist) {if (User.getloginname (). Equals (LoginName) & & User.getpassword (). Equals (PassWord)) {Model.addattribute ("user", user), return "Welcome";}} return "Login";}}
Book
Package Com.mstf.domain;import Java.io.serializable;public Class book implements Serializable {private static final long Serialversionuid = 1l;private int id;private string Name;private string author;public Book () {}public book (int ID, string Name, String author) {super (); this.id = Id;this.name = Name;this.author = author;} public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String Getauthor () {return author;} public void Setauthor (String author) {this.author = author;} @Overridepublic String toString () {return "book [id=" + ID + ", name=" + name + ", author=" + author + "]";}}
User
Package com.mstf.domain;import java.io.serializable;//domain object, implementing the serialization interface public class User implements Serializable {// Serializes private static final long Serialversionuid = 1l;//private field privately string Loginname;private string Username;private string password;//Common Constructor Public User () {super ();} Get/set method Public String Getloginname () {return loginName;} public void Setloginname (String loginName) {this.loginname = LoginName;} Public String GetUserName () {return userName;} public void Setusername (String userName) {this.username = UserName;} Public String GetPassword () {return passWord;} public void SetPassword (String passWord) {This.password = PassWord;}}
Springmvc-config.xml
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns: context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/sche Ma/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd Http://www.springframework.org/sch EMA/MVC http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd Http://www.springframework.org/sch Ema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd "> <!--Spring can automatically To scan the Java file under the Base-pack package or the sub-package, if you scan to a class with spring-related annotations, register these classes as spring beans--and <context:component-scan Base-package= "Com.mstf.controller"/> <!--setup configuration scheme-<mvc:annotation-driven/> <!--using the default Servle T to respond to static files--<mvc:default-servlet-handler/> <!--View Resolver--<bean id= "Viewresolver" class= "Org.springframework.web.servlet.view.Intern Alresourceviewresolver "> <!--prefix--<property name=" prefix "> <value>/web-i Nf/jsp/</value> </property> <!--suffix--<property name= "suffix" > & lt;value>.jsp</value> </property> </bean> </beans>
login.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Register.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >welcome.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Xml
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" Http://java.sun.com/xml/ns/javaee http://java.sun.com /xml/ns/javaee/web-app_3_0.xsd "version=" 3.0 "> <!--define the front controller of Spring MVC--><servlet> <servlet -name>springmvc</servlet-name> <servlet-class> Org.springframework.web.servlet.Dispatcher Servlets </servlet-class> <init-param><param-name>contextconfiglocation</param-name><p Aram-value>/web-inf/config/springmvc-config.xml</param-value></init-param> <load-on-startup >1</load-on-startup> </servlet><!--Let Spring MVC's front-end controller intercept all requests--<servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mappi Ng> <!--garbled filter--><filter><filter-name>characterencodingfilter</filter-name><filter-class> Org.springframework.web.filter.characterencodingfilter</filter-class><init-param><param-name >encoding</param-name><param-value>utf-8</param-value></init-param><init-param ><param-name>forceencoding</param-name><param-value>true</param-value></ Init-param></filter><filter-mapping> <filter-name>characterencodingfilter</filter-name > <url-pattern>/*</url-pattern> </filter-mapping></web-app>
Index.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE html>All the packages used are as follows:
Spring MVC Login Registration and conversion of JSON data