详解IDEA用maven创建springMVC项目和配置

网友投稿 336 2023-03-29


详解IDEA用maven创建springMVC项目和配置

本文介绍了IDEA用maven创建springMVC项目和配置,分享给大家,具体如下:

工具准备:IDEA2016.3 java jdk 1.8

1、DEA创建项目

新建一个maven project,并且选择webapp原型。

然后点击next

这里的GroupId和ArtifactID随意填写,但是ArtifactID最好和你的项目一名一样然后next

为了快一点创建,我们添加一个属性值,如图中亮的所示,点右边的加号,name=archetypeCatalog value=internal。

这里我们选择使用IDEA自带的maven,下面的两个分别是你的maven配置文件和你的仓库地址,我记得idea的maven一开始是没有setting.xml的,所以你要去maven的目录里面拷一份setting.xml到你的仓库中。idea的maven在安装路径的plugins文件夹下,即X:\xxx\JetBrains\IntelliJ IDEA 2016.3.2\plugins\plugins\maven\lib\maven3\conf\setting.xml拷贝到你的用户文件夹下的.m2文件夹下,为了之后能够快速的下载依赖包,我们要加一个官方库的镜像,因为maven的官方仓库在国外,太慢了。

我们在你的.m2文件夹下的setting.xml中添加如下代码:

alimaven

central

aliyun maven

http://maven.aliyun.com/nexus/content/repositories/central/

具体位置如下图所示

这个是阿里的库,所以很快。做完之后我们回到idea,点击next,然后填写项目名,然后finish。

接下来idea开始创建项目,这里你要把maven自动导入打开。

然后等待maven创建项目成功

这样我们的项目初始的框架就弄好了。

2、添加pom依赖

创建好之后的项目目录如图所示

我们打开其中的pom.xml,添加我们的依赖。这里我把我的依赖全部放出来,复制到你的pom.xml的dependencies标签之间就可以了,pom文件中也会给你一个示例。

我的依赖如下

junit

junit

4.12

test

org.slf4j

slf4j-log4j12

1.7.21

javax.servlet

javax.servlet-api

3.1.0

javax.servlet.jsp

jsp-api

2.2

javax.servlet

jstl

1.2

mysql

mysql-connector-java

5.1.35

org.springframework</groupId>

spring-web

4.2.6.RELEASE

org.springframework

spring-webmvc

4.2.6.RELEASE

org.springframework

spring-context

4.2.6.RELEASE

org.springframework

spring-test

4.2.6.RELEASE

org.springframework

spring-jdbc

4.2.6.RELEASE

com.github.stefanbirkner

system-rules

1.16.1

test

org.aspectj

aspectjweaver

1.8.9

org.apache.commons

commons-lang3

3.4

commons-fileupload

commons-fileupload

1.3.1

一旦你复制过去之后,maven就会开始下载相应的jar文件,等待下载完成即可。可能包有点多,不过用了阿里的镜像之后还是挺快的。

3、添加框架支持

配置完pom.xml之后,我们在idea中要添加一下框架的支持。

右击我们的项目文件夹,选择add framework support

然后在窗口中分别选中spring和springmvc,并且选择spring是,记得勾选springconfig.xml

因为我们之前下载过相应的文件,所以这里直接用我们下载好的spring文件。

点击ok之后,我们会发现WEB-INF文件夹下多出了两个文件

这个就是我们之后要配置的文件,先不用管。

4、完善目录结构

首先我们在src/main文件夹下创建java文件夹,你会发现这个文件夹不能创建java的类文件和package,别急,先把必须的文件夹全部创建好。请按照下图创建文件夹

然后在module中选择设置各个模块,其中java文件夹是 sources,test是Test,改完之后,点ok,文件夹会变色,那就成功了。

然后仔仔java文件夹中添加需要的包。最后的完整目录如下

这样我们配置前的工作就完成了,接下来就是对springmvc进行配置。我把两种配置的方法分成两部分,以供参考。

5、基于XML 的配置

5.1、配置web.xml

idea创建的web.xml这个文件版本比较低,所以我们要找一个新的。把我的文件直接全部覆盖复制进去就可以了。

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

version="3.1">

Archetype Created Web Application

index.jsp

springMVC

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

/WEB-INF/dispatcher-servlet.xml

1

true

springMVC

/

contextConfigLocation

/WEB-INF/applicationContext.xml

org.springframework.web.context.ContextLoaderListener

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

version="3.1">

Archetype Created Web Application

index.jsp

springMVC

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

/WEB-INF/dispatcher-servlet.xml

1

true

springMVC

/

contextConfigLocation

/WEB-INF/applicationContext.xml

org.springframework.web.context.ContextLoaderListener

上面代码的意思主要就是创建一个中央的控制器,都有简单的注释,如果有需要,请自行百度。

5.2 配置dispatcher-servlet.xml

这个文件负责mvc的配置。

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc=xYHamUKS"http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc=xYHamUKS"http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd">

直接复制即可,都有注释,如果有需要,请自行百度。

5.3、 配置applicationContext.xml

其实这个文件没什么好配置的,这个文件主要负责一些非mvc组件(或者其他组件)的配置,暂时没有,所以是空的,但你也可以扫描一下。

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">

5.4、 测试

三个配置文件配置好之后,就可以测试了。首先在controller文件夹下创建一个IndexController,代码如下:

@Controller

@RequestMapping("/home")

public class IndexController {

@RequestMapping("/index")

public String index() {

return "index";

}

}

views文件夹下创建index.jsp,statics/css/下创建test.css

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

"/>

Spring MVC based on XML config success!

p

{

background-color: brown;

font-family: "Courier New";

font-size:100px;

}

接下来配置tomcat,如果没有,去官网下载tomcat7以上的版本。

右上角

然后选择tomcat

配置相关信息

还有deployment

选择第二个

这里的名称和项目名一样。

然后点击ok完成。

最后运行tomcat,在浏览器输入http://localhost:8080/Demo/home/index 即可。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:接口测试用例分层设置(接口测试用例怎么设计的)
下一篇:JAVA通过HttpClient发送HTTP请求的方法示例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~