目录
一、SpringMVC 的简介
SpringMVC 主要是 Spring Frameword 中的一个模块内容。负责前端 MVC 模块的功能,在使用的时候跟 Spring 是无缝连接的。没有额外的其他配置,因为是 Spring 自家的东西,用起来体验比较好。
另外,注意,MVC 属于前端,指的是 Model View Controller 模型、视图、控制器。服务端是没有这个架构的。
二、SpringMVC 的使用
1.新建动态 Web 工程。
2.导入 Spring 中的 jar 包文件。
3.在 web.xml 文件中,配置 DispatcherServlet 前端控制器
<!-- 配置 DispatcherServlet 前端控制器 -->
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 在此处调用 springmvc.xml 文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 在 tomcat 服务器启动的时候,最先加载当前的 DispatcherServlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<!-- 对于所有符合(*.action)这种格式的资源的请求都由指定的servlet处理 -->
<servlet-name>hello</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
4.在 config路径下,配置 springmvc.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.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">
<!-- 配置注解扫描:
主要是扫 Controller 类中的 @Controller 和 @RequestMapping 注解
-->
<context:component-scan base-package="com.neuedu.controller"/>
</beans>
5.编辑 web.xml 文件,在 DispatcherServlet 中通过 <init-param> 标签,初始化 springMVC。(参考 3 中配置)
6.提供三个组件:Controller 类、JavaBean 类和 JSP 页面。
1)Controller:主要是用于接收请求,调用业务逻辑和返回视图。
@Controller
public class ItemsController {
@RequestMapping("/getlist")
public ModelAndView itemsList() throws Exception {
// 1. 准备一个集合
List<Items> itemsList = new ArrayList<>();
// 2. 创建多个数据,然后加入到集合中
Items items = new Items();
items.setName("旺仔牛奶");
items.setPrice(5.5);
items.setNote("喝了会长高的哦...");
Items items2 = new Items();
items2.setName("娃哈哈AD钙奶");
items2.setPrice(5.5);
items2.setNote("喝了会长高的哦...");
Items items3 = new Items();
items3.setName("越南红牛");
items3.setPrice(5.5);
items3.setNote("喝了会长高的哦...");
itemsList.add(items);
itemsList.add(items2);
itemsList.add(items3);
// 3. 创建 ModelAndView
ModelAndView md = new ModelAndView();
// 服务端返回到页面的数据格式:key-value 形式
md.addObject("itemsList", itemsList);
md.setViewName("/WEB-INF/jsp/itemsList.jsp");
return md;
}
}
2)JavaBean:用于封装数据,可以返回页面进行渲染。
public class Items implements Serializable {
private String name;
private double price;
private String note;
// 添加 get 和 set 方法
}
3)JSP:展示页面,可用于提交请求,然后接收响应数据进行渲染展示。
因为需要使用到 JSTL 核心标签库,所以,我们需要先引入标签库。
百度下就有,记得引入这个包。
<%-- 添加 JSTL 核心标准库 --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
页面详细内容如下:
<body>
<table wdith="100%" border="1">
<tr>
<td>名字</td>
<td>价格</td>
<td>描述</td>
</tr>
<c:forEach items="${itemsList }" var="item">
<tr>
<td>${item.name }</td>
<td>${item.price }</td>
<td>${item.note }</td>
</tr>
</c:forEach>
</table>
</body>
4)访问
http://localhost:8080/Springmvc01/getlist.action
因为拦截器那里配置了 *.action 所以需要在链接后面加上 .action 才能访问到控制器里面的东西。