Java Framework-spring MVC Understanding 001

Source: Internet
Author: User

Spring MVC Understanding

Recently read a book "See Through the Springmvc", from the understanding of some more detailed knowledge of the system, read and share it.

The nature of 1.servlet--spring MVC

2.Spring MVC is actually a tool, and the specific understanding can be divided into two steps: The first step is to understand how the tool was created, and the second step to understand how the tool is used.

3. Pre-use Preparation: setting up the environment

① Create Web projects, import jar packages, MAVEN projects simply add dependencies to SPRINGMVC and Servlets.

// maven projects join dependencies <dependency>    <groupId>javax.servlet</groupId>    <artifactId> javax.servlet-api</artifactid>    <version>3.1.0</version>    <scope>provided</ scope></dependency><dependency>    <groupId>org.springframework</groupId>    < artifactid>spring-webmvc</artifactid>    <version> 4.1.5.release</version></dependency >

Not the jar package that the MAVEN project needs to import:

Simple configuration of the ②SPRINGMVC

Configuring a spring MVC requires only three steps: ① configuring Servlet;② to create a spring MVC XML configuration file in Web. ③, creating a controller and view. The following are described separately.

To configure a servlet in Web. xml:

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns= "Http://xmlns.jcp.org/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd "version= "3.1" > <!--Spring MVC configuration Start-up <servlet> <servlet-name>Name</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <ser Vlet-name>name</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> < !--Spring MVC configuration End--<welcome-file-list> <welcome-file>index</welcome-file> &LT;/WELC Ome-file-list></web-app>a servlet called name is configured here, automatically started, and then mapping to all requests.
The configured servlet is the Dispatcherservlet type, which is the gateway to spring MVC, and the essence of Spring MVC is a servlet.
When configuring Dispatcherservlet, you can set the Contextconfiglocation parameter to specify the location of the spring MVC configuration file.
If you do not specify the default use of the Web-inf/[servletname]-servlet.xml file, the default value is used here, that is, the Web-inf/name-servlet.xml file.

Create an XML configuration file for spring MVC
Start with the new let ' Sgo-servlet.xml file in the Web-inf directory and configure it using Spring MVC's simplest configuration.

<!--web-inf/name-servlet.xml--><?xml version= "1.0" encoding= "UTF-8"?>
Introducing Constraints <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.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context.xsdhttp//Www.springframework.org/schema/mvchttp//www.springframework.org/schema/mvc/spring-mvc.xsd "><mvc:annotation-driven/><context:component-scan base- Package= "Com.excelib"/></beans>

<mvc:annotation-driven/> is a one-click configuration provided by spring MVC, and Spring MVC will help us do things like registering components automatically after configuring this tag. This configuration method is very simple,<mvc:-annotation-driven/> the underlying principle will be explained in detail later. A Context:component-scan tag is also configured to scan classes configured by annotations, and if spring is used, the Context:include-filter sub-label can be set to scan only @controller. The other is given to the spring container to manage, but only spring MVC is configured here, so it's all in spring MVC. The configuration for scanning only @controller is as follows:

<context:component-scan base-package = "Com.excelib" use-default-filters= "false" >< Context:include-filter type= "Annotation" expression= "Org.springframework.stereotype.Controller"/></context :component-scan>

Create a controller and view
Now that the spring MVC environment has been built, it's done. Write a controller and a view below so that you can run it.
First, a class--gocontroller is built under the Com.excelib.controller package.

 PackageCom.excelib.controller;ImportOrg.apache.commons.logging.Log;Importorg.apache.commons.logging.LogFactory;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod; @Controller Public classGocontroller {Private FinalLog logger = Logfactory.getlog (Gocontroller.class);//handling "/" Requests of the head type@RequestMapping (value={"/"},method={requestmethod.head}) PublicString Head () {return"Go.jsp";}//handling "/index" and "/" Requests for get types@RequestMapping (value={"/index", "/"},method={requestmethod.get}) PublicString index (model model)throwsException {logger.info ("======processed by index=======");   //return msg parameterModel.addattribute ("msg", "Go Go go!");return"Go.jsp"; }}

This is a separate way of handling the head request, which can be used to detect the state of the server because it does not return a body so it saves network resources more than a GET request. Instead of using the same method as the GET request, and then returning the response without the body, the processing of the GET request might handle something else, such as initializing some of the content that the home page needs to display, and possibly connecting to the database, which is a waste of resources. and is not required for head requests, so it's best to write a separate method.
If viewresolver,spring is not configured MVC will use Org.springframework.web.servlet.view.InternalResourceViewResolver as Viewresolver by default, and both prefix and suffix are empty. So the go.jsp return value corresponds to the go.jsp file in the root directory. We're going to build it out.

<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>${msg}</body>

Well, now that the build is deployed to Tomcat you can run it and run ing .....

Java Framework-spring MVC Understanding 001

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.