Skip to content

Commit 5c328d3

Browse files
author
Songyu
committed
增加文章idea搭建springmvc及相关图片资源
1 parent d6742ae commit 5c328d3

File tree

8 files changed

+204
-0
lines changed

8 files changed

+204
-0
lines changed
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
---
2+
layout: post
3+
title: "idea搭建springMVC"
4+
date: 2020-11-02 20:01:24 +0800
5+
tags: java
6+
description:
7+
---
8+
9+
记录一下maven搭建springMVC的过程
10+
11+
## 项目搭建
12+
13+
首先点击File-->New-->Project进入到项目创建窗口
14+
15+
![](/images/2020-11-02-1.jpg)
16+
17+
选择如下图的选项,点击Next按钮进入到下一步
18+
19+
![](/images/2020-11-02-2.jpg)
20+
21+
填写项目名称和本地保存目录,以及包路径、发布名称、版本号信息,设置完成后,点击__Finish__完成设置
22+
23+
![](/images/2020-11-02-3.png)
24+
25+
完成设置后,项目目录如下所示,只有webapp目录,java、resources、test目录需要我们自己进行创建
26+
27+
![](/images/2020-11-02-4.jpg)
28+
29+
在创建目录时候,idea会给出我们缺少目录的提示,直接创建就好,同时也会看到刚刚创建的java目录,显示出maven项目的蓝色
30+
31+
![](/images/2020-11-02-5.jpg)
32+
33+
34+
35+
## Jar包导入
36+
37+
项目建立完成后,导入springMVC需要的jar包。基本的jar包如下:
38+
39+
{% highlight xml %}
40+
<properties>
41+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
42+
<maven.compiler.source>1.7</maven.compiler.source>
43+
<maven.compiler.target>1.7</maven.compiler.target>
44+
<spring.version>5.2.8.RELEASE</spring.version>
45+
<!-- mysql驱动版本对应MySQL5.7 -->
46+
<mysql.version>5.1.47</mysql.version>
47+
</properties>
48+
<dependencies>
49+
<!-- spring相关 -->
50+
<dependency>
51+
<groupId>org.springframework</groupId>
52+
<artifactId>spring-context</artifactId>
53+
<version>${spring.version}</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.springframework</groupId>
57+
<artifactId>spring-web</artifactId>
58+
<version>${spring.version}</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.springframework</groupId>
62+
<artifactId>spring-webmvc</artifactId>
63+
<version>${spring.version}</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.springframework</groupId>
67+
<artifactId>spring-jdbc</artifactId>
68+
<version>${spring.version}</version>
69+
</dependency>
70+
<!-- mysql驱动 -->
71+
<dependency>
72+
<groupId>mysql</groupId>
73+
<artifactId>mysql-connector-java</artifactId>
74+
<version>${mysql.version}</version>
75+
</dependency>
76+
<!-- servlet -->
77+
<dependency>
78+
<groupId>javax.servlet</groupId>
79+
<artifactId>javax.servlet-api</artifactId>
80+
<version>3.1.0</version>
81+
<scope>provided</scope>
82+
</dependency>
83+
</dependencies>
84+
{% endhighlight %}
85+
86+
__PS:我们可以在idea中设置maven自动导入xml中的jar包,顺便记得安装本地的maven,设置本地包仓库目录和setting.xml配置,把默认的包源换成国内源,比如下面的阿里的maven源__
87+
88+
{% highlight xml %}
89+
<mirror>
90+
<id>nexus-aliyun</id>
91+
<mirrorOf>*</mirrorOf>
92+
<name>Nexus aliyun</name>
93+
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
94+
</mirror>
95+
{% endhighlight %}
96+
97+
98+
## 文件配置
99+
100+
由于resources目录是我们手动进行创建的,所以我们需要手动创建Spring的监听器配置文件(applicationContext.xml)和Spring的中央控制器配置文件(springmvc.xml)。同时还要在webapp下的web.xml文件中,进行两个配置文件的引入和配置。
101+
102+
### applicationContext.xml
103+
104+
> 其中__base-package__是我们的包路径,后面星号,表示路径下的包都遵循这个配置
105+
106+
{% highlight xml %}
107+
<!-- spring容器扫描 -->
108+
<context:component-scan base-package="com.bc.*">
109+
<!-- 不扫描spring mvc的controller -->
110+
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
111+
</context:component-scan>
112+
{% endhighlight %}
113+
114+
### springmvc.xml
115+
116+
{% highlight xml %}
117+
<mvc:annotation-driven/>
118+
<!--spring mvc扫描controller不扫描service-->
119+
<context:component-scan base-package="com.bc.apps.*">
120+
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
121+
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
122+
</context:component-scan>
123+
{% endhighlight %}
124+
125+
配置完成后,前去__web.xml__进行配置
126+
127+
{% highlight xml %}
128+
<?xml version="1.0" encoding="UTF-8"?>
129+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
130+
xmlns="http://java.sun.com/xml/ns/javaee"
131+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
132+
version="3.0">
133+
<display-name>Md</display-name>
134+
<!-- 监听器配置文件 -->
135+
<listener>
136+
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
137+
</listener>
138+
<context-param>
139+
<param-name>contextConfigLocation</param-name>
140+
<param-value>classpath:applicationContext.xml</param-value>
141+
</context-param>
142+
<!-- 配置过滤器 解决中文乱码问题 -->
143+
<filter>
144+
<filter-name>characterEncodingFilter</filter-name>
145+
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
146+
<init-param>
147+
<param-name>encoding</param-name>
148+
<param-value>UTF-8</param-value>
149+
</init-param>
150+
</filter>
151+
<filter-mapping>
152+
<filter-name>characterEncodingFilter</filter-name>
153+
<url-pattern>/*</url-pattern>
154+
</filter-mapping>
155+
<!-- SpringMVC的核心控制器 -->
156+
<servlet>
157+
<servlet-name>dispatcherServlet</servlet-name>
158+
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
159+
<init-param>
160+
<param-name>contextConfigLocation</param-name>
161+
<param-value>classpath:springmvc.xml</param-value>
162+
</init-param>
163+
<!-- 配置servlet启动时加载对象 -->
164+
<load-on-startup>1</load-on-startup>
165+
</servlet>
166+
<servlet-mapping>
167+
<servlet-name>dispatcherServlet</servlet-name>
168+
<url-pattern>/</url-pattern>
169+
</servlet-mapping>
170+
</web-app>
171+
{% endhighlight %}
172+
173+
### 配置完成
174+
175+
上述配置完成后,去java目录下创建测试代码,进行测试项目是否搭建完成。
176+
177+
{% highlight java %}
178+
@RequestMapping(value = "/")
179+
@RestController
180+
public class IndexController {
181+
182+
@GetMapping(value = "index")
183+
public String index(){
184+
return "Hello,Spring~";
185+
}
186+
187+
}
188+
{% endhighlight %}
189+
190+
对项目进行编译并启动,再地址栏中输入__http://domain/index__,网址中输出我们打印的文字,项目搭建成功!
191+
192+
![](/images/2020-11-02-6.jpg)
193+
194+
195+
196+
## 结尾
197+
198+
至此SpringMVC的初步搭建,已经完成,其实复杂程度到不是很高,只不过在配置和引入jar包的时候,稍微有一些多。
199+
200+
下一步,要进行视图解析器的配置,将使用[Thymeleaf][Thymeleaf]作为我们的视图解析器
201+
202+
![](/images/2020-11-02-7.jpg)
203+
204+
[Thymeleaf]:https://www.thymeleaf.org/

images/2020-11-02-1.jpg

116 KB
Loading

images/2020-11-02-2.jpg

76.4 KB
Loading

images/2020-11-02-3.png

31.7 KB
Loading

images/2020-11-02-4.jpg

143 KB
Loading

images/2020-11-02-5.jpg

131 KB
Loading

images/2020-11-02-6.jpg

8.22 KB
Loading

images/2020-11-02-7.jpg

10.9 KB
Loading

0 commit comments

Comments
 (0)