使用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的字样