程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> springmvc學習筆記(9)-springmvc整合mybatis之controller

springmvc學習筆記(9)-springmvc整合mybatis之controller

編輯:JAVA綜合教程

springmvc學習筆記(9)-springmvc整合mybatis之controller


springmvc學習筆記(9)-springmvc整合mybatis之controller

 

本文介紹如何配置springmvc配置文件和web.xml,以及如何編寫controller,jsp

springmvc.xml

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

<code class="language-xml hljs "><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">

   <!--{cke_protected}{C}%3C!%2D%2D%20%E5%AF%B9%E4%BA%8E%E6%B3%A8%E8%A7%A3%E7%9A%84Handler%20%E5%8F%AF%E4%BB%A5%E5%8D%95%E4%B8%AA%E9%85%8D%E7%BD%AE%0A%20%20%20%20%E5%AE%9E%E9%99%85%E5%BC%80%E5%8F%91%E4%B8%AD%E5%8A%A0%E4%BD%A0%E4%BD%BF%E7%94%A8%E7%BB%84%E4%BB%B6%E6%89%AB%E6%8F%8F%0A%20%20%20%20%2D%2D%3E-->
    <!--{cke_protected}{C}%3C!%2D%2D%20%E5%8F%AF%E4%BB%A5%E6%89%AB%E6%8F%8Fcontroller%E3%80%81service%E3%80%81...%0A%20%20%20%20%E8%BF%99%E9%87%8C%E8%AE%A9%E6%89%AB%E6%8F%8Fcontroller%EF%BC%8C%E6%8C%87%E5%AE%9Acontroller%E7%9A%84%E5%8C%85%0A%20%20%20%20%20%2D%2D%3E-->
    <context:component-scan base-package="com.iot.learnssm.firstssm.controller"></context:component-scan>


    <!--{cke_protected}{C}%3C!%2D%2D%20%E4%BD%BF%E7%94%A8mvc%3Aannotation-driven%E4%BB%A3%E6%9B%BF%E4%B8%8A%E9%9D%A2%E4%B8%A4%E4%B8%AA%E6%B3%A8%E8%A7%A3%E6%98%A0%E5%B0%84%E5%99%A8%E5%92%8C%E6%B3%A8%E8%A7%A3%E9%80%82%E9%85%8D%E7%9A%84%E9%85%8D%E7%BD%AE%0A%20%20%20%20%20mvc%3Aannotation-driven%E9%BB%98%E8%AE%A4%E5%8A%A0%E8%BD%BD%E5%BE%88%E5%A4%9A%E7%9A%84%E5%8F%82%E6%95%B0%E7%BB%91%E5%AE%9A%E6%96%B9%E6%B3%95%EF%BC%8C%0A%20%20%20%20%20%E6%AF%94%E5%A6%82json%E8%BD%AC%E6%8D%A2%E8%A7%A3%E6%9E%90%E5%99%A8%E9%BB%98%E8%AE%A4%E5%8A%A0%E8%BD%BD%E4%BA%86%EF%BC%8C%E5%A6%82%E6%9E%9C%E4%BD%BF%E7%94%A8mvc%3Aannotation-driven%E5%88%99%E4%B8%8D%E7%94%A8%E9%85%8D%E7%BD%AE%E4%B8%8A%E9%9D%A2%E7%9A%84RequestMappingHandlerMapping%E5%92%8CRequestMappingHandlerAdapter%0A%20%20%20%20%20%E5%AE%9E%E9%99%85%E5%BC%80%E5%8F%91%E6%97%B6%E4%BD%BF%E7%94%A8mvc%3Aannotation-driven%0A%20%20%20%20%20%2D%2D%3E-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--{cke_protected}{C}%3C!%2D%2D%20%E8%A7%86%E5%9B%BE%E8%A7%A3%E6%9E%90%E5%99%A8%0A%20%20%20%20%E8%A7%A3%E6%9E%90jsp%2C%E9%BB%98%E8%AE%A4%E4%BD%BF%E7%94%A8jstl%2Cclasspath%E4%B8%8B%E8%A6%81%E6%9C%89jstl%E7%9A%84%E5%8C%85%0A%20%20%20%20%2D%2D%3E-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--{cke_protected}{C}%3C!%2D%2D%20%E9%85%8D%E7%BD%AEjsp%E8%B7%AF%E5%BE%84%E7%9A%84%E5%89%8D%E7%BC%80%20%2D%2D%3E-->
        <property name="prefix" value="/WEB-INF/jsp/">
        <!--{cke_protected}{C}%3C!%2D%2D%20%E9%85%8D%E7%BD%AEjsp%E8%B7%AF%E5%BE%84%E7%9A%84%E5%90%8E%E7%BC%80%20%2D%2D%3E-->
        <property name="suffix" value=".jsp">
    </property></property></bean>

</beans></code>

配置web.xml

參考入門程序,web.xml

<code class="language-xml hljs "><!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%20%3F%2D%2D%3E-->
<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>

    <!--{cke_protected}{C}%3C!%2D%2D%20%E5%8A%A0%E8%BD%BDspring%E5%AE%B9%E5%99%A8%20%2D%2D%3E-->
    <context-param>
        <cke:param-name>contextConfigLocation</cke:param-name>
        <cke:param-value>WEB-INF/classes/spring/applicationContext-*.xml</cke:param-value>
        <!--{cke_protected}{C}%3C!%2D%2D%20%20%3Cparam-value%3Eclasspath%3Aspring%2FapplicationContext-*.xml%3C%2Fparam-value%3E%2D%2D%3E-->
      </context-param>
    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


<!--{cke_protected}{C}%3C!%2D%2D%20springmvc%20%E5%89%8D%E7%AB%AF%E6%8E%A7%E5%88%B6%E5%99%A8%20%20%2D%2D%3E-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--{cke_protected}{C}%3C!%2D%2D%20contextConfigLocation%E9%85%8D%E7%BD%AEspringmvc%E5%8A%A0%E8%BD%BD%E7%9A%84%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6(%E9%85%8D%E7%BD%AE%E5%A4%84%E7%90%86%E5%99%A8%E6%98%A0%E5%B0%84%E5%99%A8%E3%80%81%E9%80%82%E9%85%8D%E5%99%A8%E7%AD%89%E7%AD%89)%0A%20%20%20%20%20%20%20%20%20%20%E8%8B%A5%E4%B8%8D%E9%85%8D%E7%BD%AE%EF%BC%8C%E9%BB%98%E8%AE%A4%E5%8A%A0%E8%BD%BDWEB-INF%2Fservlet%E5%90%8D%E7%A7%B0-servlet(springmvc-servlet.xml)%0A%20%20%20%20%20%20%20%20%2D%2D%3E-->
        <init-param>
            <cke:param-name>contextConfigLocation</cke:param-name>
            <cke:param-value>classpath:spring/springmvc.xml</cke:param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!--{cke_protected}{C}%3C!%2D%2D%0A%20%20%20%20%20%20%20%20%E7%AC%AC%E4%B8%80%E7%A7%8D%3A*.action%2C%E8%AE%BF%E9%97%AE%E4%BB%A5.action%E4%B8%89%E7%BB%93%E5%B0%BE%EF%BC%8C%E7%94%B1DispatcherServlet%E8%BF%9B%E8%A1%8C%E8%A7%A3%E6%9E%90%0A%20%20%20%20%20%20%20%20%E7%AC%AC%E4%BA%8C%E7%A7%8D%3A%2F%2C%E6%89%80%E6%9C%89%E8%AE%BF%E9%97%AE%E7%9A%84%E5%9C%B0%E5%9D%80%E7%94%B1DispatcherServlet%E8%BF%9B%E8%A1%8C%E8%A7%A3%E6%9E%90%EF%BC%8C%E5%AF%B9%E9%9D%99%E6%80%81%E6%96%87%E4%BB%B6%E7%9A%84%E8%A7%A3%E6%9E%90%E9%9C%80%E8%A6%81%E9%85%8D%E7%BD%AE%E4%B8%8D%E8%AE%A9DispatcherServlet%E8%BF%9B%E8%A1%8C%E8%A7%A3%E6%9E%90%EF%BC%8C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E4%BD%BF%E7%94%A8%E6%AD%A4%E7%A7%8D%E6%96%B9%E5%BC%8F%E5%92%8C%E5%AE%9E%E7%8E%B0RESTful%E9%A3%8E%E6%A0%BC%E7%9A%84url%0A%20%20%20%20%20%20%20%20%E7%AC%AC%E4%B8%89%E7%A7%8D%3A%2F*%2C%E8%BF%99%E6%A0%B7%E9%85%8D%E7%BD%AE%E4%B8%8D%E5%AF%B9%EF%BC%8C%E4%BD%BF%E7%94%A8%E8%BF%99%E7%A7%8D%E9%85%8D%E7%BD%AE%EF%BC%8C%E6%9C%80%E7%BB%88%E8%A6%81%E8%BD%AC%E5%8F%91%E5%88%B0%E4%B8%80%E4%B8%AAjsp%E9%A1%B5%E9%9D%A2%E6%97%B6%EF%BC%8C%E4%BB%8D%E7%84%B6%E4%BC%9A%E7%94%B1DispatcherServlet%E8%A7%A3%E6%9E%90jsp%E5%9C%B0%E5%9D%80%EF%BC%8C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E4%B8%8D%E8%83%BD%E6%A0%B9%E6%8D%AEjsp%E9%A1%B5%E9%9D%A2%E6%89%BE%E5%88%B0handler%EF%BC%8C%E4%BC%9A%E6%8A%A5%E9%94%99%0A%20%20%20%20%20%20%20%20%2D%2D%3E-->
        <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></code>

這個文件有兩個作用:

配置前端控制器(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;

/**
 * Created by brian on 2016/3/2.
 */

//使用@Controller來標識它是一個控制器
@Controller
//為了對url進行分類管理 ,可以在這裡定義根路徑,最終訪問url是根路徑+子路徑
//比如:商品列表:/items/queryItems.action
public class ItemsController {

    @Autowired
    private ItemsService itemsService;

    //商品查詢列表
    @RequestMapping("/queryItems")
    //實現 對queryItems方法和url進行映射,一個方法對應一個url
    //一般建議將url和方法寫成一樣
    public ModelAndView queryItems() throws Exception{
        //調用service查找數據庫,查詢商品列表
        List 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.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"%>


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved