初识Spring-MVC之最小配置运行Hello world的maven项目

使用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包

例如:

[html]  view plain  copy
  1. <!--Spring-mvc的jar包begin-->  
  2.             <dependency>  
  3.                 <groupId>org.springframework</groupId>  
  4.                 <artifactId>spring-webmvc</artifactId>  
  5.                 <version>4.2.5.RELEASE</version>  
  6.             </dependency>  
  7.             <dependency>  
  8.                 <groupId>org.springframework</groupId>  
  9.                 <artifactId>spring-context</artifactId>  
  10.                 <version>4.2.5.RELEASE</version>  
  11.             </dependency>  
  12.         <!--Spring-mvc的jar包end-->  

只要导入这两个dependency的jar包,剩余的相关Spring的Jar包就会自动添加上来

3.如果有WEB-INF文件,就不用创建,没有的话就在【WEB 页】下创建一个【WEB-INF】文件夹

在【WEB-INF】文件夹中创建一个【web.xml】文件。在网上搜索一下【javaweb的web.xml的基本配置】就会出现一个大的框架

例如:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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">  
  3.   <display-name>db</display-name>  
  4.   <welcome-file-list>  
  5.     <welcome-file>index.html</welcome-file>  
  6.     <welcome-file>index.htm</welcome-file>  
  7.     <welcome-file>index.jsp</welcome-file>  
  8.     <welcome-file>default.html</welcome-file>  
  9.     <welcome-file>default.htm</welcome-file>  
  10.     <welcome-file>default.jsp</welcome-file>  
  11.   </welcome-file-list>  
  12. </web-app>  

然后在web.xml中配置Spring-MVC的配置属性

例如:

[html]  view plain  copy
  1. <!--Spring的配置文件-->  
  2.     <context-param>  
  3.         <param-name>contextConfigLocation</param-name>  
  4.         <param-value>/WEB-INF/applicationContext.xml</param-value>  
  5.         <!--默认值是:applicationContext.xml-->  
  6.     </context-param>  
  7.       
  8.     <!--作用就是在启动web服务器时,自动装配ApplicationContext的配置信息-->  
  9.     <listener>  
  10.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  11.     </listener>  
  12.       
  13.     <servlet>  
  14.         <!--servlet的一个名称-->  
  15.         <servlet-name>do</servlet-name>  
  16.         <!--Dispatcherservlet所在的类路径-->  
  17.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  18.         <!--下面可以不写,他有一个默认的contextConfigLocation配置,默认的param-value是【servlet-name】的值-servlet.xml文件-->  
  19.         <init-param>  
  20.             <param-name>contextConfigLocation</param-name>  
  21.             <param-value>/WEB-INF/do-servlet.xml</param-value>  
  22.         </init-param>  
  23.     </servlet>  
  24.     <servlet-mapping>  
  25.         <servlet-name>do</servlet-name>  
  26.         <url-pattern>*.do</url-pattern>  
  27.     </servlet-mapping>  
4.创建一个空的applicationContext.xml文件(Spring的配置文件就是web.xml中的context-param的标签)

[html]  view plain  copy
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  3.     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.                            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  6.                            http://www.springframework.org/schema/tx  
  7.                            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
  8.                            http://www.springframework.org/schema/aop  
  9.                            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
  10.                            http://www.springframework.org/schema/context  
  11.                            http://www.springframework.org/schema/context/spring-context-4.0.xsd">  
  12.   
  13.       
  14. </beans>  
5.创建一个空的do-servlet.xml文件(Spring-MVC的配置文件,就是web.xml文件中的servlet中的param)

他和applicationContext.xml文件是一样的。

在src在创建一个控制器所在的包:例如:com.controller

在do-servlet.xml文件中设置注解设置控制器

例如

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:aop="http://www.springframework.org/schema/aop"  
  7.     xmlns:tx="http://www.springframework.org/schema/tx"  
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  9.                         http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   
  10.                         http://www.springframework.org/schema/mvc   
  11.                         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd   
  12.                         http://www.springframework.org/schema/context   
  13.                         http://www.springframework.org/schema/context/spring-context-4.0.xsd   
  14.                         http://www.springframework.org/schema/aop   
  15.                         http://www.springframework.org/schema/aop/spring-aop-4.0.xsd   
  16.                         http://www.springframework.org/schema/tx   
  17.                         http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">  
  18.     <!-- 设置使用注解的类所在的jar包 -->  
  19.     <context:component-scan base-package="com.controller" />  
  20. </beans>  
6.创建一个控制器:MyController

使用注解的方式设置控制器。使用@controller注解。

例如:

[java]  view plain  copy
  1. /* 
  2.  * To change this template, choose Tools | Templates 
  3.  * and open the template in the editor. 
  4.  */  
  5. package com.controller;  
  6.   
  7. import org.springframework.stereotype.Controller;  
  8. import org.springframework.web.bind.annotation.RequestMapping;  
  9. import org.springframework.web.bind.annotation.ResponseBody;  
  10.   
  11. /** 
  12.  *  
  13.  * @author Administrator 
  14.  */  
  15. @Controller  
  16. public class MyController {  
  17.     //设置请求路径的匹配,其中value就是匹配项  
  18.     @RequestMapping(value = "/ajax1")  
  19.     public @ResponseBody String test(){  
  20.         return "11111";  
  21.     }  
  22. }  
7.在运行并部署这个项目到Tomcat上,在地址栏中输入http://localhost:8080/项目名/ajax1.do

就会看见11111的字样

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值