Beginners ' understanding of spring MVC

Source: Internet
Author: User

The first thing to be sure is, what does that mean? Spring MVC is a follow-on product of Springframework and has been incorporated into spring Web flow while spring MVC separates the controller, model object, Dispatcher ( Actually I don't know what this is) and the role of the handler object, and this separation makes it easier to customize.

Said these very official words, I have a little dizzy, hehe, first a principle flowchart, I believe it will be more intuitive!

Spring MVC schematic diagram

Spring MVC Correspondence principle Process steps:

1.Web client-to-Dispatcherservlet user sends a request to, first to Dispatcherservlet (first to the front controller for global Process Control)

2.DispatcherServlet--Handlermapping resolve the request submitted by the user at the foreground and assign to the specified controller (equivalent to action in struts2) as per our requirements

3.DispatcherServlet--The controller enters the specified controller object, executes the inside method, returns the required String (Modelandview), and the controller needs to operate on the bean. Which means interacting with the model.

4.DispatcherServlet--Viewresolver parse the string returned from the controller and convert it to a valid JSP path

5.DispatcherServlet--JSP converts a good JSP path to a specific page

6.JSP--Web client displays a specific page in the Client interface

Well, the above is the spring MVC process, is not a certain understanding, not urgent, there is ...

With a basic understanding, let's start coding! (I'm using a Springframework-3.2.8.)

As you all know, when you open the compiler and prepare to start developing a project, the first step is to create a new project. Ha ha!

There will be no words, hehe, not other, just a lively atmosphere, the reason you understand.

OK, this step is not included in the coding step, the following official start!

First step: Add a rack Package

Do you think it's very small, yes, this is the Spring MVC feature, continue to let it overturn your thoughts.

Step two: Must be the Web. xml file

Here is the dispatcherservlet mentioned above--Front controller

Behind the "*.do" is used to intercept the page user's request, here is a. Do end of the request will hit, of course, this can be defined by themselves, it is necessary to explain, no matter what they are defined, must abide by, hehe, is not suddenly feel good and reasonable!

Step Three: Configure the Mvc-servlet.xml file in Web-inf (requires that the name must begin with the first name in the <servlet-name> node in Web. XML +servlet.xml) Just go:

Java code

<span style= "Font-family:courier new;color: #6600CC;" ><</span><span style= "color: #6600CC;" >?xml version= "1.0" encoding= "UTF-8"?>

<span style= "Font-family:courier New;" ><</span>beans xmlns= "Http://www.springframework.org/schema/beans"

Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p"

xmlns:context= "Http://www.springframework.org/schema/context"

Xmlns:mvc= "Http://www.springframework.org/schema/mvc"

Xsi:schemalocation= "

Http://www.springframework.org/schema/beans

Http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

Http://www.springframework.org/schema/context

Http://www.springframework.org/schema/context/spring-context-3.2.xsd

Http://www.springframework.org/schema/mvc

Http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

">

<bean id= "Parametermethodnameresolver"

class= "Org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver" >

<property name= "ParamName" value= "action" ></property>

</bean>

<bean id= "user" class= "Com.mvc.controller.UserController" >

<property name= "Methodnameresolver" ref= "Parametermethodnameresolver" ></property>

</bean>

<span style= "color: #999999;" ><!--Resolve requests on the address bar--></span>

<bean class= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >

<property name= "Mappings" >

<props>

<!--key corresponding to the Url-pattern, the value is the controller's bean ID--

<prop key= "Login.do" >user</prop>

</props>

</property>

</bean></span>

<span style= "color: #999999;" >

</span><span style= "color: #6600CC;" ><span style= "color: #999999;" ><!--Personal Indication is not recommended--

<!--You cannot use this method when a controller jump is required--

<!--view parser, before forwarding it to the model to be processed (that is, intelligently converts the returned string into a page)--></span>

<bean class= "Org.springframework.web.servlet.view.UrlBasedViewResolver" >

<property name= "Viewclass"

Value= "Org.springframework.web.servlet.view.JstlView" ></property>

<property name= "prefix" value= "/"/>

<property name= "suffix" value= ". jsp"/>

</bean>

</beans></span>

Need to explain: if you need to call the unused method in a controller, you must specify the name of the definition method, for example, the path to the request is: User.do?action=userlogin (1). The name of the definition method needs to be specified: <property name = "ParamName" value= "action"/> defined, the program will know after the action

is a method that needs to be entered.

(If you're calling the same method, of course you don't need to define this, but as long as a controller needs a different approach, it needs to be defined)

(2). After parsing the controller path, where spring's inversion of control (IOC) is used, the others are more different.

<property name= "Methodnameresolver" ref= "Parametermethodnameresolver"/> Define Method name Resolver 2.1 for controller. You can apply an already defined parser (see above)

2.2. You can define a parser for each bean individually

(3). Resolve all requests from the page and assign to a different controller

(4). In the configuration file, if you use the contents of the last bean, if you need to return to the "index.jsp" page, you can return to "index" OK, this configuration file is for us to return the string, before and after automatically add "/" and ". JSP".

Once you have this configuration, you cannot jump to the controller in the controller because it adds "/" and ". JSP" before and after all return strings, which is obviously not what we need, so this method is not generally used!

Here, need to calm down, a good idea.

Fourth step: Define a Usercontroller and inherit the Multiactioncontroller class

Java code

<span style= "Font-family:courier new;color: #6600CC;" >publicclassusercontrollerextendsmultiactioncontroller {

Publicmodelandview Login (HttpServletRequest req,

HttpServletResponse resp, user user) throwsexception{

String result = "";

if (User.getname (). Equals ("Jack")) {

Req.getsession (). SetAttribute ("user", user);

result = "Login.do?action=login";//can Jump between controllers

result = "index.jsp?age=22";//value can be passed to the page

}else{

result = "login.jsp";

}

Returnnewmodelandview (result);

}

}</span>

(1). The above code inherits the Multiactioncontroller class, meaning that it can be used to define a number of different methods to be determined by the name of the action following the request

Of course, you can also directly complete the controller interface, which means that there is only one method in the current controller, and must be given

(2). The parameters in the method are two by default, the Httpservletrequset and HttpServletResponse objects, and, of course, the parameters of an object type can be defined, mainly by simplifying the form submission of multiple parameters (currently only known here) Write here, almost, no accident, you should be able to complete a relatively simple

Small project of the Spring MVC framework

Technology sharing: Edith Academy

Beginners ' understanding of spring MVC

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.