您的位置:首页 > 编程语言 > Java开发

在IDEA中用maven来创建一个springMVC项目

2017-05-19 23:13 113 查看

在IDEA中用maven来创建一个springMVC项目

刚刚学习springMVC,现在记录一下如何创建一个springMVC项目。

首先介绍使用的工具是Intellij Idea2016.3.4,IntelliJ在业界被公认为最好的java开发工具之一。

  • 首先New一个Maven Project,勾选使用网络模板create from archetype,下面选maven-archetype-webapp,然后next

  • 填写项目坐标

  • 接下来是配置maven仓库。Idea默认会去找本地C:\Users\用户名.m2\repository的maven,如果没有的话就回去国外的maven仓库服务器上下载
  • 接下来填写项目名字,finish
  • 项目创建完成后,在src-main下建立java目录,此时需要在IDea中对目录进行标注,这样才能让Idea知道这是写代码的目录。

  • Sources 一般用于标注类似 src 这种可编译目录。有时候我们不单单项目的 src 目录要可编译,还有其他一些特别的目录也许我们也要作为可编译的目录,就需要对该目录进行此标注。只有 Sources 这种可编译目录才可以新建 Java 类和包,这一点需要牢记。
  • Tests 一般用于标注可编译的单元测试目录。在规范的 maven 项目结构中,顶级目录是 src,maven 的 src 我们是不会设置为 Sources 的,而是在其子目录 main 目录下的 java 目录,我们会设置为 Sources。而单元测试的目录是 src - test - java,这里的 java 目录我们就会设置为 Tests,表示该目录是作为可编译的单元测试目录。一般这个和后面几个我们都是在 maven 项目下进行配置的,但是我这里还是会先说说。从这一点我们也可以看出 IntelliJ IDEA 对 maven 项目的支持是比较彻底的。
  • Resources 一般用于标注资源文件目录。在 maven 项目下,资源目录是单独划分出来的,其目录为:src - main -resources,这里的 resources 目录我们就会设置为 Resources,表示该目录是作为资源目录。资源目录下的文件是会被编译到输出目录下的。
  • Test Resources 一般用于标注单元测试的资源文件目录。在 maven 项目下,单元测试的资源目录是单独划分出来的,其目录为:src - test -resources,这里的 resources 目录我们就会设置为 Test Resources,表示该目录是作为单元测试的资源目录。资源目录下的文件是会被编译到输出目录下的。
  • Excluded 一般用于标注排除目录。被排除的目录不会被 IntelliJ IDEA 创建索引,相当于被 IntelliJ IDEA 废弃,该目录下的代码文件是不具备代码检查和智能提示等常规代码功能。
    通过上面的介绍,我们知道对于非 maven 项目我们只要会设置 src 即可。
    引用http://wiki.jikexueyuan.com/project/intellij-idea-tutorial/eclipse-java-web-project-introduce.html
  • 现在一个完整的项目结构是这样

  • 接下来在pom.xml中添加需要的jar包。pom内容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.Demo</groupId>
<artifactId>mvc_maven</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>mvc_maven Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>

</dependencies>
<build>
<finalName>mvc_maven</finalName>
</build>
</project>

此时右下角会跳出小气泡,点击import changes会导入jar包。
- 导入完成之后,就可以配置web.xml了

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!--welcome pages-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!--配置springmvc DispatcherServlet-->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

DispitcherServlet就是SpringMVC框架的前端控制器了,servlet-name可以随便起,记住后面要用到。映射中url-pattern输入‘/’表示对所有请求进行拦截,交由前端控制器来处理。
- 在WEB-INF目录下面新建一个‘servlet-name’+mvc的xml文件,我这里就用mvc-servlet.xml,其内容如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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></mvc:annotation-driven>
<context:component-scan base-package="com.example.controller"></context:component-scan>

</beans>
  • 最后在controller中新建一个普通的java文件,其中创建一个控制器
package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* Created by 10300 on 2017/5/19.
*/
@ResponseBody
@Controller
public class Test {
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String sayhello(){
return "hello!";
}
}
  • 现在一个springMVC项目就建好了,接下来就是把它发布到tomcat上面就可以访问了

  • 最终结果

https://www.geek-share.com/detail/2681139561.html 内容部分参考了这篇文章。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: