八、SpringMVC整合Mybatis之Controller

springmvc.xml

resources/spring文件下建立springmvc.xml文件,配置處理器映射器、適配器、視圖解析器。html

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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,指定controller的包-->
    <context:component-scan base-package="com.iot.learnssm.firstssm.controller"></context:component-scan>
    
    <!-- 使用mvc:annotation-driven代替配置RequestMappingHandlerMapping和RequestMappingHandlerAdapter
     -->
    <mvc:annotation-driven></mvc:annotation-driven>
    
    <!-- 視圖解析器解析jsp,默認使用jstl,classpath下要有jstl的包-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 配置jsp路徑的前綴 -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!-- 配置jsp路徑的後綴 -->
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

配置web.xml

參考入門程序,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_3_0.xsd"
         id="WebApp_ID" version="3.0">
    <display-name>firstssm</display-name>

    <!-- 加載spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/classes/spring/applicationContext-*.xml</param-value>
        <!--  <param-value>classpath:spring/applicationContext-*.xml</param-value>-->
    </context-param>
    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
	<!-- springmvc 前端控制器  -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- contextConfigLocation配置springmvc加載的配置文件(配置處理器映射器、適配器等等)
          若不配置,默認加載WEB-INF/servlet名稱-servlet(springmvc-servlet.xml)
        -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
    </servlet>    
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    
    <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>
</web-app>

這個文件有兩個做用:java

  • 配置前端控制器(DispatcherServlet)
  • 加載spring容器:添加spring容器監聽器,加載spring容器,使用通配符加載spring/下的配置文件
    • applicationContext-dao.xml
    • applicationContext-service.xml
    • applicationContext-transaction.xml

編寫Controller(就是Handler)

package com.iot.learnssm.firstssm.controller;

import com.iot.learnssm.firstssm.po.Items;
import com.iot.learnssm.firstssm.po.ItemsCustom;
import com.iot.learnssm.firstssm.service.ItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

//使用@Controller來標識它是一個控制器
@Controller
public class ItemsController {

    @Autowired
    private ItemsService itemsService;

    @RequestMapping("/queryItems")
    public ModelAndView queryItems() throws Exception{
        //調用service查找數據庫,查詢商品列表
        List<ItemsCustom> itemsList = itemsService.findItemsList(null);
        //返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        //至關於request的setAttribute方法,在jsp頁面中經過itemsList取數據
        modelAndView.addObject("itemsList",itemsList);
        //指定視圖
        //下邊的路徑,若是在視圖解析器中配置jsp的路徑前綴和後綴,修改成items/itemsList
        //modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
        //下邊的路徑配置就能夠不在程序中指定jsp路徑的前綴和後綴
        modelAndView.setViewName("items/itemsList");
        return modelAndView;
    }
}

編寫jsp

服務器路徑爲WEB-INF/jsp/items/itemsList.jspweb

<%@ 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>
相關文章
相關標籤/搜索