初识Spring-MVC之最小配置运行Hello world的maven项目
2017-05-26 00:42
525 查看
使用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
<!--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的基本配置】就会出现一个大的框架
例如:
[html] view
plain copy
<?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的配置属性
例如:
[html] view
plain copy
<!--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的标签)
[html] view
plain copy
<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文件中设置注解设置控制器
例如
[html] view
plain copy
<?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注解。
例如:
[java] view
plain copy
/*
* 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的字样
可以从以下两个网址下载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
<!--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的基本配置】就会出现一个大的框架
例如:
[html] view
plain copy
<?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的配置属性
例如:
[html] view
plain copy
<!--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的标签)
[html] view
plain copy
<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文件中设置注解设置控制器
例如
[html] view
plain copy
<?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注解。
例如:
[java] view
plain copy
/*
* 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的字样
相关文章推荐
- 初识Spring-MVC之最小配置运行Hello world的maven项目
- maven+springMVC+hibernate项目搭建(一基础环境配置)
- 详细配置maven+springmvc入门项目及会碰到的问题
- maven构建项目二(添加配置文件)SpringMvc+Spring+Mybatis
- eclipse 中创建 整合springmvc+mybatis+maven 的java web项目步骤,以及maven仓库配置
- maven+springMVC+hibernate项目搭建(三spring配置结束)
- 使用maven给spring项目打可直接运行的jar包(配置文件内置外置的打法)
- SpringBoot项目在新电脑上的配置运行,包括JDK+MAVEN+Git+SpringBoot配置等
- maven 项目 spring mvc + jdbc 配置文件
- 公司项目所用到的一些配置(SpringMvc+maven+ngix+tomcat)
- springmvc+mongodb+maven 项目搭建配置
- Spring boot框架项目,使用maven命令将配置文件打包到jar包外,项目运行读取jar外配置文件
- IDEA 配置maven + SpringBoot项目在新电脑上的配置运行
- 【安装】IntelliJ IDEA+Maven+SpringMVC+Hibernate项目搭建运行
- java SSM(Spring+SpringMVC+MyBatis)maven项目 intellij idea 2017配置 MAC,(Linux,ubuntu,centos 只要更改相应目录即可)
- 从零开始搭建maven+springmvc+mybatis web项目(2)---springmvc配置文件编写
- springmvc+mongodb+maven 项目搭建配置
- Eclipse Maven项目 Spring4 MVC 配置 Freemarker视图添加 templateLoaderPath 时报错
- Spring+SpringMVC+Mybatis配置(Maven项目)
- 使用maven给spring项目打可直接运行的jar包(配置文件内置外置的打法)