使用Maven快速创建一个SpringMVC工程步骤
第一步:创建maven工程,加入SpringMVC的maven依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.0.RELEASE</version>
</dependency> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> <dependency>
<groupId>taglibs</groupId>
15 <artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
第二步:在pom.xml文件中加入tomcat7的插件;
<pluginManagement>
<plugins>
<!-- 配置tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version> <configuration>
<path>/tomcat-demo</path>
<port>8080</port>
<uriEncoding>UTF-8</uriEncoding>
<url>http://localhost:8080/manager/text</url>
<server>tomcat7</server>
</configuration>
</plugin>
</plugins>
</pluginManagement>
第三步:配置SpringMVC配置文件;
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
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/util
http://www.springframework.org/schema/util/spring-util.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" >
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven/> <!-- 如果当前请求为“/”时,则转发到“/helloworld/index” -->
<mvc:view-controller path="/" view-name="forward:/helloworld/index"/>
<!-- 静态资源映射 -->
<mvc:resources mapping="/js/**" location="/WEB-INF/js/" />
<mvc:resources mapping="/css/**" location="/WEB-INF/css/" />
<mvc:resources mapping="/fonts/**" location="/WEB-INF/fonts/" />
<mvc:resources mapping="/plugins/**" location="/WEB-INF/plugins/" />
<mvc:resources mapping="images/**" location="/WEB-INF/images/" />
<!-- 当上面要访问的静态资源不包括在上面的配置中时,则根据此配置来访问 -->
<mvc:default-servlet-handler/>
<!-- 开启controller注解支持 -->
<!-- use-default-filters="false" 只扫描指定的注解 -->
<context:component-scan base-package="com.zte.ems.controllers" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan> <!-- 配置视图解析器,并指定视图所在的文件夹 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="contentType" value="text/html"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
第四步:配置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="taotao" version="2.5">
<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SpringMVCLesson</servlet-name>
<!-- DispatcherServlet主要就是拦截请求,然后调用对应的Controller和Action -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- classpath:springservlet-config.xml指定具体的配置文件为springservlet-config.xml -->
<param-value>classpath:springservlet-config.xml</param-value>
</init-param>
<!-- <load-on-startup>是启动顺序,让这个Servlet随Servletp容器一起启动,必须放在<servlet> 配置的最后。 -->
<load-on-startup>1</load-on-startup><!-- load-on-startup必须放在最后 -->
</servlet>
<!-- Spring MVC配置文件结束 -->
<servlet-mapping>
<!-- 指定配置的是哪个servlet -->
<servlet-name>SpringMVCLesson</servlet-name>
<!-- 指定拦截哪些请求到该servlet,这里配置的是拦截全部请求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
第五步:创建一个Contrller控制的Java类:
package com.zte.ems.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/user")
public class FormController {
@RequestMapping("/success")
public String getSuccess() {
//跳转页面,默认为转发
return "success"; }
}
其中@Controller注解表示,该类是一个受Spring管理的类,一个类使用了@Controller进行标记的都是Controller。
@RequestMapper注解表示,url路径的映射,其实RequestMapping在Class上,可看做是父Request请求url,而RequestMapping在方法上的可看做是子Request请求url,父子请求url最终会拼起来与页面请求url进行匹配。
最后通过clean tomcat7:run 来启动tomcat,在浏览器输入响应的url即可访问。
使用Maven快速创建一个SpringMVC工程步骤的更多相关文章
- Maven快速创建SpringMVC web(1)
Maven快速创建SpringMVC web工程详解 转自:http://blog.csdn.net/cndmss/article/details/52184836
- SpringBoot2.x入门:快速创建一个SpringBoot应用
前提 这篇文章是<SpringBoot2.x入门>专辑的第2篇文章,使用的SpringBoot版本为2.3.1.RELEASE,JDK版本为1.8. 常规的套路会建议使用Spring官方提 ...
- 使用Vaadin的maven archetype创建一个空Vaadin项目
所在公司要求使用这个臭屎粑粑一样的Vaadin,我也没办法.为了更好地开展工作,对得起老板发给我的工资,就算是臭屎粑粑,也要尽力给他玩儿出花样来. Vaadin针对Eclipse和Netbeans等I ...
- Eclipse创建一个mybatis工程实现连接数据库查询
Eclipse上创建第一mybatis工程实现数据库查询 步骤: 1.创建一个java工程 2.创建lib文件夹,加入mybatis核心包.依赖包.数据驱动包.并为jar包添加路径 3.创建resou ...
- 利用Zynq Soc创建一个嵌入式工程
英文题目:Using the Zynq SoC Processing System,参考自ADI的ug1165文档. 利用Zynq Soc创建一个嵌入式工程,该工程总体上包括五个步骤: 步骤一.新建空 ...
- 通过beego快速创建一个Restful风格API项目及API文档自动化
通过beego快速创建一个Restful风格API项目及API文档自动化 本文演示如何快速(一分钟内,不写一行代码)的根据数据库及表创建一个Restful风格的API项目,及提供便于在线测试API的界 ...
- Android开发的初学者快速创建一个项目
因为gwf的原因,大陆连不上google所以AndroidSDK是无法更新的 而且设置代理也不一定能解决问题 如果是初学者想快速的了解安卓开发,可以在国内的内网下载整合包 下载地址:http://rj ...
- 创建一个Android工程
Creating an Android Project 原文演示了怎么通过Android Studio和命令行两种方式来创建一个Android工程. 原文链接:http://developer.and ...
- 在.NET中快速创建一个5GB、10GB或更大的空文件
对于通过UDP进行打文件传输的朋友应该首先会考虑到一个问题,那就是由于UDP并不会根据先来先到原则进行发送,也许你发送端发送的时候是以包1和包2的顺序传输的,但接收端可能以包2和包1 的顺序来进行接收 ...
随机推荐
- apache Alias使用问题
今天在配置apache的过程中,使用了Alias,但是由于配置错误导致403 forbidden错误,不能正常访问. 首先理解一下Alias,Alias就是别名的意思,假如我的项目目录在/home/w ...
- jQuery中,$.extend,$obj.extend和$.fn.extend三者的区别
jQuery中,$.extend,$obj.extend和$.fn.extend三者的区别 现在做的一个项目,所使用的框架是基于jQuery扩展的,于是平时学了一下jQuery,了解到了它的扩展函数: ...
- poj1269
基础题,直线间关系 #include <iostream> #include <math.h> #include <iomanip> #define eps 1e- ...
- Emacs折腾经验谈
Emacs折腾经验谈 这几天都没有动力写mongodb的东西,我果然还是太懒了么~ 主要是没有一个系统的东西整理出来,加上我令人拙计的语言表达能力,这个坑只能慢慢再补了. 最近在折腾emacs这个东西 ...
- MSBuild是什么?
MSBuild入门 MSBuild是什么? MSBuild全称(Microsoft Build Engine),是用来生成.NET程序的平台.您可能不知道它,但是如果您在使用VS做开发,那么一定时时刻 ...
- c# winform 在一个窗体中使用另一个窗体中TextBox控件的值——解决办法
[前提]一个winform应用程序项目中,窗体B,需要使用 窗体A 中一个TextBox控件的值,进行计算等操作. [解决方案] 1.在窗体A中定义:public static double a;// ...
- Python自动化测试 -ConfigParser模块读写配置文件
C#之所以容易让人感兴趣,是因为安装完Visual Studio, 就可以很简单的直接写程序了,不需要做如何配置. 对新手来说,这是非常好的“初体验”, 会激发初学者的自信和兴趣. 而有些语言的开发环 ...
- service structure flowchart [mobile to server via HTTP RESTful API and TCP/IP in a map]
mobile to server in RESTful and TCP/IP way
- VMware vSphere 服务器虚拟化之二十四 桌面虚拟化之手动池管理物理机
VMware vSphere 服务器虚拟化之二十四 桌面虚拟化之手动池管理物理机 VMwareView手动池可以管理物理计算机 说明: 环境基于实验二十三 1.准备一台Windows 7的物理计算机名 ...
- 12503 - Robot Instructions
Robot Instructions You have a robot standing on the origin of x axis. The robot will be given som ...