使用NetBeans軟件和Tomcat服務器開發Spring-MVC的最小配置的運行環境的Maven項目
可以從以下兩個網址下載Spring-MVC的所需的Maven包:
①、http://mvnrepository.com/
②、http://search.maven.org/
1.使用Netbeans創建一個Maven的【Web 應用程序】項目
2.配置pom.xml文件,下載所需的Spring-MVC的Jar包
①、Spring-webmvc的jar包
②、Spring-context的jar包
例如:
<!--Spring-mvc的jar包begin-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<!--Spring-mvc的jar包end-->
只要導入這兩個dependency的jar包,剩餘的相關Spring的Jar包就會自動添加上來
3.如果有WEB-INF文件,就不用創建,沒有的話就在【WEB 頁】下創建一個【WEB-INF】文件夾
在【WEB-INF】文件夾中創建一個【web.xml】文件。在網上搜索一下【javaweb的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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>db</display-name>
<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>
然後在web.xml中配置Spring-MVC的配置屬性
例如:
<!--Spring的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
<!--默認值是:applicationContext.xml-->
</context-param>
<!--作用就是在啓動web服務器時,自動裝配ApplicationContext的配置信息-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<!--servlet的一個名稱-->
<servlet-name>do</servlet-name>
<!--Dispatcherservlet所在的類路徑-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--下面可以不寫,他有一個默認的contextConfigLocation配置,默認的param-value是【servlet-name】的值-servlet.xml文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/do-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>do</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
4.創建一個空的applicationContext.xml文件(Spring的配置文件就是web.xml中的context-param的標籤)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
</beans>
5.創建一個空的do-servlet.xml文件(Spring-MVC的配置文件,就是web.xml文件中的servlet中的param)
他和applicationContext.xml文件是一樣的。
在src在創建一個控制器所在的包:例如:com.controller
在do-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: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-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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 設置使用註解的類所在的jar包 -->
<context:component-scan base-package="com.controller" />
</beans>
6.創建一個控制器:MyController
使用註解的方式設置控制器。使用@controller註解。
例如:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
*
* @author Administrator
*/
@Controller
public class MyController {
//設置請求路徑的匹配,其中value就是匹配項
@RequestMapping(value = "/ajax1")
public @ResponseBody String test(){
return "11111";
}
}
7.在運行並部署這個項目到Tomcat上,在地址欄中輸入http://localhost:8080/項目名/ajax1.do
就會看見11111的字樣