Preliminary exploration of SPRINGMVC--environment construction and first HelloWorld simple project

Source: Internet
Author: User

Note: This article is for the study Springmvc, makes the note collation.

What does the MVC framework do?

A, map URLs to Java classes, or Java class methods

b, encapsulate user-submitted data

C, processing requests, calling related business processing, encapsulating the response data

D, render the data in response

What are the advantages of the SPRINGMVC framework?

Better performance than struts2

Simple and easy to learn, can be more concise web layer development

Natural and Spring Seamless integration

Usage conventions better than configuration

Ability to perform simple junit tests

Support RESTful style

Exception handling, localization internationalization, data validation, interceptors

Provides a powerful set of JSP tag libraries to simplify JSP development

Simple understanding of the structure and process of SPRINGMVC

This is a SPRINGMVC structure flowchart found on the Internet:


Environment Building and Hello Springmvc simple Case 1. Profile Development (non-annotated) A. Importing related jar packages










B.. Configuring Dispatcherservlet in Web. xml
<!--Configuring the dispatcher dispatcher--  <servlet>      <servlet-name>springmvc</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      <!--booting with the system-    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>      <servlet-name>springmvc</servlet-name>      <url-pattern>*.do</url-pattern>  </servlet-mapping>
C. Add the SPRINGMVC configuration file, which is added by default in the Web-inf directory, springmvc-servlet.xml
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://" xmlns: Xsi= "Http://" xmlns:p= "" xmlns:context=         "Http://" xsi:schemalocation= "Http://        Http:// Http:// Http:// "> <!--configuration handlermapping-&lt ; Bean class= "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" ></bean> <!-- Configuration Handleadapter--<bean class= "Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" > </bean> <!--configuration renderer-<bean id= "Jspviewresolver" class= "Org.springframework.web.servlet.view.Interna Lresourceviewresolver "> <property name=" viewcLass "value=" Org.springframework.web.servlet.view.JstlView "/> <!--result view prefixes--<property name=" p Refix "value="/web-inf/jsp/"/> <!--result view suffix--<property name=" suffix "value=". jsp "/> &lt ;/bean> <!--configuration requests and processors--<bean name= "/" class= "" ></bean& Gt;</beans>
D. Preparation of
Package;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 Hellocontroler implements controller{    @Override Public    Modelandview HandleRequest (httpservletrequest request,            HttpServletResponse response) throws Exception {        Modelandview mv=new modelandview ();        Mv.addobject ("msg", "Hello Springmvc");        System.out.println ("111");        Mv.setviewname ("Hello");        return mv;    }    }

Create a new hello.jsp in the Web-inf directory, write a simple expression ${msg}, and test in the browser.

Using annotations A. Guide jar package (same) b. Configure Dispatcherservlet in Web. XML (this time configure a contextconfiglocation parameter to place mvc.xml in the SRC directory):
<servlet>      <servlet-name>springmvc</servlet-name>      <servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class>      <init-param>          < Param-name>contextconfiglocation</param-name>          <param-value>classpath:mvc.xml</ param-value>      </init-param>      <load-on-startup>1</load-on-startup>  </servlet >  <servlet-mapping>      <servlet-name>springmvc</servlet-name>      <url-pattern >*.do</url-pattern>  </servlet-mapping>
C. Create a new Mvc.xml file in the SRC directory:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://" xmlns: Xsi= "Http://" xmlns:p= "" xmlns:context=         "Http://" xsi:schemalocation= "Http://        Http:// Http:// Http:// "> <!--Configuration renderer-<bean id=" J Spviewresolver "class=" Org.springframework.web.servlet.view.InternalResourceViewResolver "> <property name=" Viewclass "value=" Org.springframework.web.servlet.view.JstlView "/> <!--result view prefixes--<property NA    Me= "prefix" value= "/web-inf/jsp/"/> <!--result view suffix--<property name= "suffix" value= ". jsp"/> </bean> <!--Configure the packages that need to be scanned (heavyDots)--<context:component-scan base-package= ""/></beans> 
New Hellocontroller.jar (focus) under D.SRC directory:
Package;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.servlet.modelandview;@ Controllerpublic class Hellocontroller {    @RequestMapping ("/hello") public    Modelandview Hello ( HttpServletRequest req,httpservletresponse Res) {        Modelandview mv=new modelandview ();        Mv.addobject ("msg", "Hello World");        Mv.setviewname ("Hello");        return mv;    }}

Reference Blog

Preliminary exploration of SPRINGMVC--environment construction and first HelloWorld simple project

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: 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.