SpringMvc (framework understanding and the first SpringMvc Program)

Source: Internet
Author: User

SpringMvc (framework understanding and the first SpringMvc Program)
Question: from today on, we will update at least two blogs every week, review the framework knowledge, and record it in a gradual manner. First, we will make it easier for you to review later, so that Java beginners can better learn this knowledge.
I. Spring mvc introduces Spring MVC, which is a follow-up product of Spring framework and has been integrated into Spring Web Flow. The Spring framework provides a full-featured MVC module for Building Web applications. Using Spring-pluggable MVC Architecture, you can choose to use a built-in Spring Web framework or a Web framework such as Struts. Spring web mvc and Struts2 both belong to the presentation layer framework, which is part of the Spring framework. We can see from the overall structure of Spring:

Ii. Web mvc Structure

Note:

1. the user initiates a request to the Controller to control the data that receives the user request and delegates it to the model for processing.

2. A Model is usually used by a controller to process data and obtain the processing result.

3. The controller displays the model data in the View. The model cannot directly display the data in the View. It must be done through the Controller. If the model is used in the C/S application, the data can be displayed in the view.

4. The controller returns the view response to the user and presents the data or processing result to the user through the view.

Iii. Spring mvc Architecture
1. process description

1. the user sends a request to the front-end controller DispatcherServlet

2. The DispatcherServlet receives a request to call the HandlerMapping processor er.

3. The processor er finds the specific processor and generates the processor object and the processor Interceptor (if any) and returns it to DispatcherServlet.

4. DispatcherServlet calls HandlerAdapter processor Adapter

5. HandlerAdapter is used to call a specific processor (Controller, also called a backend Controller ).

6. After the Controller is executed, return ModelAndView.

7. HandlerAdapter returns the controller execution result ModelAndView to DispatcherServlet

8. DispatcherServlet transmits ModelAndView to the ViewReslover view parser.

9. After ViewReslover resolution, return the specific View

10. DispatcherServlet renders the View based on the View (filling model data into the View ).

11. Respond to users with DispatcherServlet

2. Component Description

The following components are generally implemented using the framework (that is, the actual development in an enterprise can meet most of the requirements ):

DispatcherServlet: as a front-end controller, the entire process control center controls the execution of other components, unified scheduling, reducing coupling between components, and improving the scalability of each component.

HandlerMapping: extends the processor ing to implement different ing methods, such as the configuration file method, interface method, and annotation method.

HandlAdapter: supports more types of processors through the extended processor adapter.

ViewResolver: extends the view parser to support more types of view parsing, such as jsp, freemarker, pdf, and excel.

 

 

The following two components usually need to be developed:

Handler: processor, that is, the backend controller is represented by controller.

View: View, that is, the interface displayed to the user. The tag language is usually used to display model data in the View.

 

3. SpringMvc for HelloWorld test and development environment: win7 + Eclipse + Tomcat71. create the project in the next two steps and simply use the default one. The created project is as follows: 2. Add the SpringMvc Jar package. I will release a Baidu cloud link to the jar used by the blog at the end of the article for you to download. Select All and copy to/springmvc_blog_001/WebContent/WEB-INF/lib in the project. 3. Compile the Web. xml configuration Controller
 
 
  
   
Springmvc_blog_001
  
  
  
   
    
Springmvc
   
   
    
Org. springframework. web. servlet. DispatcherServlet
   
   
    
     
ContextConfigLocation
    
    
     
Classpath: springmvc-servlet.xml
    
   
   
    
1
   
  
  
   
    
Springmvc
   
   
    
*. Action
   
  
  
   
    
Index. jsp
   
  
 
Configuration instructions:

Load-on-startup: indicates that the servlet is started with the service;

Url-pattern: *. The action must be handled by DispatcherServlet.

ContextConfigLocation: Specifies the loading location of the springmvc configuration. If not specified, the WEB-INF/[DispatcherServlet Servlet name]-servlet. xml is loaded by default.

4. Compile the SpringMvc configuration file to create the WEB-INF file under the springmvc-servlet.xml 1). Configure the processor ER in the springmvc-servlet.xml file configuration as follows:
 
 
  
  
 
Note: BeanNameUrlHandlerMapping indicates that the Bean name defined is used as the request url. You must configure the controller in the spring container and specify the bean name as the request url, and must start. end of action

 

2) configure the processor Adapter


Simple: SimpleControllerHandlerAdapter: A simple Controller processing Adapter. All beans that implement the org. springframework. web. servlet. mvc. Controller Interface are used as backend controllers of Springmvc.



3) configure the view parser
 
Note:

InternalResourceViewResolver: Support for JSP view parsing viewClass: JstlView indicates that JSTL label library is required for JSP template pages. Therefore, classpath must contain jar packages related to jstl; prefix and suffix: search for the prefix and suffix of the view page. The final view address is: prefix +Logical view name+ Suffix, the logical view name needs to be returned in the controller ModelAndView specified, for example, the logical view name hello, the final returned jsp view Address "WEB-INF/jsp/hello. jsp ". some configuration is omitted for the sake of simplicity of the first program.

 

4). The complete configuration is as follows:
 
 
  
  
   
   
    
    
   
  
 

 

5. Develop a backend Controller)
Package com. billstudy. springmvc. controller; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. springframework. web. servlet. modelAndView; import org. springframework. web. servlet. mvc. controller;/*** HelloWorld Controller * @ author Bill * @ since V1.0 2014/01/14 */public class HelloWorldController implements Controller {@ Overridepublic ModelAndView handleReques T (HttpServletRequest request, HttpServletResponse response) throws Exception {ModelAndView modelAndView = new ModelAndView (); // Save the value modelAndView. addObject ("testMessage", "hi, the first SpringMvc program! "); // Set the page to jump to (View: View) modelAndView. setViewName (" WEB-INF/helloworld. jsp "); return modelAndView ;}}


6. add your own HelloWorldController configuration to the springmvb-servlet.xml
 
 
7. Compile the jsp page to create the helloworld. jsp file under the WEB-INF, now deploy to Tomcat to test! Okay, no problem! If you appear when starting the project, the springmvc-servlet.xml cannot find the error, please go to the Tomcat release directory to see if the spring-servlet.xml location is in classes, probably because Eclipse and Tomcat configuration is different, the location is not the same. You can use the red code to configure the corresponding location. 
 
  
   contextConfigLocation
  
  
   classpath:springmvc-servlet.xml
  
 

 

All source code + jar package Baidu cloud address: Click to open the link

SpringMvc HelloWorld, will you? Here is the big girl who loves life, technology, and friends.

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.