1, spring jar package download
Enter http://repo.springsource.org/libs-release-local/, then Click org / -> springframework -> spring, you can download the compressed spring framework as needed.
2, common-logging jar package
spring core container-dependent common-logging jar package, Download http://commons.apache.org, Click Releases -> Logging, select commons-logging-1.2-bin.zip download.
3、springMVC的DispatcherServlet
DispatcherServlet is a Servlet, inherited from HttpServlet. It is springMVC front controller, for intercepting the request, interact with the client.
4, springMVC entry case
Environment to build: build dynamic web project, import the jar package springMVC need to configure the front controller springMVC in web.xml, write springMVC.xml core configuration file, write controller.
front-end controller is configured web.xml springMVC
1 <!-- 配置springMVC前段控制器 --> 2 <servlet> 3 <servlet-name>springMVC</servlet-name> 4 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 5 <init-param> 6 <param-name>contextConfigLocation</param-name> 7 <param-value>/WEB-INF/springMVC.xml</param-value> 8 </init-param> 9 <!-- Tomcat启动时加载该servlet --> 10 <load-on-startup>1</load-on-startup> 11 </servlet> 12 <servlet-mapping> 13 <servlet-name>springMVC</servlet-name> 14 <url-pattern>/</url-pattern> 15 </servlet-mapping>
Core configuration file springMVC.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc . 11 http://www.springframework.org/schema/mvc/spring- mvc.xsd " > 12 is 13 is ! <- configuration @Controller annotation scan -> 14 < context: scan-Component Base-Package =" com.alphajuns.controller " > </ context: Component-scan > 15 <-! - configuring the processor mapper -> 16 < the bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" > </bean> 17 <! - Configuration Processor Adapter -> 18 is < the bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" > </ the bean > . 19 <-! Configuration view resolver -> 20 is < the bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > </ the bean > 21 is </ Beans >
controller class
1 package com.alphajuns.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.servlet.ModelAndView; 6 7 @Controller 8 public class HelloController { 9 10 @RequestMapping(value="/hello") 11 public ModelAndView hello() { 12 // 创建模型和视图对象 13 ModelAndView mv = new ModelAndView(); 14 // add the model 15 mv.addObject ( "Message", "SPRINGMVC the Hello!" ); 16 // set the view . 17 mv.setViewName ( "/ the WEB-INF / Content / the welcome.jsp" ); 18 is // Returns the model and view . 19 return Music Videos; 20 is } 21 is }
If the value of the main page is available el expressions, such as welcome.jsp page
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 </head> <9 body> 10 ${requestScope.message } 11 </body> 12 </html>
Successful visit
If you do not specify a path to load the configuration file, by default, the configuration file will be loaded springMVC to find the corresponding [servlet-name] -servlet.xml folder WEB-INF.
SpringMVC configuration file parsing WebApplicationContext creates a container object based on the profile, WebApplicationContext inherits from ApplicationContext container.
springMVC recommended that all view pages stored in the WEB-INF folder, so you can view protection, to avoid sending requests directly to view the page.