java springmvc maven_Javaweb学习:Maven下使用SpringMVC

前言

对于Java web应用来说,SpringMVC已经越来越流行。这里就对SpringMVC环境的搭建做一个记录。

创建Maven Java web项目

这里可以参考以前的文章,这里就不多说了。

创建目录

1、 在 src/main/ 下创建 java 和 resources 目录。创建好的目录,程序是不识别的,我们需要进行一下配置:

7803b5bac36e132f37672a392959f889.png

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 废弃,该目录下的代码文件是不具备代码检查和智能提示等常规代码功能。

可以在 hellotest.iml 中看到目录被引入,可以被识别了。

2、 添加依赖库

可以开始在 Maven 提供的 pom 文件(pom.xml) 添加我们spring的依赖,设置 pom.xml 前,需要到项目设置中 Maven autoload 勾选,否则无法自动下载 maven 依赖库(这是Intellij的坑)。

547e3e366b5a01bf503aaf459202359e.png

在 pom.xml 添加 dependencies, plugins,以下是 pom.xml 设置后的完整文件:

4.0.0com.zhl.hellotesthellotestwar1.0.0-SNAPSHOThellotest Maven Webapphttp://maven.apache.orgUTF-84.3.8.RELEASEjunitjunit3.8.1testorg.springframeworkspring-context${spring.version}org.springframeworkspring-orm${spring.version}org.springframeworkspring-context-support${spring.version}org.springframeworkspring-test${spring.version}org.springframeworkspring-core${spring.version}org.springframeworkspring-web${spring.version}org.springframeworkspring-webmvc${spring.version}org.springframeworkspring-aop${spring.version}org.springframeworkspring-beans${spring.version}hellotest

添加完成后,系统会自动下载所列出的依赖包,如果没有自动下载,可以在pom.xml文件中右键,选择Maven->Reimport载入。如下图:

28bfa6314b9658835652c378bda6b714.png

3、 进行SpringMVC配置

* 首先在resource目录下新建一个spring的配置文件springmvc.xml,文件图片和内容如下:

3ec02bd7c85f1780a31ab74b4f556428.png

注意:如果没有加下面的内容,会导致错误:

xmlns:context="http://www.springframework.org/schema/context"http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

在web.xml配置前端控制器,并加载springmvc.xml的配置文件,内容如下:

Archetype Created Web ApplicationSpringMVCorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:springmvc.xmlSpringMVC/

至此,文件配置完毕,下面我们用注解写一个方法测试一下,测试类放在springmvc.xml配置的扫描的包中,此处包名要与配置文件中扫描的包名一致。

创建包com.zhl.hellotest.controller,java文件TestController。

8a6335c917ce834d2cc104ff75e0bfcc.png

TestController内容如下:

package com.zhl.hellotest.controller;/** 注意不加下面的import,会导致出现找不到符号 Controller等的错误 */import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;/*** Created by zhl on 2017/6/5.*/@Controllerpublic class TestController {@RequestMapping(value = "/admin")@ResponseBodypublic String testSpring(){return "My testSpring";}}

此代码需要说明一下:

注解 @Controller 表示这是一个控制器,当请求来时将会扫描是否有匹配的RequestMapping。

注解 @RequestMapping 表示映射的路由,这里表示的是 /admin。

注解 @ResponseBody 表示返回的响应数据。

运行

编译,会出现如下错误:

24a2af7ada23098dfe7d0ca85145266f.png

这里重新设置项目的java版本。

5881a46fd66f449680f0e7170b2576bf.png

059f86161d1f090019fa01fb0630ebaa.png

点击运行,打开浏览器后输入http://localhost:8080/admin,可以看到 My testSpring。

469b14b96b5558d0e4cf1427d8db6e74.png

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值