IntelliJ IDEA搭建springmvc项目图文介绍
前一段时间有网友说不知道如何使用maven来创建一个java web项目,在这里,我就使用IntelliJ IDEA来搭建一个spring MVC的maven项目。如果对我的文章有兴趣,欢迎订阅我的头条号:一点热。
1、实验前提
首先得安装好IntelliJ IDEA
这里我就不做介绍,安装软件都是开发入门必备的。
2、新建项目
打开idea,通过new->project,会弹出如下的信息
由于我们是要创建一个maven的项目,所以我们需要选择maven这个选项,进去后选择SDK,ARCHETYPE
然后点击下一步
输入groupid和artifactid,这里使用包名和项目名字
点击下一步,就是maven的信息,我们这里保持默认就好,如果没有安装maven的就需要我们手动配置
最后我们需要填写一下项目的名字,如下图。
点击finish,至此,我们创建项目的步骤就完成了。这时会进入idea,可能需要一定的时间,这个是由于要下载一些包,所以大家耐心的等待。
当maven 加载完毕会有提示,我们的项目会生成相应的包,如图
与此同时,右上角还会提示我们need to be imported,我们选中,这样倒是就会自动导入。
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
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文件
输入名字mvc-dispatcher-servlet
点击左上角的configure
点击ok
6、建立源码包java
在main目录下,右键创建目录,如下图所示
输入java
这是项目会多出一个java的包,我们需要把它变成源码目录,如下图所示:
7、添加controller的包和文件
我这里以com.yeehot.controller包为例子,创建一个首页的IndexContrller
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。
进入界面后,点击左上角的“+”,滑动到底部的tomcat server,选择local。
修改名字,配置tomcat的路径,如下图,由于我这里之前配置有tomcat8了,所以大家可以按照自己的时间情况添加
我配置了两个tomcat。
配置好后,还需要对我们的程序压缩包添加tomcat里面,这里列举两种方法
方法1:
方法二:
10、运行程序,点击绿色的按钮,启动tomcat
但编译成功后,会自动启动浏览器,打开
localhost:8080
当出现这样的结果的时候,说明已经配置成功了。
今天的课程就说到这里,欢迎继续关注我的头条号:一点热
欢迎大家收藏与转发,如果转载到其它网站,请与我联系.