20. MVC's web Framework (Spring MVC)

Source: Internet
Author: User

One, Spring MVC

1. Also import the corresponding jar package, and import the jar package into the project's Webcontent/web-inf/lib directory.

2. Web. xml file

Create a Web. xml file under the Web-inf directory to configure the Spring MVC Portal Dispatcherservlet (similar to the STRUCTS2 configuration Structsprepareandexecutefilter filter), The goal is to submit all requests to dispatcherservlet for processing.

Note: The configuration of the <servlet-name>springmvc</servlet-name>servlet name, Convenient configuration Springmvc servlet.xml name correspondence, Springmvc-servelt.xml.

<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "2.4 xmlns="http://java.sun.com/xml/ns/j2ee "Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">        <servlet>            <Servlet-name>Springmvc</Servlet-name>            <!--Configuring the Springmvc dispatcher -            <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>            <Load-on-startup>1</Load-on-startup>        </servlet>        <servlet-mapping>            <Servlet-name>Springmvc</Servlet-name>            <Url-pattern>/</Url-pattern>        </servlet-mapping></Web-app>

3. Create Springmvc-servlet.xml

Create Springmvc-servlet.xml in the Web-inf directory with the file name of Web. xml

The name of the label <servlet-name>springmvc</servlet-name> configuration corresponds.

Springmvc-servlet.xml is the Spring MVC mapping configuration file, and when the path to the access is/index, it will send the request to Id=indexcontroller's bean to handle the request, and this class is Indexcontroller

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Beans Public "-/spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd ">    <Beans>        <BeanID= "Simpleurlhandlermapping"class= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">            < Propertyname= "Mappings">                <Props>                    <propKey= "/index">Indexcontroller</prop>                </Props>            </ Property>        </Bean>        <BeanID= "Indexcontroller"class= "Com.demo.controller.IndexController"></Bean>    </Beans>

4. Create the Controller class Indexcontroller

The controller Indexcontroller implements the interface controller, providing a method handlerequest to process the request. Spring MVC then combines the model and view views with the Modelandview object.

 PackageCom.demo.controller;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.Controller; Public classIndexcontrollerImplementscontroller{pubic Modelandview handlerequest (httpserveltrequest request,httpservletresponse response)throwsexception{Modelandview Mav=NewModelandview ("index.jsp"); Mav.addobject ("Name", "I Love you!");//model data is product, content is I love you        returnMav; }}

5. Create index.jsp

Create a index.jsp in the WebContent directory and get the contents of name through the EL expression

<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"iselignored="false"%><H1>${name}</H1>

6, Principle Summary:

6.1. When the user accesses/index

6.2, according to the configuration in Web. XML, all access will be dispatcherservlet processed and then entered into the SPRINGMVC process.

6.3, according to the configuration file Springmvc-servlet.xml, access the path/index, and then enter into the Indexcontroller controller class.

6.4, specify the jump page to index.jsp in the Indexcontroller controller. and pass the name of the data

6.5. Display the data of name in index.jsp.

20. MVC's web Framework (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.