转--【工具使用】IntelliJ IDEA 2017 搭建spring mvc

来源:互联网 发布:unity3d下载安装教程 编辑:程序博客网 时间:2024/05/20 03:40

背景

用idea搭建spring mvc环境,网上有很多教程,但是普遍都不是很详细,容易有遗漏。也没有太多解释原因。我自己总结了一个。并会附上一定的解释。

具体步骤

新建项目

我使用的版本是IntelliJ 15.0.3 
idea版本 
打开IntelliJ,新建项目 
新建项目
然后选择spring mvc 
选择springMvc
选择目录,这个可以自己随便选 
本地选择
新建好之后,可以看到自带的lib文件夹,idea会自动配置常用的,不过一般来说,我们都是用maven来做管理。 
lib
选中项目文件夹,呼出菜单,选择添加框架。 
添加maven框架
在弹出的菜单中选择maven 
选择maven框架
这个时候可以看到项目目录改变了,多了Java主目录和测试目录 
新的目录结构

配置pom文件

先配置pom文件

<?xml version="1.0" encoding="UTF-8"?><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/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>groupId</groupId>    <artifactId>LearnSpringMvc</artifactId>    <version>1.0-SNAPSHOT</version>    <!-- 打包方法 -->    <packaging>war</packaging>    <dependencies>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>4.2.6.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-beans</artifactId>            <version>4.2.6.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-aspects</artifactId>            <version>4.2.6.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>4.2.6.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>4.2.6.RELEASE</version>        </dependency>    </dependencies></project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

注意一定要使用war的打包方式 
因为maven默认的打包方式不会放到/WEB-INF/lib里头,但是tomcat启动的时候找类会在WEB-INF/lib,如果不用war打包方式,会不停地抛 
classNotFound异常的。

添加controller

添加controller类

@Controller@RequestMapping(value = "/main")public class MainController {    @RequestMapping(method = RequestMethod.GET)    @ResponseBody    public String welcome(){        return "test Web";    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

使用controller注解,表示该类为MVC模型中的Controller层。 
使用RequestMapping注解,表明该Controller的主进入目录。 
method表示请求的方法,@ResponseBody表明返回是一段数据。

添加spring配置文件

在resource目录中添加mvc-dispatcher-servlet.xml文件

<?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:context="http://www.springframework.org/schema/context"       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">    <context:component-scan base-package="Controller"/></beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

很多教程中会有配置项目<’mvc:annotation-driven’/>说这个是启动bean扫描的驱动,其实<’context:component-scan base-package=”Controller”/’>就已经开启扫描了,扫描选择添加Controller注解的包就好了。

配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"         xmlns:xsi="http://www.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">    <!-- 添加展示名称 -->    <display-name>SpringMvcLearn</display-name>    <servlet>        <servlet-name>mvc-dispatcher</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:mvc-dispatcher-servlet.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>mvc-dispatcher</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

servlet配置了spring的上下文,<’param-value’>指定spring配置文件。servlet-mapping 指定了spring mvc映射的目录。 
目前的目录结构如下 
目前的结构

配置project

在file菜单中呼出project structure选项 
呼出project 
在弹出的框中选择jdk的版本,我这里用的是java6 
然后配置module,也选择6.0。 
点击Problems,看看有没有什么问题,然后按推荐的fix就好了。 
module
project

配置tomcat

下载tomcat

先上Apache Tomcat官网,下载tomcat,推荐使用tomcat7,下载之后cd到tomcat目录下的bin文件夹。因为一般来说mac下默认从zip中解压的文件都是没有运行权限的。所以给这里的catalina.sh附上运行权限。不过我一般比较懒,为了方便我会给bin上所有的文件都附上运行权限,一般本地开发无所谓的,如果商业化运作,就另当别论了。

chmod 777 *
  • 1
  • 1

在idea中配置tomcat

在右上角打开运行配置 
编辑运行配置 
添加配置,选择tomcat local 
添加tomcatLocal 
选择配置tomcat配置 
配置tomcat
选择下载tomcat的目录 
add tomcat 
我推荐使用7. 
配置deployment 
配置deploy
选择exploded,注意一定要是exploded的 
war

关于打包的说明

一般来说tomcat会在项目的主目录下搜索web.xml文件,如果启动失败,可以检查target中是否有web.xml文件。如果没有 
没有web.xml文件的情况 
这个时候,可以进入Project Structure 
添加web.xml文件
将web.xml引入到WEB-INF的目录下就好了 
添加web.xml文件地址
注意要在WEB-INF目录下,不要搞错了 
web.xml位置

运行

到此为止配置就算完成了,然后点击运行 
运行 
这个时候浏览器打开以下链接

http://localhost:8080/main
  • 1
  • 1

就可以看到运行结果test Web了。

阅读全文
1 0
原创粉丝点击