3. Hello SpringMVC
(1) 新建 maven 模块
springmvc-02-hellomvc
(2) 确认依赖的导入
(3) 配置 web.xml
<!--web/WEB-INF/web.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--1.注册DispatcherServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--关联一个springmvc的配置文件:【servlet-name】-servlet.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--启动级别-1:与服务器同时启动-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--/ 匹配所有的请求;(不包括.jsp)-->
<!--/* 匹配所有的请求;(包括.jsp)-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
(4) 编写 springmvc 配置文件
<!--resources/springmvc-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.xsd">
</beans>
(5) 添加处理映射器
<!--resources/springmvc-servlet.xml-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
(6) 添加处理适配器
<!--resources/springmvc-servlet.xml-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
(7) 添加视图解析器
<!--resources/springmvc-servlet.xml-->
<!--视图解析器:DispatcherServlet给他的ModelAndView-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
(8) 编写 Controller
// com/why/controller/HelloController
package com.why.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//注意:这里我们先导入Controller接口
public class HelloController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
//ModelAndView 模型和视图
ModelAndView mv = new ModelAndView();
//封装对象,放在ModelAndView中。Model
mv.addObject("msg","HelloSpringMVC!");
//封装要跳转的视图,放在ModelAndView中
mv.setViewName("hello"); //: /WEB-INF/jsp/hello.jsp
return mv;
}
}
(9) 注册 Controller
<!--resources/springmvc-servlet.xml-->
<!--Handler-->
<bean id="/hello" class="com.why.controller.HelloController"/>
(10) 跳转 jsp 页面
<!--web/WEB-INF/jsp/hello.jsp-->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Kuangshen</title>
</head>
<body>
${msg}
</body>
</html>
(11) 问题排查
-
访问出现404,排查步骤:
(1) 查看控制台输出,看一下是不是缺少了什么jar包。
(2) 如果jar包存在,显示无法输出,就在IDEA的项目发布中,添加lib依赖!
(3) 重启Tomcat 即可解决!