IntelliJ IDEA搭建springmvc项目图文介绍

前一段时间有网友说不知道如何使用maven来创建一个java web项目,在这里,我就使用IntelliJ IDEA来搭建一个spring MVC的maven项目。如果对我的文章有兴趣,欢迎订阅我的头条号:一点热。

1、实验前提

首先得安装好IntelliJ IDEA

这里我就不做介绍,安装软件都是开发入门必备的。

IntelliJ IDEA搭建springmvc项目图文介绍

2、新建项目

打开idea,通过new->project,会弹出如下的信息

IntelliJ IDEA搭建springmvc项目图文介绍由于我们是要创建一个maven的项目,所以我们需要选择maven这个选项,进去后选择SDK,ARCHETYPE

IntelliJ IDEA搭建springmvc项目图文介绍

然后点击下一步

输入groupid和artifactid,这里使用包名和项目名字

IntelliJ IDEA搭建springmvc项目图文介绍点击下一步,就是maven的信息,我们这里保持默认就好,如果没有安装maven的就需要我们手动配置

IntelliJ IDEA搭建springmvc项目图文介绍最后我们需要填写一下项目的名字,如下图。

IntelliJ IDEA搭建springmvc项目图文介绍点击finish,至此,我们创建项目的步骤就完成了。这时会进入idea,可能需要一定的时间,这个是由于要下载一些包,所以大家耐心的等待。

IntelliJ IDEA搭建springmvc项目图文介绍

当maven 加载完毕会有提示,我们的项目会生成相应的包,如图

IntelliJ IDEA搭建springmvc项目图文介绍

IntelliJ IDEA搭建springmvc项目图文介绍

与此同时,右上角还会提示我们need to be imported,我们选中,这样倒是就会自动导入。



IntelliJ IDEA搭建springmvc项目图文介绍

3、配置maven,下载springmvc相应的包

打开pom.xml,输入版本信息

<properties>

<spring.version>4.2.5.RELEASE</spring.version>

</properties>

然后在填写下载的spring版本

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>${spring.version}</version>

</dependency>

完整的pom.xml如下

<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.yeehot</groupId>

<artifactId>springidea</artifactId>

<packaging>war</packaging>

<version>1.0-SNAPSHOT</version>

<name>springidea Maven Webapp</name>

<url>http://maven.apache.org</url>

<properties>

<spring.version>4.2.5.RELEASE</spring.version>

</properties>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>${spring.version}</version>

</dependency>

</dependencies>

<build>

<finalName>springidea</finalName>

</build>

</project>

这里可能需要一定的时间,当下载这些包后,可以在下图的位置看到相应的jar

IntelliJ IDEA搭建springmvc项目图文介绍

4、修改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>springidea</display-name>

<servlet>

<servlet-name>mvc-dispatcher</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>mvc-dispatcher</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

<filter>

<filter-name>encodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

<init-param>

<param-name>forceEncoding</param-name>

<param-value>true</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>encodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

</web-app>

5、根据web.xml的servlet-name添加mvc-dispatcher-servlet.xml

在WEB-INF,新建mvc-dispatcher-servlet.xml文件

IntelliJ IDEA搭建springmvc项目图文介绍输入名字mvc-dispatcher-servlet

IntelliJ IDEA搭建springmvc项目图文介绍

点击左上角的configure

IntelliJ IDEA搭建springmvc项目图文介绍点击ok


IntelliJ IDEA搭建springmvc项目图文介绍

6、建立源码包java

在main目录下,右键创建目录,如下图所示

IntelliJ IDEA搭建springmvc项目图文介绍

输入java

IntelliJ IDEA搭建springmvc项目图文介绍这是项目会多出一个java的包,我们需要把它变成源码目录,如下图所示:

IntelliJ IDEA搭建springmvc项目图文介绍

7、添加controller的包和文件

我这里以com.yeehot.controller包为例子,创建一个首页的IndexContrller

IntelliJ IDEA搭建springmvc项目图文介绍

IntelliJ IDEA搭建springmvc项目图文介绍

IndexController的源码如下,主要就是跳转到index.jsp

/**

* Created by yeehot on 16/7/7.

* weisite www.yeehot.com

*/

@Controller

public class IndexController {

@RequestMapping(value = "/", method = RequestMethod.GET)

public String index() {

System.out.print("hello");

return "index";

}

}

8、修改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:mvc="http://www.springframework.org/schema/mvc"

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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->

<mvc:annotation-driven />

<!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->

<context:component-scan base-package="com.yeehot.*" />

<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->

<!--static resource-->

<mvc:resources mapping="/index.jsp*" location="/index.jsp"/>

<mvc:resources mapping="/resources/**" location="/resources/"/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>

<property name="prefix" value="/WEB-INF/jsp/"/>

<property name="suffix" value=".jsp"/>

</bean>

</beans>

9、配置tomcat

点击右上角的箭头,然后点击edit。

IntelliJ IDEA搭建springmvc项目图文介绍

进入界面后,点击左上角的“+”,滑动到底部的tomcat server,选择local。

IntelliJ IDEA搭建springmvc项目图文介绍

修改名字,配置tomcat的路径,如下图,由于我这里之前配置有tomcat8了,所以大家可以按照自己的时间情况添加

IntelliJ IDEA搭建springmvc项目图文介绍

我配置了两个tomcat。

IntelliJ IDEA搭建springmvc项目图文介绍

配置好后,还需要对我们的程序压缩包添加tomcat里面,这里列举两种方法

方法1:

IntelliJ IDEA搭建springmvc项目图文介绍

IntelliJ IDEA搭建springmvc项目图文介绍

方法二:

IntelliJ IDEA搭建springmvc项目图文介绍

IntelliJ IDEA搭建springmvc项目图文介绍

10、运行程序,点击绿色的按钮,启动tomcat

IntelliJ IDEA搭建springmvc项目图文介绍

但编译成功后,会自动启动浏览器,打开

localhost:8080

IntelliJ IDEA搭建springmvc项目图文介绍

当出现这样的结果的时候,说明已经配置成功了。

今天的课程就说到这里,欢迎继续关注我的头条号:一点热

欢迎大家收藏与转发,如果转载到其它网站,请与我联系.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值