关于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"; } }