The first Spring MVC application

1. In Eclipse, create a name for the chapter11 Web project, add JAR package running Spring MVC procedures required in the lib directory of the project, and published to the classpath.

(Tomcat-juli.jar is found in the bin folder under the tomcat)

         Spring project added four core JAR package, commons-logging of JAR JAR and two web-related (libs directory can be found in the Spring unpacked folder), these two web-related JAR package is the Spring MVC framework required JAR package.

            2. in web.xml configure the front of the controller DispatcherServlet Spring MVC.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 id="WebApp_ID" version="2.5">

    <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:springmvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
         </servlet>
         <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
 </web-app>

3. src directory, create a com.itheima.controller package, and creates a package in FirstController controller class, the class need to implement the Controller interface, edited as shown below.

package com.itheima.controller;

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 FirstController implements Controller {//实现Controller接口

	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();//创建ModelAndView对象
		mav.addObject("msg","This is the first Spring MVC programme!");//添加返回信息
		mav.setViewName("/WEB-INF/jsp/first.jsp");//设置逻辑视图名
		return mav;
	}

}

4. src directory, create a profile springmvc-config.xml, and configure the controller information in the file.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
   <bean name = "/firstController" class = "com.itheima.controller.FirstController"/>
   <!-- 处理器映射器,将处理器Handle的name作为url进行查找 -->
   <bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
   <!-- 处理器适配器,配置对处理器中handleRequest()方法的调用-->
   <bean class = "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
   <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver"/>
   </beans>

5. In the WEB-INF directory, jsp create a folder, and create a page file in the folder first.jsp, msg obtain information using an EL expression in this page, as shown below.

     <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Good</title>
  </head>
  <body>
        ${msg}
  </body>
     </html> 


Run Process Display:

 

 

   6. chapter11 publish a project to Tomcat, and start the Tomcat server. Access browser address http: // localhost: 8080 / chapter11 / firstController, which show the following effects:

         Can be seen from the figure, the browser has shown the character string information model objects. This explains the first Spring MVC program successful.

Published 376 original articles · won praise 172 · views 90000 +

Guess you like

Origin blog.csdn.net/Eider1998/article/details/104163638