温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么搭建一个SpringMVC工程

发布时间:2021-04-07 18:01:37 来源:亿速云 阅读:325 作者:Leah 栏目:开发技术

怎么搭建一个SpringMVC工程?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

一、创建项目

1、新建一个项目名为:springmvc-demo-yuyongqing

右键项目名选择Add Framework Support

怎么搭建一个SpringMVC工程

2、选择Web Application

怎么搭建一个SpringMVC工程

3、配置SpringMVC

pom.xml

<dependencies>
<dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.13.2</version>
 <scope>test</scope>
</dependency>
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-webmvc</artifactId>
 <version>5.2.13.RELEASE</version>
</dependency>
<dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>servlet-api</artifactId>
 <version>2.5</version>
</dependency>
<dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>javax.servlet-api</artifactId>
 <version>4.0.1</version>
 <scope>provided</scope>
</dependency>
</dependencies>

刷新maven后再加入如下图所示代码

怎么搭建一个SpringMVC工程

<build>
<resources>
 <resource>
  <directory>src/main/java</directory>
  <includes>
   <include>**/*.properties</include>
   <include>**/*.xml</include>
  </includes>
  <filtering>false</filtering>
 </resource>
 <resource>
  <directory>src/main/resources</directory>
  <includes>
   <include>**/*.properties</include>
   <include>**/*.xml</include>
  </includes>
  <filtering>false</filtering>
 </resource>
</resources>
</build>

二、配置核心文件

怎么搭建一个SpringMVC工程

1、

<?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"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context
  https://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/mvc
  https://www.springframework.org/schema/mvc/spring-mvc.xsd
 ">

<!-- bean definitions here -->

2、添加SpringMVC配置内容

怎么搭建一个SpringMVC工程

 <!-- 自动扫描包,让指定包下的注解生效,由IOC容器统一管理 -->
  <context:component-scan base-package="controller"/>
  
<!-- 1加载注解驱动 -->
<mvc:annotation-driven/>
 <!-- 2静态资源过滤 -->
<mvc:default-servlet-handler/>
<!-- 3视图解析器 -->
<bean id="internalResourceViewResolver" class=
 "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

3、Controller层

新建一个HelloController类

怎么搭建一个SpringMVC工程

 package controller;

@Controller
public class HelloController {

@RequestMapping("/hello")
public String hello(Model model){
 // Model 封装数据
 model.addAttribute("msg","HELLO MY FIRST SPRING MVC PROJECT");

 // 返回的字符串就是视图的名字 会被视图解析器处理
 return "hello";
 }
}

4、JSP

在JSP包下新建hello.jsp

怎么搭建一个SpringMVC工程

 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 <html>
 <head>
<title>Title</title>
 </head>
 <body>
${msg}
 </body>
 </html>

三、web.xml

1、配置前端控制器

<!-- 配置前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

2、配置初始化参数

<!-- 配置初始化参数 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationcontext.xml</param-value>
</init-param>

3、设置启动级别

<!-- 设置启动级别 -->
<load-on-startup>1</load-on-startup>
</servlet>

4、设置SpringMVC拦截请求

<!-- 设置SpringMVC拦截请求 -->
<servlet-mapping>
 <servlet-name>springmvc</servlet-name>
 <url-pattern> / </url-pattern> <!--拦截除.jsp的请求-->
</servlet-mapping>

5、乱码过滤

 <!-- 乱码过滤 -->
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

6、运行web

打包
File→Project Structure

怎么搭建一个SpringMVC工程

删除默认的包

怎么搭建一个SpringMVC工程

怎么搭建一个SpringMVC工程

点ok→ok

怎么搭建一个SpringMVC工程

四、配置TomCat

1、点击 Add Configuration… 进入运行配置框

怎么搭建一个SpringMVC工程

2、点 + 选择Tomcat Server 下的 Local

怎么搭建一个SpringMVC工程

3、点击 Configure 选择我们自己的TomCat

怎么搭建一个SpringMVC工程

怎么搭建一个SpringMVC工程

五、运行TomCat

怎么搭建一个SpringMVC工程

在浏览器输入http://localhost:8080/hello

怎么搭建一个SpringMVC工程

怎么搭建一个SpringMVC工程

关于怎么搭建一个SpringMVC工程问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI