Spring-MVC entry instance (1)

Source: Internet
Author: User

Spring-MVC entry instance (1)

Spring MVC is a very good MVC Framework. Since its release in version 3.0, more and more teams have chosen Spring 3 MVC. The structure of Spring MVC is simple. The sentence is simple and beautiful, and it is powerful and flexible, and has excellent performance.


Struts2 VS Spring MVC

Struts2 features

It is also an excellent MVC Architecture with many advantages, such as a good structure. However, the disadvantage is that Struts2 uses the value stack, OGNL expression, and struts2 tag library, which will lead to a reduction in application performance. Struts2 has good performance in multi-layer interceptor and multi-instance action.


Advantages of spring 3 MVC


1. Spring3 MVC is less difficult to learn than Struts2, and Struts2 does not provide many additional functions. Well, of course this is not the deciding factor.

2. Spring3 MVC can easily write programs with excellent performance. Struts2 must be careful to write programs with excellent performance (referring to the MVC part)

3. Spring3 MVC's flexibility is beyond your imagination. Spring's scalability is well known, and Spring3 MVC certainly will not lag behind, and will not feel any restrictions due to the use of the MVC framework.



First, let's learn about MVC: Model-View-Control.

The main work to be done at the C-layer of the framework is to encapsulate web requests as a data object, call the business logic layer to process data objects, return data processing results, and send corresponding views to users.


The core of the Spring C-layer framework is DispatcherServlet, which distributes requests to different backend processors, that is, a mode called Front Controller is used (this mode is briefly described later ). Spring's C-layer framework uses backend controllers, ing processors, and view parsers to complete the main work of the C-layer framework. In addition, the C-layer framework of spring is really assembling the data results processed by the business layer and the corresponding views into an object, that is, the ModelAndView object we will often use later.


Spring MVC solves the following problems for the mature Model-View-Control framework:
1. encapsulate the input elements on the web page as a (request) data object.
2. Schedule the corresponding logical processing units based on different requests and pass in the (request) data object as a parameter.
3. A result data object is returned after the logical Processing Unit completes the operation.
4. Combine and present the data in the result data object with the pre-designed presentation layer to the user.


I. Entry instances 1. Build the environment



In the official spring API documentation, we provide an overview of the functions of all packages, and list common packages and their functions:

Org. springframework. aop-3.0.5.RELEASE.jar: packages related to Aop Programming

Org. springframework. beans-3.0.5.RELEASE.jar: Provides simple bean operations

Org. springframework. context-3.0.5.RELEASE.jar: built on the beans package to process resource files and internationalize.

Org. springframework. core-3.0.5.RELEASE.jar: spring core package

Org. springframework. web-3.0.5.RELEASE.jar: web core package that provides web layer Interfaces

Org. springframework. web. servlet-3.0.5.RELEASE.jar: a specific implementation package for the web layer, where DispatcherServlet is also located.

Later versions are all carried out in spring3.0. For convenience, we recommend that you import all jar packages of spring3.0 in the setup environment (all jar packages are in the dist directory ).


2. Write a welcome blog -- lishehe instance

Create lishehespringMVC1 and import the jar package listed above.




Spring can load files from classpath by specifying classpath *: With classpath: prefix and path, such as bean definition files.
Classpath *: to load the same file from multiple jar files. Classpath: only the first file found can be loaded.


Compile the web. xml configuration file and enable the Spring request receiver DispatcherServlet

The Code is as follows:


 
   
  
   
LishehespringMVC1
    
      
   
    
Index.html
       
   
    
Index.htm
       
   
    
Index. jsp
       
   
    
Default.html
       
   
    
Default.htm
       
   
    
Default. jsp
     
    
      
   
    
SpringMVC
       
   
    
Org. springframework. web. servlet. DispatcherServlet
       
   
       
    
     
ContextConfigLocation
        
    
     
Classpath *: config/spring-servlet.xml
        
           
   
    
1
     
    
      
   
    
SpringMVC
       
   
    
/
     
  
 



3: Compile HelloWorldController. java class and implement the Controller Interface. Generally, the more lightweight the framework is, the better. If a new class needs to implement other interfaces or inherit other classes, the coupling of the class will be very large.


Package com. tgb. web. controller; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. springframework. web. servlet. modelAndView; import org. springframework. web. servlet. mvc. controller; public class HelloWorldController implements Controller {@ Overridepublic ModelAndView handleRequest (HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {System. out. print ("--- hellotigaoban ---"); String hello = "lishehe hello"; // return to the foreground page. Based on the result String, return new ModelAndView ("/welcome", "result", hello);} // annotation and xml, now, annotations greatly improve the development efficiency}



Spring-servlet.xml


 
 
  
  
 

New foreground page welcome. jsp

$ {} JSTL expression


<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %>
 Insert title hereWelcome to my blog-Li shehe
Transmit data $ {Result}

Effect after running:


Summary

Mastering the above Spring MVC has already had a good foundation, and almost can cope with any development. After mastering these skills, you can use more in-depth and flexible technologies, such as multiple view technologies, such as Jsp, Velocity, Tiles, iText, and POI. The Spring MVC Framework does not know the view used, so it does not force you to only use JSP technology.


Next, Let's explain the difference between springMVC and Struts. Please look forward to it!

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.