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

Commons-logging-1.1.3.jar

Spring-beans-4.2.5.release.jar

Spring-context-4.2.5.release.jar

Spring-context-support-4.2.5.release.jar

Spring-core-4.2.5.release.jar

Spring-expression-4.3.0.release.jar

Spring-web-4.2.5.release.jar

Spring-webmvc-4.2.5.release.jar

Spring-aop-4.3.0.release.jar

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://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" xsi:schemalocation= "Http://www.springframework.org/schema/beans        Http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context.xsd "> <!--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= "/hello.do" class= "Com.wang.controler.HelloControler" ></bean& Gt;</beans>
D. Preparation of Hellocontroler.java
Package Com.wang.controler;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://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" xsi:schemalocation= "Http://www.springframework.org/schema/beans        Http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context.xsd "> <!--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= "Com.wang.controller"/></beans> 
New Hellocontroller.jar (focus) under D.SRC directory:
Package Com.wang.controller;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 http://jinnianshilongnian.iteye.com/blog/1594806

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