第一个SpringMVC-HelloSpringMVC
一、配置版
1.新建一个Model,添加Web支持
2.导入SpringMVC的依赖
3.配置web.xml,注册DispatcherServlet
<?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">
<!--配置DispatchServlet(调度Servlet):这个是SpringMVC的核心:请求分发器,或者前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--DispatcherServlet需要绑定Spring的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--设置启动级别为一,即和服务器一同启动-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--
在SpringMVC中, / 和 /* 的区别
/ 只匹配所有的请求,不会去匹配jsp页面
/* 匹配所有的请求,包括jsp页面
-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4.编写SpringMVC的配置文件,名称: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.添加处理器映射器
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
6.添加处理器适配器
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
7.添加视图解析器
<!--视图解析器-->
<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,这一步要么实现Controller接口,要么增加注解。需要返回一个ModelAndView,装数据,封视图
9.将自己的类交给SpringIoC容器,注册Bean
<!--handle-->
<bean id="hello" class="com.xiang.controller.HelloController"/>
10.写要跳转的jsp页面,显示ModelAndView存放的数据,以及正常页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
11.配置TomCat,启动页面
【可能遇到的问题】
- 查看控制台输出,看是否缺少jar包
- 如果不缺少jar包,显示无法输出,就在IDEA的项目发布中,添加lib依赖
- 重启TomCat
二、注解版
上面的配置版是用来解释SpringMVC实现的原理,而用注解实现才是SpringMVC的精髓
1、新建一个Model,添加Web支持,建立包com.xx.controller
2、配置资源过滤问题
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
3、引入依赖
有Spring框架核心库、SpringMVC、servlet、JSTL
4、配置web.xml
注意
- web.xml的版本要最新版(这里是4.0的)
- 注册DispatcherServlet
- 关联SpringMVC的配置文件
- 设置该配置的启动级别为1
- 设置映射路径为(/)
- 【易忽略】在web项目中打包,即建立lib,导入各个包
<?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">
<!--配置DispatchServlet(调度Servlet):这个是SpringMVC的核心:请求分发器,或者前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--DispatcherServlet需要绑定Spring的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.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>
5、添加SpringMVC的配置文件
注意此处的配置和注解版的配置很不一样
- 让IoC的注解生效
- 静态资源过滤
- MVC注解驱动
- 配置视图解析器
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--自动扫描包,让指定包下的注解生效,由IoC容器统一管理-->
<context:component-scan base-package="com.xiang.controller"/>
<!--让SpringMVC不处理静态资源,这样可以引用一些 .css .js .html,让这些东西不走配置的视图解析器-->
<mvc:default-servlet-handler />
<!--配置注解驱动-->
<mvc:annotation-driven />
<!--配置视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
在视图解析器中我们吧所有的视图都存放在/WEB-INF/目录下,这样可以保护视图安全,因为这个目录下的文件,客户端不能直接访问
6、创建Controller
编写一个controller控制类helloController.java
package com.xiang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
//真实访问地址:项目名/HelloController/hello
@RequestMapping("/hello")
public String hello(Model model){
//封装数据,向模型中添加属性和值,可以在JSP页面取出并渲染
model.addAttribute("msg","hello,this is SpringMVCAnnotation!");
/*这里返回的字符串就是视图的名字
并且该返回会被视图解析器处理
返回的值会被视图解析器加上首尾成为一个可以被访问的jsp*/
return "hello";// web-inf/jsp/hello.jsp
}
}
7、创建视图层
在WEB-INF/jsp目录中创建hello.jsp,视图可以直接取出并展示从Controller带回的信息;可以提供EL表达式取出Model中存放的值或者对象
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
8、配置TomCat运行
小结
实现的步骤
- 新建web项目
- 导包
- 编写web.xml,注册DispatcherServlet
- 编写SpringMVC配置文件
- 创建对应的控制类
- 完善前端视图和controller之间的对应
- 测试运行
SpringMVC的三大件:处理器映射器、处理器适配器、视图解析器
通常,我么只需要手动配置视图解析器,而处理器映射器和处理器适配器只需要开启注解驱动即可