注解的处理器映射器相比与非注解的处理器映射器的优势
:可以在一个类中写多个RequestMapping("url")的格式,不需要以实现接口的形式进行开发,
注解:可以在一个类中完成多个action,
非注解:实现接口,一个类中只能写一个action
另外:注解的处理器映射器需要与注解的解析器成对使用
演示代码(接lesson1,2)相同部分不展示
1.Springmvc.xml(配置文件)
2.Controller实现(注解形式)
3.前端Jsp展示页面
文档目录结构
1.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 注解的开发 -->
<!-- MVC注解驱动 : 默认加载了多个参数绑定方法 JOSN转换解析器默认加载-->
<!--
<mvc:annotation-driven></mvc:annotation-driven>
-->
<!-- 注解处理器映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!-- 注解的处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!-- 组件扫描: 用来导入handler(controller) service... -->
<context:component-scan base-package="cn.itcast.ssm.controller"></context:component-scan>
<!-- ================================================================================ -->
<!-- ================================================================================ -->
<!-- ================================================================================ -->
<!-- ================================================================================ -->
<!-- ================================================================================ -->
<!-- 非注解的开发 -->
<!-- 非注解处理器Handler: 处理器映射器映射的具体的处理器 -->
<bean id="itemController1" name="/queryItems.action" class="cn.itcast.ssm.controller.ItemsController1"/>
<bean id="itemController2" class="cn.itcast.ssm.controller.ItemsController2"/>
<!-- 非注解处理器适配器: 所有处理器适配器都实现 HandlerAdapter接口 -->
<bean
class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<!-- 非注解处理器适配器:(第二种) 实现 -->
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" />
<!-- 非注解处理器映射器 : 将Bean的 name的 url进行查找,需要在配置Handler指定Beanname(就是url) -->
<bean
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/queryItems1.action">itemController1</prop>
<prop key="/queryItems2.action">itemController1</prop>
<prop key="/queryItems3.action">itemController2</prop>
</props>
</property>
</bean>
<!-- 非注解视图解析器 -->
<!-- ViewResolver
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
</bean>
</beans>
2.Controller实现(注解形式)
cn.itcast.ssm.controller.ItemsController3.java
package cn.itcast.ssm.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import cn.itcast.ssm.po.Items;
@Controller
public class ItemsController3 {
@RequestMapping("/queryItems4")
public ModelAndView queryItems() throws Exception {
List<Items> itemsList = new ArrayList<>();
Items items_1 = new Items();
items_1.setName("联想笔记本");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad T430 联想笔记本电脑!");
Items items_2 = new Items();
items_2.setName("苹果手机");
items_2.setPrice(7000f);
items_2.setDetail("iphone6苹果手机!");
itemsList.add(items_1);
itemsList.add(items_2);
// 新建ModelAndView
ModelAndView modelAndView = new ModelAndView();
// 相当于request 的 setAttribute, 在 jsp 页面中通过 itemList 取数据
modelAndView.addObject("itemsList", itemsList);
// 指定视图
modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.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="${itemsList }" 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 }/item/editItem.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>