The SPRINGMVC framework of spring learning is quickly built to realize user login function

Source: Internet
Author: User

About the introduction of SPRINGMVC I will not say more, online a search a lot, a lot of Big Bird Blog has a detailed description, before watching with open Tao study Springmvc, write very good, SPRINGMVC run the process and the principle of very meticulous in this I quote the picture and text of the Elder, If you want to see the original, click on the link above.

SPRINGMVC process flowchart for processing requests


We must look carefully, the best is to take a piece of paper, draw a picture, can be more than you see the effect, we can compare with the pure MVC model, so it is not so difficult to understand.

To the above figure in this refinement


Here we can see the specific core development steps:

    1. Dispatcherservlet deployment description in Web. XML to intercept requests to spring WEB MVC
    2. Handlermapping configuration to map the request to the processor
    3. Handleradapter configuration to support multiple types of processors
    4. Viewresolver configuration, which resolves the logical view name to the specific view technology
    5. Configuration of the Processor (page controller) for functional processing

The above five steps and figure one by one correspond, then the development is to follow these five steps!

Later I want to implement a simple user login Verification example to verify that, after all, practice is the only way to verify the theory is the best way to learn!

No database, no need, I'm just learning SPRINGMVC's operating mechanism

First build a Web project named Springlogindemo

In the import of the required packages, (download below)


What do we do next? Look at the five steps above, the first step is to deploy Web. XML to intercept requests to spring Web MVC

Xml

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" 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_2_5.xsd "> <display-name></display-name>< welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet > <servlet-name>Dispatcher</servlet-name> <servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class> <!--Spring configuration file--<init-param > <param-name>contextConfigLocation</param-name> <param-value>classpath: applicationcontext.xml</param-value> </init-param> </servlet> <servlet-mapping> < Servlet-name>dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping > </web-app>
don't forget to write the eneity

Package Com.example.entity;public class User {private string username;private string Password;public User (string Username, String password) {super (); this.username = Username;this.password = password;} 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;}}

Next is Logincontroller

Package Com.example.controller;import Java.util.hashmap;import Java.util.map;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.web.servlet.modelandview;import Org.springframework.web.servlet.mvc.AbstractController; Import Com.example.entity.user;public class Logincontroller extends Abstractcontroller {//Success and failure fields private String Successview;private string Failview;public string Getsuccessview () {return successview;} public void Setsuccessview (String successview) {this.successview = Successview;} Public String Getfailview () {return failview;} public void Setfailview (String failview) {this.failview = Failview;} @Overrideprotected Modelandview handlerequestinternal (httpservletrequest request,httpservletresponse response) Throws Exception {//should not be written like this, but it seems easier to understand string username = Request.getparameter ("username"); String Password = request.getparameter ("password"); User user = GetUser (username, password);//Save the corresponding parameter, return map& by ModelandviewLt String,object> model=new hashmap<string,object> (); if (user!=null) {model.put ("user", user); return new Modelandview (Getsuccessview (), model);} Else{model.put ("Error", "username or password entered incorrectly!!!" "); return new Modelandview (Getfailview (), model);}} To facilitate direct write authentication methods public User getUser (String username,string password) {if (Username.equals ("test") && Password.equals ("test")) {return new User (Username,password);} Else{return null;}}}
The next step is the core configuration file Applicationcontext.xml, which is important, including the 2,3,4 in the above steps

<?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:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= " Http://www.springframework.org/schema/tx "xsi:schemalocation=" Http://www.springframework.org/schema/beans http:/ /www.springframework.org/schema/beans/spring-beans-2.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www. Springframework.org/schema/aop/spring-aop-2.0.xsd Http://www.springframework.org/schema/tx Http://www.springfram Ework.org/schema/tx/spring-tx-2.0.xsd "><bean id=" Logincontroller "class=" Com.example.controller.LoginController "><!--note here the two properties, corresponding to the two pages need to jump, one is to display the user, one is login failure or login interface-->< Property Name= "Successview" value= "Showuser" ></property><property name= "Failview" value= "Login" > </property></bean><bean id= "urlmapping" class= "Org.springframework.web.servlet.handler.SimplEurlhandlermapping "><property name=" mappings "><props><prop key="/login.do ">loginController </prop></props></property></bean><bean id= "Viewresolver" class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "><property name=" prefix "value="/"> </property><property name= "suffix" value= ". JSP" ></property></bean></beans>
See a piece



Actually, a should be in Web. Xml.

  <servlet-mapping>  <servlet-name>Dispatcher</servlet-name>  <url-pattern>*.do </url-pattern>  </servlet-mapping>  
This is the beginning, and then there's the flow of the above arrows.
There are also two JSP pages

Login.jsp<body>${error}<form action= "login.do" method= "POST" > User login <br>
showuser.jsp<body> User Information <br> user name: ${account.cardno}<br> Password: ${account.password}<br><font Color= "Red" > Welcome to LOGIN!!! </font><br></body>
the last is the run ....






SOURCE download

This section of this article refers to the following: http://jinnianshilongnian.iteye.com/blog/1594806

The SPRINGMVC framework of spring learning is quickly built to realize user login function

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.