Web.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
- <display-name></display-name>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <!-- 配置Spring MVC -->
- <servlet>
- <servlet-name>springmvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/mvc-servlet.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>springmvc</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- </web-app>
注意:
1、错误提示:org.springframework.web.servlet.DispatcherServlet noHandlerFound
警告: No mapping found for HTTP request with URI [/myspring/WEB-INF/jsp/hello.jsp] in DispatcherServlet with name 'springmvc'
- <servlet-mapping>
- <servlet-name>springmvc</servlet-name>
- <url-pattern>/*</url-pattern> <!---或者<url-pattern>/*.do</url-pattern>-->
- </servlet-mapping>
这是一种错误的写法,可以改为:
- <servlet-mapping>
- <servlet-name>springmvc</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
2、默认情况:
servlet-name一般与项目名称一致;若不写:
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/mvc-servlet.xml</param-value>
- </init-param>
mvc-servlet.xml:
- <?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-3.0.xsd">
- <!-- HandlerMapping -->
- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
- <property name="mappings">
- <props>
- <prop key="hello.do">helloController</prop>
- </props>
- </property>
- </bean>
- <!-- ViewResolver -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
- <property name="prefix" value="/WEB-INF/jsp/"/>
- <property name="suffix" value=".jsp"/>
- </bean>
- <!-- 处理器 -->
- <bean id="helloController" name="/hello" class="com.mzsx.web.controller.HelloWorldController"/>
- </beans>
处理器:HelloWorldController.java
- package com.mzsx.web.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 HelloWorldController implements Controller {
- @Override
- public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
- //1、收集参数
- //2、绑定参数到命令对象
- //3、调用业务对象
- //4、选择下一个页面
- ModelAndView mv = new ModelAndView();
- //添加模型数据 可以是任意的POJO对象
- mv.addObject("message", "Hello World!");
- //设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面
- mv.setViewName("hello");
- System.out.println("执行Controller---------");
- return mv;
- }
- }
JSP: /WEB-INF/jsp/hello.jsp
- <body>
- 取得数据:
- ${message }
- </body>