(转)SpringMVC学习(一)——SpringMVC介绍与入门
http://blog.csdn.net/yerenyuan_pku/article/details/72231272
SpringMVC介绍
SpringMVC是什么?
SpringMVC和Struts2都属于表现层的框架,它是Spring框架的一部分,我们可以从Spring的整体结构中看得出来: 
SpringMVC处理流程
SpringMVC处理流程如下图所示: 
这个图大致描述了SpringMVC的整个处理流程,乍一看有点晕乎,且待我一步步分析,最后弄个流程图出来就明白了。
SpringMVC入门程序
本系列教程使用的是SpringMVC4.1.3这个版本。下面我就来教大家如何入门SpringMVC这个框架。
现有这样一个需求:使用SpringMVC这个框架实现商品列表的展示。这是我对这个需求的分析:我这里假设请求的url为/itemList.action,由于我想要展示商品列表,所以是并不需要传递参数的,再次是这里仅仅是一个SpringMVC的一个入门小程序,并不会与MyBatis进行整合,也就不会从数据库表里面查询商品列表信息,故查询商品列表数据也仅仅只是一些静态数据。下面正式开始SpringMVC的入门小程序。
SpringMVC入门程序的开发步骤
【第一步】,创建一个javaweb工程,例如springmvc-first。
【第二步】,导入SpringMVC独立运行的jar包,如下: 
【第三步】,创建一个jsp页面——itemList.jsp,内容如下:
<%@ 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 }/item/queryitem.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<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>
并把该jsp页面复制到工程的/WEB-INF/jsp目录下。
【第四步】,创建一个Item类,用于描述商品信息,其内容如下:
public class Items {
private int id;
private String name;
private double price;
private Date createtime;
private String detail;
public Items(int id, String name, double price, Date createtime, String detail) {
super();
this.id = id;
this.name = name;
this.price = price;
this.createtime = createtime;
this.detail = detail;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
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;
}
}
并将该类复制到工程src目录下的com.itheima.springmvc.pojo包中。
【第五步】,创建ItemController,ItemController是一个普通的java类,有点类似于Struts2中的Action,且不需要实现任何接口,只需要在类上添加@Controller注解即可。@RequestMapping注解指定请求的url,其中“.action”可以加也可以不加。在ModelAndView对象中,将视图设置为“/WEB-INF/jsp/itemList.jsp”。
@Controller
public class ItemController {
// .action可以省略 (请求的url地址)
@RequestMapping("/itemList.action")
public ModelAndView itemList() {
// 查询商品列表,使用静态数据生成一个商品列表
List<Items> itemList = new ArrayList<Items>();
itemList.add(new Items(1, "imac", 20000, new Date(), "苹果本很贵"));
itemList.add(new Items(2, "imac1", 20000, new Date(), "苹果本很贵"));
itemList.add(new Items(3, "imac2", 20000, new Date(), "苹果本很贵"));
itemList.add(new Items(4, "imac3", 20000, new Date(), "苹果本很贵"));
itemList.add(new Items(5, "imac4", 20000, new Date(), "卧槽,苹果本很贵啦!"));
// 把商品列表传递给jsp
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList", itemList);
// 设置展示数据的视图,即jsp
modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
// 返回结果
return modelAndView;
}
}
最后将ItemController类复制到工程src目录下的com.itheima.springmvc.controller包中。
【第六步】,创建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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.itheima.springmvc.controller"/>
</beans>
上面配置了扫描包(Controller类所在的包),那么它就会扫描这个包下所有带@Controller注解的类,并创建对象放到springmvc容器中。
【第七步】,配置前端控制器。在web.xml中添加DispatcherServlet的配置,即在web.xml文件中添加如下配置:
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 指定springmvc配置文件的路径。如果不指定,默认为:/WEB-INF/${servlet-name}-servlet.xml -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
【第八步】,入门程序测试。在浏览器地址栏中输入url地址——http://localhost:8080/springmvc-first/itemList.action,回车,就能看到如下效果: 
读者如需源码,可点击SpringMVC学习(一)——SpringMVC入门小程序下载!
(转)SpringMVC学习(一)——SpringMVC介绍与入门的更多相关文章
- (转)SpringMVC学习(三)——SpringMVC的配置文件
http://blog.csdn.net/yerenyuan_pku/article/details/72231527 读者阅读过SpringMVC学习(一)——SpringMVC介绍与入门这篇文章后 ...
- (转)SpringMVC学习(六)——SpringMVC高级参数绑定与@RequestMapping注解
http://blog.csdn.net/yerenyuan_pku/article/details/72511749 高级参数绑定 现在进入SpringMVC高级参数绑定的学习,本文所有案例代码的编 ...
- JAVAEE——SpringMVC第一天:介绍、入门程序、架构讲解、SpringMVC整合MyBatis、参数绑定、SpringMVC和Struts2的区别
1. 学习计划 第一天 1.SpringMVC介绍 2.入门程序 3.SpringMVC架构讲解 a) 框架结构 b) 组件说明 4.SpringMVC整合MyBatis 5.参数绑定 a) Sp ...
- (转)SpringMVC学习(五)——SpringMVC的参数绑定
http://blog.csdn.net/yerenyuan_pku/article/details/72511611 SpringMVC中的参数绑定还是蛮重要的,所以单独开一篇文章来讲解.本文所有案 ...
- springMVC学习(3)-springMVC和mybatis整合
一.需求:使用springmvc和mybatis完成商品列表查询. 二.整合思路:springMVC+mybaits的系统架构: 1步):整合dao层 mybatis和spring整合,通过sprin ...
- SpringMVC第一篇【介绍、入门、工作流程、控制器】
什么是SpringMVC? SpringMVC是Spring家族的一员,Spring是将现在开发中流行的组件进行组合而成的一个框架!它用在基于MVC的表现层开发,类似于struts2框架 为什么要使用 ...
- (转)SpringMVC学习(二)——SpringMVC架构及组件
http://blog.csdn.net/yerenyuan_pku/article/details/72231385 相信大家通过前文的学习,已经对SpringMVC这个框架多少有些理解了.还记得上 ...
- SpringMVC学习(二)——SpringMVC架构及组件(及其运行原理)-转载
相信大家通过前文的学习,已经对SpringMVC这个框架多少有些理解了.还记得上一篇文章中SpringMVC的处理流程吗? 这个图大致描述了SpringMVC的整个处理流程,这个流程图还是相对来说比 ...
- SpringMVC 学习 十一 springMVC控制器向jsp或者别的控制器传递参数的四种方法
以后的开发,大部分是发送ajax,因此这四种传递参数的方法,并不太常用.作为了解吧 第一种:使用原生 Servlet 在控制器的响应的方法中添加Servlet中的一些作用域:HttpRequestSe ...
随机推荐
- CF-796B
B. Find The Bone time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- error:: undefined reference to symbol '__glewBufferSubData' 未定义的引用 以及 error: main.o: undefined reference to symbol 'glTexImage2D'
在把DSO移植到QT工程中,出现了 /usr/bin/ld: KeyFrameDisplay.o: undefined reference to symbol '__glewBufferSubData ...
- HDU - 2181 哈密顿绕行世界问题 dfs图的遍历
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- Bootstrap 自适应排列顺序
一.前用 我们在做一些页面的设计时,总会想到自适应的问题.其实 Bootstrap 框架就很好的融合这个问题了.下面是我学习 Bootstrap 的总结. 二.问题来源 我为什么会遇见这个问题,是因为 ...
- jquery冲突的关键字nodeName、nodeValue和nodeType!
原文:http://blog.csdn.net/hdfyq/article/details/52805836 [缘由]在工作流数据库设计的时候, 都节点管理的功能. 结果有2个字段为 NODE_ ...
- 传统JDBC操作数据库
package com.jdbc.example; import java.sql.Connection; import java.sql.Date; import java.sql.DriverMa ...
- 区间质数查询 luoguP1865
原题 https://www.luogu.org/problemnew/show/P1865 本来get到了一个很好的判断素数的方法 O(玄学常数)https://www.luogu.org/blog ...
- 集合框架Collection<E>接口
- 彻底剖析RMI底层源码 、手写轻量级RMI框架
https://blog.csdn.net/Z0157/article/details/82049975 User public class User { private int age; publi ...
- 调用webServer
1,右键引用 2