關於IDEA建立一個基於springMVC的javaweb項目請查看個人另一篇博客,以下連接:javascript
http://www.javashuo.com/article/p-tifjnhni-nx.htmlcss
本篇文章假設在創建完一個springMVC項目以後,介紹如何進行springMVC的配置,文章的最後會給一個完整的基於IDEA的工程demo。html
web.xml文件是用來初始化工程配置信息的,好比說welcome頁面,filter,listener,servlet,servlet-mapping,啓動加載級別等等。每個xml文件都有定義他書寫規範的schema文件,web.xml所對應的xml Schema文件中定義了多少種標籤元素,web.xml中就能夠出現它所定義的標籤元素,也就具有哪些特定的功能。web.xml的模式文件是由Sun 公司定義的,每一個web.xml文件的根元素爲<web-app>中,必須標明這個web.xml使用的是哪一個模式文件。前端
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> </web-app>
當spring web啓動的時候,會同時啓動兩個應用上下文,一個是由DispatcherServlet建立,另一個是由ContextLoaderListener建立。java
1. DispatcherServlet:主要負責控制器,視圖解析器、以及映射處理器的映射(注入)mysql
2.ContextLoaderListener:主要負責加載應用中其餘bean(JDBC、Email等等)react
1). 先新建一個servlet配置xml:dispatcher-servlet.xmljquery
(IDEA創建springMVC工程默認有一個servlet在WEB-INF下:dispatcher-servlet.xml)。這個命名有個必須遵照的規律:dispatcher是servlet名!而後 接上 -servlet.xml。如要重命名,則:重命名-servlet.xmlgit
2). web.xml 引入dispatch-servlet.xml的配置github
注意servlet名爲以上的dispatcher-servlet.xml 的 dispatcher!
<servlet> <servlet-name>dispatcher</servlet-name><!-- 這個名字很重要! --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 導入類 --> <load-on-startup>1</load-on-startup><!-- 啓動就加載 --> </servlet>
如是自定義的重命名或者變動位置,則要在配置servlet調度員時候說明。如下是自定義servlet名springmvc
,注意都要同步改<servlet-name>名字。
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- 如下兩條是聲明自定義的servlet配置文件,說明其具體的路徑 --> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
3)爲dispatcher的經常使用基本配置舉例(在dispatch-servlet.xml)中
<!-- controller掃描器 --> <context:component-scan base-package="com.test.controller"/> <!-- 配置試圖解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/jsp/"/> <property name="suffix" value=".jsp"/> </bean>
4) web.xml設置servlet接收請求過濾器
<servlet-mapping> <servlet-name>dispatcher</servlet-name><!-- 保持這個名字同步 --> <url-pattern>*.do</url-pattern> <!--只攔截*.do的請求--> </servlet-mapping>
1). 在classpath:/confg/applicationContext.xml位置建立
2). 在web.xml中引入
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/confg/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
3)applicationContext.xml用於常規spring容器配置
<?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:p="http://www.springframework.org/schema/p" 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"> <context:component-scan base-package="com.test.dao"/> <context:component-scan base-package="com.test.service"/> <context:property-placeholder location="classpath:/confg/jdbc.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}"/> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource"/> </beans>
如以上applicationContext.xml所用到的jdbc.properties配置jdbc的數據源
#Mysql jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/skyauto?useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=root
1). web.xml中配置:讓servlet調度器攔截全部的請求
<servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> <!--攔截全部的請求--> </servlet-mapping>
默認加載css/js/img等靜態資源(其實也是URL的形式加載)的時候能夠使用 / 攔截全部的請求,意思是接收任何形式的URL請求。
2). 設置只攔截特定的URL請求(如*.do)另外特定的靜態資源開「後門」
若是想在這一關口作安全機制,只攔截特定的請求,以保護服務器免受干擾請求,浪費時間和資源。因此能夠設置以下。
<servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.do</url-pattern> <!-- 攔截帶*.do結尾的URL請求 --> </servlet-mapping>
可這樣一作,又把WEB-INF下的css/js/img等靜態資源的加載請求拒之門外(WEB-INF是Java的WEB應用的安全目錄。所謂安全就是客戶端(html中導入)沒法訪問,只有服務端能夠訪問的目錄。)!因此須要給這些資源打開特殊的後門,以下在dispatcher-Servlet.xml中進行配置。讓這些靜態資源不用通過servlet調度器。dispatcher-servlet.xml中添加以下設置
<!-- 聲明一些靜態資源不用通過DispatcherServlet調度器 --> <mvc:annotation-driven/> <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/> <mvc:resources location="/WEB-INF/images/" mapping="/images/**"/> <mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>
<welcome-file-list> <welcome-file>/WEB-INF/jsp/***.jsp</welcome-file> </welcome-file-list>
IDEA project demo下載[github]:https://github.com/BobwithB/springMVC/blob/master/demoProj.zip
1. web.xml
<!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> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file> </welcome-file-list> </web-app>
2. dispatcher-servlet.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.xsd"> <!-- 註解掃描器 --> <context:component-scan base-package="com.mycompany.demoProj"/> <!-- 聲明一些靜態資源不用通過DispatcherServlet調度器 --> <mvc:annotation-driven/> <mvc:resources location="/css/" mapping="/css/**"/> <mvc:resources location="/img/" mapping="/img/**"/> <mvc:resources location="/js/" mapping="/js/**"/> <!-- 配置試圖解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
3.前端 AJAX發出一個請求
<%-- Created by IntelliJ IDEA. User: BCool Date: 2017/9/25 Time: 20:13 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>hello world</title> </head> <body> <button id="btn" onclick="test()">測試</button> <script src="../../js/jquery-3.2.1.min.js"></script> <script> function test(){ $.ajax({ url: "test.do", type: "POST", dataType: "text", contentType:"application/json", success: function(data) { alert("後臺返回"+data); }, error: function() { alert("error"); } }); } </script> </body> </html>
4. 後臺剛剛dispatcher-servlet.xml中設置的依賴注入掃描包:com.***.controller中響應請求
package com.mycompany.demoProj; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class myController { @RequestMapping(value="/test.do",method= RequestMethod.POST,produces="text/html;charset=UTF-8;") @ResponseBody public String test(){ System.out.print("\n + react test.do"); return "hello world"; } }