一個(gè)中心邓厕,三大組件:前端控制器逝嚎,處理器映射器,處理器適配器详恼,視圖解析器
需要用戶(hù)自己開(kāi)發(fā)的是handler和view
image.png
1.前端控制器
/* 攔截所有的jsp补君,js,png昧互,css 不建議使用
*.do *.action 以 do挽铁,action結(jié)尾的攔截 肯定使用 后臺(tái)
/ 攔截所有(不包括JSP) 強(qiáng)烈建議使用 頁(yè)面前臺(tái)
<!-- 配置前端控制器DispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet.class</servlet-class>
<!-- 默認(rèn)找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(相當(dāng)于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"/> -->
<!-- 注解驅(qū)動(dòng) -->
<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>
導(dǎo)入pojo類(lèi)
用數(shù)據(jù)庫(kù)生成表格
//編寫(xiě)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(),"質(zhì)量好!1"));
list.add(new Items(2,"1 華為榮耀8",2499f,new Date(),"質(zhì)量好敞掘!1"));
list.add(new Items(3,"1 華為榮耀8",2599f,new Date(),"質(zhì)量好叽掘!1"));
list.add(new Items(4,"1 華為榮耀8",2699f,new Date(),"質(zhì)量好!1"));
list.add(new Items(5,"1 華為榮耀8",2799f,new Date(),"質(zhì)量好玖雁!1"));
list.add(new Items(6,"1 華為榮耀8",2899f,new Date(),"質(zhì)量好更扁!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>查詢(xún)商品列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/item/queryitem.action" method="post">
查詢(xún)條件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查詢(xún)"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
<td>商品名稱(chēng)</td>
<td>商品價(jià)格</td>
<td>生產(chǎn)日期</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>