Spring learning SpringMVC framework quick setup for User Login
I will not talk much about SpringMVC. I have searched a lot on the Internet, and many big bird blogs have detailed descriptions. I have learned SpringMVC with Dr. Kai, and I have written very well, springMVC's processes and principles are very detailed. Here I will reference the pictures and texts of Dr. Kai Tao. If you want to read the original text, click the link above.
SpringMVC request processing Flowchart
You must take a closer look. It is best to take a piece of paper and draw a picture, which is more effective than simply looking at it. You can compare it with the pure MVC mode. This is not so difficult to understand.
The above figure is refined here
Here we can see the specific core development steps:
DispatcherServlet in web. the deployment description in xml to intercept the request to the configuration of Spring Web MVC HandlerMapping, map the request to the configuration of the processor HandlerAdapter, and support the configuration of ViewResolver for various types of processors, resolve the logical view name to the configuration of the specific view technical processor (page Controller) for function processing.
Match the five steps above with the figure one by one. Subsequent development will follow these five steps!
Then I want to implement a simple user login verification example to verify it. After all, practice is the only way to verify the theory and the best way to learn it!
There is no need to use databases. Here I just want to learn the runtime mechanism of SpringMVC.
First, create a Web project named SpringLoginDemo.
In the required package for import (download below)
What will we do next? Looking at the five steps above, deploy web. xml in the first step to intercept requests to Spring Web MVC
Web. xml
Index. jsp
Dispatcher
Org. springframework. web. servlet. DispatcherServlet
ContextConfigLocation
Classpath: applicationContext. xml
Dispatcher
*. Do
Don't forget to write 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, 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 {// 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 {// This should not be the case, but it seems easier to understand String username = request. getParameter ("username"); String password = request. getParameter ("password"); User user User = getUser (username, password); // Save the corresponding parameters and return Map through ModelAndView
Model = new HashMap
(); If (user! = Null) {model. put ("user", user); return new ModelAndView (getSuccessView (), model);} else {model. put ("error", "incorrect user name or password !!! "); Return new ModelAndView (getFailView (), model) ;}}// the verification method public User getUser (String username, String password) {if (username. equals ("test") & password. equals ("test") {return new User (username, password) ;}else {return null ;}}}
Next, the core configuration file applicationContext. xml is very important, including the 2, 3, and 4
LoginController
Let's see a piece.
In fact, A should be in web. xml.
Dispatcher
*.do
This is the start. The next step is the operation procedure described by the arrow above.
There are two more jsp pages
login.jsp${error }
ShowUser. jspUser Information
Username: $ {account. cardNo}
Password: $ {account. password}
Welcome to log on !!!
The last step is to run ....
Download
Some references in this article come from: http://jinnianshilongnian.iteye.com/blog/1594806