2.springmvc_demo(传智播客)
需求:查询商品列表(静态数据模拟)
一.环境搭建
1.创建maven工程
2.添加相关依赖(参考github上项目的pom.xml)
3.项目工程结构
二.开发步骤
1.web.xml
完成springmvc配置文件的加载以及将所有请求交由springmvc解析。
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载springmvc配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<!-- 配置将所有请求交给springmvc来处理 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2.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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 1.配置处理器 -->
<!-- 可以扫描controller、service、... 这里让扫描controller,指定controller的包 -->
<context:component-scan base-package="com.steven.ssm.controller"></context:component-scan>
<!-- 2.配置处理器映射起和处理器适配器 -->
<!-- 使用mvc:annotation-driven配置注解映射器和注解适配器,其默认加载很多的参数绑定方法,比如json转换解析器等等-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 3.配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置jsp路径的前缀 -->
<property name="prefix" value="/WEB-INF/views/"/>
<!-- 配置jsp路径的后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
3.处理器(控制器)的开发
@Controller
@RequestMapping("/items")
public class ItemsController {
//商品查询列表
@RequestMapping("/queryItems")
public ModelAndView queryItems() throws Exception{
//调用service查找数据库,查询商品列表,这里使用静态数据模拟
List<Items> itemsList = new ArrayList<Items>();
Items items_1 = new Items();
items_1.setId(1);
items_1.setName("联想笔记本");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad T430 c3 联想笔记本电脑!");
Items items_2 = new Items();
items_2.setId(2);
items_2.setName("苹果手机");
items_2.setPrice(5000f);
items_2.setDetail("iphone6苹果手机!");
itemsList.add(items_1);
itemsList.add(items_2);
//返回ModelAndView
ModelAndView modelAndView = new ModelAndView();
//相当于request的setAttribute方法,在jsp页面中通过itemsList取数据
modelAndView.addObject("itemsList",itemsList);
//指定视图
modelAndView.setViewName("items/queryItems");
return modelAndView;
}
}
4.视图的开发
<%@ 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="" method="post">
商品列表:
<table width="100%" border=1>
<tr>
<td>商品编号</td>
<td>商品名称</td>
<td>商品价格</td>
<td>商品描述</td>
</tr>
<c:forEach items="${itemsList }" var="item">
<tr>
<td>${item.id}</td>
<td>${item.name}</td>
<td>${item.price }</td>
<td>${item.detail }</td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
三.入门总结
1.前端控制器配置
- 第一种:*.action,访问以.action结尾由DispatcherServlet进行解析。
- 第二种:/,所以访问的地址都由DispatcherServlet进行解析,对于静态文件的解析需要配置不让DispatcherServlet进行解析 ,使用此种方式可以实现 RESTful风格的url。
<mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>
2.处理器映射器(注解方式)
对标记@Controller类中标识有@RequestMapping的方法进行映射,在@RequestMapping里边定义映射的url。使用注解的映射器不用在xml中配置url和Handler的映射关系。
3.处理器适配器(注解方式)
注解处理器适配器和注解的处理器映射器是配对使用。理解为不能使用非注解映射器进行映射。