Simple springmvc the demo
1, related to the introduction of the jar package springmvc
2, disposed in web.xml DispatcherServlet
<servlet> <servlet-name>springMVCConfig-servlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVCConfig-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springMVCConfig-servlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
3, configuration springmvc profile
Springmvc.xml create a file, and configure
<? XML Version = "1.0" encoding = "UTF-. 8" ?> < Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: the xsi = "http://www.w3.org / 2001 / XMLSchema-instance " xsi: schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd " > <-! Configuring a HandlerMapping, if they are not configured HandlerMapping, there is a default HandlerMapping -> < bean class = "org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" > </ bean > <!- Register your own Controller class ->
<! - to find the name of the controller class corresponding Controller class, Controller class in addition to the first letter capitalized, all other lowercase ->
< bean class = "com.zhiyou100.zjc.controller.ExampleController" > </ bean > <! - configure trying parser -> < bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > < Property name = "prefix" value = "/ the WEB-INF / View /" > </ Property > < Property name = "suffix" value =. "JSP" > </ Property > </bean> </beans>
4, writing Controller
public class ExampleController extends AbstractController{ @Override protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { ModelAndView mav = new ModelAndView(); mav.setViewName("index"); mav.addObject("info", "Hello SpringMVC"); return mav; } }
5, create a jsp file to see, receive information
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> ${info } </body> </html>
6, pages output
输入http://localhost:8080/day090202/exampleController.do