springmvc--入门程序编写

(一)springMVC由一个中心(DispatcherServlet:前端控制器)和三个组件(HandleMapping处理器映射器,HandlerAdapter处理器适配器,ViewResolver视图解析器)组成,

我们需要书写视图(jsp)处理器(Controller)两个文件

(二)springmvc处理请求的流程:(中心只负责拦截请求,然后调用其他三个组件)

  1. 用户发送请求至前端控制器DispatcherServlet(http://localhost:8080/springmvc01/item/itemList.action
  2. DispatcherServlet收到请求调用HandlerMapping处理器映射器。
  3. 处理器映射器根据请求url找到具体的处理器,生成处理器对象及处理器拦截器(如果有则生成)一并返回给DispatcherServlet。(@RequestMapping(value="/item/itemList.action")
  4. DispatcherServlet通过HandlerAdapter处理器适配器调用处理器
  5. 执行处理器(Controller,也叫后端控制器)。(public ModelAndView itemList(){}
  6. Controller执行完成返回ModelAndView(return mav;
  7. HandlerAdapter将controller执行结果ModelAndView返回给DispatcherServlet
  8. DispatcherServlet将ModelAndView传给ViewReslover视图解析器
  9. ViewReslover解析后返回具体View
  10. DispatcherServlet对View进行渲染视图(即将模型数据填充至视图中)。
  11. DispatcherServlet响应用户

(一)导包

(二)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.createtimes}" 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>

(三)POJO类

package cn.shu.springmvc.pojo;

import java.sql.Date;

public class Items {
	
	private Integer id;
	private String name;
	private Float price;
	private String pic;
	private java.util.Date createtimes;
	private String detail;
	
	//构造器
	
	
	
	public Items(Integer id, String name, Float price, java.util.Date date, String detail) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
		this.createtimes = date;
		this.detail = detail;
	}
	/*setter and  getter*/
	@Override
	public String toString() {
		return "Items [id=" + id + ", name=" + name + ", price=" + price + ", pic=" + pic + ", createtimes="
				+ createtimes + ", detail=" + detail + "]";
	}
	

}

(四)处理器层的书写

package cn.shu.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 cn.shu.springmvc.pojo.Items;

//处理器
@Controller
public class ItemController {
	
	
	//RequestMapping 中放的是请求的URL,和用户请求的URL进行匹配
	@RequestMapping(value="/item/itemList.action")
	public ModelAndView itemList(){
		
		//创建页面需要显示的商品数据
		List<Items> list = new ArrayList<>();
		list.add(new Items(1,"小米3c",2199f,new Date(),"质量好1"));
		list.add(new Items(2,"华为p7",2199f,new Date(),"质量好2"));
		list.add(new Items(3,"ViVo",2199f,new Date(),"质量好3"));
		list.add(new Items(4,"oneplus",2199f,new Date(),"质量好4"));
		list.add(new Items(5,"坚果Pro",2199f,new Date(),"质量好5"));
		list.add(new Items(6,"iPhone",2199f,new Date(),"质量好6"));
		
		ModelAndView mav = new ModelAndView();
		//封装数据
		//request.setAttribute()...
		//ModelAndView org.springframework.web.servlet.ModelAndView.addObject(String attributeName, Object attributeValue)
		mav.addObject("itemList", list);
		mav.setViewName("/WEB-INF/jsp/itemList.jsp");
		return mav;
	}
	
}

(五)注解扫描配置

<?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">
        
   <!-- 扫描@Controller @Service等注解-->    
     <context:component-scan base-package="cn.shu"></context:component-scan>   
        
 </beans>

(六)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">
  <display-name>springmvc01</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  
 <!-- 前端控制器 -->
 <servlet>
 	<servlet-name>springmvc</servlet-name>
 	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 	<!--上下文配置文件, 默认找/WEB-INF/[servlet的名称]-Servlet -->
 	<init-param>
 		<param-name>contextConfigLocation</param-name>
 		<param-value>classpath:springmvc.xml</param-value>
 	</init-param>
 </servlet>
 <servlet-mapping>
 	<servlet-name>springmvc</servlet-name>
 	<!-- 
 	1. /*  拦截所有jsp js png .css 真的全拦截,建议不使用
 	2.  *.action   *.do  拦截以do  action结尾的请求
 	3.  /   拦截所有(不包括jsp)(包含.js  .png .css)强烈建议使用,对静态资源放行
 	 拦截到Controller层-->
 	<url-pattern>*.action</url-pattern>
 </servlet-mapping>
</web-app>

(七)测试结果

(8)问题

在给POJO类的属性设置类型时遇到有关private java.util.Date createtimes;问题

import java.util.Date;与import java.sql.Date;的区别

猜你喜欢

转载自blog.csdn.net/JayBillions/article/details/81592108