Notes to Hello Spring MVC

Source: Internet
Author: User

To build a package required for a SPRINGMVC:

1. Create a MYSPRINGMVC project, using the Dynamic Web project approach.

2. Import the jar package and place it in the/webcontent (WebRoot)/web-inf/lib directory

3. Associated jar package, Build Path

4. Create the Web. XML in the Web-inf directory
Configure Spring MVC's ingress Dispatcherservlet to submit all requests to the servlet
Note: <servlet-name>myspringmvc</servlet-name>
The name MYSPRINGMVC will be used in the next step.

1<?xml version="1.0"encoding="UTF-8"?>2<web-app version="2.4"xmlns="HTTP://JAVA.SUN.COM/XML/NS/J2EE"3Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"4xsi:schemalocation="HTTP://JAVA.SUN.COM/XML/NS/J2EE5http//java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">6<servlet>7<servlet-name>myspringmvc</servlet-name>8<servlet-class>9 Org.springframework.web.servlet.DispatcherServletTen</servlet-class> One<load-on-startup>1</load-on-startup> A</servlet> -<servlet-mapping> -<servlet-name>myspringmvc</servlet-name> the<url-pattern>/</url-pattern> -</servlet-mapping> -</web-app>

5. Create the Myspringmvc-servlet.xml,myspringmvc-servlet.xml in the Web-inf directory with the <SERVLET-NAME>MYSPRINGMVC in the previous step</ Servlet-name> correspondence.

This is the Spring MVC mapping configuration file indicates that the access path /index will be given to id=indexcontroller bean processing Id=indexcontroller Bean is configured as class:Indexcontroller

1<?xml version="1.0"encoding="UTF-8"?>2<! DOCTYPE Beans Public"-//SPRING//DTD bean//en" "HTTP://WWW.SPRINGFRAMEWORK.ORG/DTD/SPRING-BEANS.DTD">3<beans>4<bean id="simpleurlhandlermapping"5         class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">6<property name="Mappings">7<props>8<prop key="/index">indexController</prop>9</props>Ten</property> One</bean> A<bean id="Indexcontroller" class="Controller. Indexcontroller"></bean> -</beans>

6. Control class Indexcontroller

The control class Indexcontroller implements the interface controller, providing a method handlerequest processing the request
SPRINGMVC combining models and views with Modelandview objects

Modelandview Mav = new Modelandview ("index.jsp"); Mav.addobject ("Message", "Hello Spring MVC");

Indicates that the view is index.jsp
Model data is a message with the content "Hello Spring MVC"

1 Package Controller;2 3 import javax.servlet.http.HttpServletRequest;4 import Javax.servlet.http.HttpServletResponse;5 6 import Org.springframework.web.servlet.ModelAndView;7 import Org.springframework.web.servlet.mvc.Controller;8 9  Public classIndexcontroller implements Controller {Ten      PublicModelandview HandleRequest (httpservletrequest request, httpservletresponse response) throws Exception { OneModelandview Mav =NewModelandview ("index.jsp"); AMav.addobject ("message","Hello Spring MVC"); -         returnMav; -     } the}

7. View Index.jsp

1<%@ page language="Java"import="java.util.*"pageencoding="iso-8859-1"%>2<%3String Path =Request.getcontextpath ();4String basepath = request.getscheme () +"://"+request.getservername () +":"+request.getserverport () +path+"/";5%>6 7<! DOCTYPE HTML Public"-//w3c//dtd HTML 4.01 transitional//en">89Ten<Basehref="<%=basePath%>"> One      A<title>my JSP'index2.jsp'Starting page</title> -      -<meta http-equiv="pragma"Content="No-cache"> the<meta http-equiv="Cache-control"Content="No-cache"> -<meta http-equiv="Expires"Content="0"> -<meta http-equiv="keywords"Content="Keyword1,keyword2,keyword3"> -<meta http-equiv="Description"Content="This is my page"> +<!-- -<link rel="stylesheet"Type="Text/css"href="Styles.css"> +- A  at -    -<body> - -</body> -

8. Running

Schematic diagram:

1. User access to/index
2. All access according to the configuration in Web. XML will go through the Dispatcherservlet
3. According to the configuration file Springmvc-servlet.xml, Access path/index
Will enter the Indexcontroller class
4. In Indexcontroller, specify jump to page index.jsp and pass message data
5. Display message information in index.jsp

Notes to Hello Spring MVC

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.