Spring web mvc和Struts2都属于表现层的框架,它是Spring框架的一部分,我们可以从Spring的整体结构中看得出来,如下图:
需求:使用浏览器显示商品列表
<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="cn.zdfy.springmvc.controller">context:component-scan>
beans>
<servlet>
<servlet-name>springmvc-firstservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvc-firstservlet-name>
<url-pattern>*.actionurl-pattern>
servlet-mapping>
<%@ 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"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询商品列表title>
head>
<body>
<form action="${pageContext.request.contextPath }/itemList.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>
package cn.zdfy.springmvc.pojo;
import java.util.Date;
public class Item {
private int id;
private String name;
private double price;
private Date createtime;
private String detail;
public Item(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;
}
@Override
public String toString() {
return "Item [id=" + id + ", name=" + name + ", price=" + price + ", createtime=" + createtime + ", detail="
+ detail + "]";
}
}
ItemController是一个普通的java类,不需要实现任何接口。
需要在类上添加@Controller注解,把Controller交由Spring管理
在方法上面添加@RequestMapping注解,里面指定请求的url。其中“.action”可以加也可以不加。
@Controller
public class ItemController {
// @RequestMapping:里面放的是请求的url,和用户请求的url进行匹配
// action可以写也可以不写
@RequestMapping("/itemList.action")
public ModelAndView queryItemList() {
List<Item> list = new ArrayList<>();
list.add(new Item(1, "1 小米 1", 1999, new Date(), "性能怪兽! 1"));
list.add(new Item(2, "2 小米 2", 1999, new Date(), "性能怪兽! 2"));
list.add(new Item(3, "3 小米 3", 1999, new Date(), "性能怪兽! 3"));
list.add(new Item(4, "4 小米 4", 1999, new Date(), "性能怪兽! 4"));
list.add(new Item(5, "5 小米 5", 1999, new Date(), "性能怪兽! 5"));
// 创建ModelAndView,用来存放数据和视图
ModelAndView modelAndView = new ModelAndView();
// 设置数据到模型中
modelAndView.addObject("itemList", list);
// 设置视图jsp,需要设置视图的物理地址
modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
return modelAndView;
}
}