一个中心,三大组件:前端控制器,处理器映射器,处理器适配器,视图解析器
需要用户自己开发的是handler和view
1.前端控制器
/* 拦截所有的jsp,js,png,css 不建议使用
*.do *.action 以 do,action结尾的拦截 肯定使用 后台
/ 拦截所有(不包括JSP) 强烈建议使用 页面前台
<!-- 配置前端控制器DispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet.class</servlet-class>
<!-- 默认找WEB-INF/[servlet-name]-serlvet.xml 比如:springmvc-serlvet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern> *.action</url-pattern>
</servlet-mapping>
2.handler(相当于controller)
<?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">
<!-- 扫描@Controler @Service -->
<context:component-scan base-package="com.ryan.springMVC"></context:component-scan>
<!-- 处理器映射器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->
<!-- 处理器适配器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->
<!-- 注解驱动 -->
<mvc:annotation-driven/>
<!-- 视图解释器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
导入pojo类
用数据库生成表格
//编写handler代码
package com.ryan.springMVC.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.ryan.springMVC.pojo.Items;
@Controller
public class ItemController {
@RequestMapping(value="/item/itemList.action")
public ModelAndView itemList() {
List<Items> list=new ArrayList<Items>();
list.add(new Items(1,"1 华为荣耀8",2399f,new Date(),"质量好!1"));
list.add(new Items(2,"1 华为荣耀8",2499f,new Date(),"质量好!1"));
list.add(new Items(3,"1 华为荣耀8",2599f,new Date(),"质量好!1"));
list.add(new Items(4,"1 华为荣耀8",2699f,new Date(),"质量好!1"));
list.add(new Items(5,"1 华为荣耀8",2799f,new Date(),"质量好!1"));
list.add(new Items(6,"1 华为荣耀8",2899f,new Date(),"质量好!1"));
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("itemList", list);
modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
return modelAndView;
}
}
3.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>