Spring MVC Environment builds and implements simple HelloWorld

Source: Internet
Author: User
Tags tomcat server

First step: Create a new Web project named "Testspringmvc" using Eclipse, which configures the Tomcat server

Step two: Copy the jar package you are using to the Web-inf/lib directory

Step Three: Configure Dispatcherservlet in Web. xml

Dispatcherservlet is SPRINGMVC's entrance, the essence of SPRINGMVC is a servlet

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"ID= "webapp_id"version= "3.0">3   4     <!--Configure Dispatcherservlet -5     <servlet>6         <Servlet-name>Springdispatcherservlet</Servlet-name>7         <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>8         9         <Init-param>Ten             <Param-name>Contextconfiglocation</Param-name> One             <Param-value>Classpath:springmvc.xml</Param-value> A             <!--Configuring an initialization parameter for Dispatcherservlet - -         </Init-param> -         <load-on-startup>1</load-on-startup>  the     </servlet> -     <!--Map All requests to the Dispatcherservlet for handling - -     <servlet-mapping> -         <Servlet-name>Springdispatcherservlet</Servlet-name> +         <Url-pattern>/</Url-pattern> -     </servlet-mapping> + </Web-app>

the meaning of Load-on-startup in Web. XML is whether the tag container loads the servlet when it is started.

A value of 0 or greater than 0 o'clock indicates that the container loads the servlet when the app is started;

When it is a negative number or is not specified, it indicates that the container is loaded only when the servlet is selected.

The lower the value of a positive number, the higher the priority to start the servlet.

Url-pattern in the servlet:

When the browser initiates a URL request and the request is sent to the servlet container, the container first subtracts the requested URL from the path of the current application context as a mapped URL for the servlet, such as the URL is http://10.43.11.143/myapp/kata/ Detail.html, the application context is MyApp, the container will Http://10.43.11.143/myapp removed, the remainder of the/kata/detail.html to do the servlet mapping matching. This mapping matching process is prioritized (described later in the order of precedence rules), and when a servlet match succeeds, the remaining servlet is not ignored.

If <url-pattern> is configured as one of the following two types of

<url-pattern>/</url-pattern>

<url-pattern>/*</url-pattern>

All URLs can be matched. where/* is the path match, just the path is/.

Fourth step: Create a SPRINGMVC configuration file

Create a new XML file named "Springmvc" under Web-inf

<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:cache= "Http://www.springframework.org/schema/cache"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4. 0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/contex T/spring-context-4.0.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/ Schema/mvc/spring-cache-4.0.xsd ">                <!--Configuring a custom scanned package -    <Context:component-scanBase-package= "Com.java.springmvc"></Context:component-scan>    <!--Configure the View resolver: How to parse the handler method into a physical view -    <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        < Propertyname= "prefix"value= "/web-inf/views/"></ Property>        < Propertyname= "suffix"value= ". jsp"></ Property>    </Bean></Beans>

Fifth step: Create the processor that handles the request

Create a new class as a processor under Java resource

 Packagecom.java.springmvc.handlers;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping; @Controller Public classHelloWorld {/*** The URL of the request using the @requestmapping annotation map * The return value is returned as the actual physical view through the view parser *@return     */@RequestMapping ("/helloworld")     PublicString Hello () {System.out.println ("Hello World"); return"Success"; }}

Sixth step: Create a View

Create a JSP file named "Index" under WebContent

<%@ Page Language="Java"ContentType="text/html; charset=iso-8859-1"pageencoding="iso-8859-1"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=iso-8859-1"><title>Insert Title here</title></Head><Body><a href= "HelloWorld" >hello world</a></Body></HTML>

Under Web-inf, create a folder named Views, and then create a JSP file named success under the file.

<%@ Page Language="Java"ContentType="text/html; charset=iso-8859-1"pageencoding="iso-8859-1"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=iso-8859-1"><title>Insert Title here</title></Head><Body>    <h4>Success page</h4></Body></HTML>

Structure diagram under webcontent after creation

After the final compilation, deploy to Tomcat to run the project:

After clicking "Hello World"

Spring MVC Environment builds and implements simple HelloWorld

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.