入门程序
场景:浏览器输入 list.action 跳转到一个商品列表页面
步骤:
1、创建web工程
2、添加jar:springMVC、spring、junit
3、在web.xml中添加一个DispatcherServlet 需要初始化一个springMVC容器
4、添加springmvc.xml
5、创建一个商品pojo
6、创建一个商品的Controller
传统方式:实现一个Controller接口
注解方式:添加一个@Controller标签
7、把Controller配置到springmvc.xml中(传统方式)
开启包扫描(注解方式)
8、创建一个jsp页面
9、tomcat测试
jar包
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<!-- 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<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>
</web-app>
pojo类
package com.it.bean;
import java.util.Date;
public class Items {
private Integer id;
private String name;
private Float price;
private String pic;
private Date createtime;
private String detail;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic == null ? null : pic.trim();
}
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 == null ? null : detail.trim();
}
@Override
public String toString() {
return "Items [id=" + id + ", name=" + name + ", price=" + price
+ ", pic=" + pic + ", createtime=" + createtime + ", detail="
+ detail + "]";
}
}
一。使用接口的方式实现Controller类
package com.it.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.it.bean.Items;
//使用接口实现controller类,一个类只能实现一个方法
//解决:使用注解@Controller
public class ItemsController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 模拟数据
List<Items> list = new ArrayList<Items>();
for (int i = 0; i < 10; i++) {
Items items = new Items();
items.setId(i);
items.setCreatetime(new Date());
items.setName("小米手机"+i);
items.setDetail("国产");
items.setPrice((float) (1000*i));
list.add(items);
}
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList", list); //相当于把数据放到了request域中
// 逻辑视图:就是jsp的路径
modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
return modelAndView;
}
}
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.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<bean name="/list.action" class="com.it.controller.ItemsController" ></bean>
</beans>
二,使用注解的方式实现Controller类
package com.it.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.it.bean.Items;
//注解写controller类
@Controller
public class ItemsController2 {
@RequestMapping("/list")
public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception {
//模拟数据
List<Items> list = new ArrayList<Items>();
for (int i = 0; i < 10; i++) {
Items items = new Items();
items.setId(i);
items.setCreatetime(new Date());
items.setName("华为手机"+i);
items.setDetail("国产");
items.setPrice((float) (1000*i));
list.add(items);
}
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList", list);//相当于把数据放到了request域中
// 逻辑视图:就是jsp的路径
modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
return modelAndView;
}
@RequestMapping("/list2")
public ModelAndView list2(HttpServletRequest request, HttpServletResponse response) throws Exception
{
List<Items> list=new ArrayList<Items>();
for (int i = 0; i < 10; i++) {
Items items = new Items();
items.setId(i);
items.setCreatetime(new Date());
items.setName("苹果手机"+i);
items.setDetail("不怎么样");
items.setPrice((float) (1000*i));
list.add(items);
}
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList", list);
modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
return modelAndView;
}
}
springmvc.xml配置
<!-- 注解包扫描器 -->
<context:component-scan base-package="com.it.controller"></context:component-scan>
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>
项目结构