1.MVC是一种架构模型,本身没有什么功能,只是让我们的项目结构更加合理,流程控制更加清晰。
MVC 是 Model、View 和 Controller 的缩写,分别代表 Web 应用程序中的 3 种职责。
- 模型:用于存储数据以及处理用户请求的业务逻辑。
- 视图:向控制器提交数据,显示模型中的数据。
- 控制器:根据视图提出的请求判断将请求和数据交给哪个模型处理,将处理后的有关结果交给哪个视图更新显示。
基于 Servlet 的 MVC 模式的具体实现如下。
- 模型:一个或多个 JavaBean 对象,用于存储数据(实体模型,由 JavaBean 类创建)和处理业务逻辑(业务模型,由一般的 Java 类创建)。
- 视图:一个或多个 JSP 页面,向控制器提交数据和为模型提供数据显示,JSP 页面主要使用 HTML 标记和 JavaBean 标记来显示数据。
- 控制器:一个或多个 Servlet 对象,根据视图提交的请求进行控制,即将请求转发给处理业务逻辑的 JavaBean,并将处理结果存放到实体模型 JavaBean 中,输出给视图显示。
2.SpringMVC是Spring架构中的一部分:Spring Web MVC是一种基于Java的,实现了Web MVC设计模式的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,采用了松散耦合可插拔组件结构,比其它MVC框架更具扩展性和灵活性。
可以让我们实现:
- 进行更简洁的web层开发
- 天生与Spring框架集成(如IOC容器、AOP等)
- 提供更强大的约定大雨配置的契约式编程支持
- 支持灵活的URL到页面控制器的映射
- 非常容易与其他视图技术集成,如Velocity、FreeMarker等等,因为数据模型不放在特定的API里,而是放在一个Model里(Map数据结构实现,因此很容易被其他框架使用);
- 非常灵活的数据验证、格式化和数据绑定机制,能使用任何对象进行数据绑定,不必实现特定框架的API
- 支持Restful风格
SpringMVC在架构设计、扩展性、灵活性方面已经全面超越了Struts、WebWork等MVC框架。
3.SpringMVC处理流程如图:
Spring MVC 的工作流程如下:
- 客户端请求提交到 DispatcherServlet。
- 由 DispatcherServlet 控制器寻找一个或多个 HandlerMapping,找到处理请求的 Controller。
- DispatcherServlet 将请求提交到 Controller。
- Controller 调用业务逻辑处理后返回 ModelAndView。
- DispatcherServlet 寻找一个或多个 ViewResolver 视图解析器,找到 ModelAndView 指定的视图。
- 视图负责将结果显示到客户端。
4.入门程序:使用浏览器显示商品列表
//商品
package com.feng.pojo;
import java.util.Date;
public class Item {
private Integer id;
private String name;
private Double price;
private Date createtime;
private String detail;
public Item(String name, Double price, Date createtime, String detail) {
this.name = name;
this.price = price;
this.createtime = createtime;
this.detail = detail;
}
public String getName() {
return name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询商品列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/queryItem.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td>商品id<input type="text" name="item.id"/> </td>
<td>商品名称<input type="text" name="item.name"/> </td>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
<td>商品名称</td>
<td>商品价格</td>
<td>生产日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemList }" var="item">
<tr>
<td>${item.name }</td>
<td>${item.price }</td>
<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td>${item.detail }</td>
<td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
package com.feng.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.feng.pojo.Item;
@Controller
public class ItemController {
//编写一个方法,处理显示商品列表
//ModelAndView 容器,放置要返回的数据和页面
//使用注解的方式处理显示商品列表的请求
@RequestMapping("/itemList.action")
public ModelAndView list(){
//调用service--》dao--》返回数据
List<Item> items = new ArrayList<>();
items.add(new Item("苹果", 8.8d, new Date(), "圣诞节"));
items.add(new Item("饺子", 18.8d, new Date(), "冬至"));
items.add(new Item("手机", 1888d, new Date(), "元旦"));
ModelAndView modelAndView = new ModelAndView();
//加入返回数据
modelAndView.addObject("itemList", items);
//request.setAttribute("itemList", items)
//跳转的页面
modelAndView.setViewName("itemList");
//request.getRequestDispatcher("/WEB-INF/jsp/itemList.jsp").forword("req","res");
//把modelAndView返回给spring mvc框架去处理
return modelAndView;
}
}
//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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
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-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<!--配置controller扫描包-->
<context:component-scan base-package="com.feng.controller"></context:component-scan>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置返回页面的前缀 (路径)-->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 配置返回页面的后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
//web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" version="4.0">
<display-name>SpringMVC1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置SpringMVC前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定SpringMVC配置文件 -->
<!-- SpringMVC的配置文件的默认路径是/WEB-INF/${servlet-name}-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 设置所有以action结尾的请求进入SpringMVC jsp html -->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
在浏览器输入http://localhost:8080/SpringMVC1/itemList.action
注:这原本是7-31该完成的,但是昨天为的配置文件一直不对,找不到xml文件,8-1才解决。要小心配置文件。