1. 程式人生 > >JavaEE_Mybatis_SpringMVC_Spring_lesson3_註解處理器對映器與介面卡以及處理器(Controller)

JavaEE_Mybatis_SpringMVC_Spring_lesson3_註解處理器對映器與介面卡以及處理器(Controller)

註解的處理器對映器相比與非註解的處理器對映器的優勢

:可以在一個類中寫多個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>