怎么搭建一个SpringMVC工程

  介绍

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

一、创建项目

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

右键项目名选择添加框架支持

怎么搭建一个SpringMVC工程

2,选择Web应用程序

怎么搭建一个SpringMVC工程

3,配置SpringMVC

砰的一声。xml

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

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

怎么搭建一个SpringMVC工程

& lt; build>   & lt; resources>   ,& lt; resource>   & lt;才能directory> src/主/java   & lt;才能includes>   ,,& lt; include> * */* .properties   ,,& lt; include> * */* .xml   & lt;才能/includes>   & lt;才能filtering> false</filtering>   ,& lt;/resource>   ,& lt; resource>   & lt;才能directory> src/主/resources   & lt;才能includes>   ,,& lt; include> * */* .properties   ,,& lt; include> * */* .xml   & lt;才能/includes>   & lt;才能filtering> false</filtering>   ,& lt;/resource>   & lt;/resources>   & lt;/build>

二,配置核心文件

怎么搭建一个SpringMVC工程

1,

& lt; ? xml  version=?.0“,编码=癠TF-8" ?比;   http://www.springframework.org/schema/beans" & lt; beans  xmlns=?;   ,xmlns: xsi=癶ttp://www.w3.org/2001/XMLSchema-instance"   ,xmlns:上下文=癶ttp://www.springframework.org/schema/context"   ,xmlns: mvc=癶ttp://www.springframework.org/schema/mvc"   xsi: schemaLocation=癶ttp://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   ,“在      & lt; !——, bean  definitions  here ——在

2,添加SpringMVC配置内容

怎么搭建一个SpringMVC工程

, & lt; !——,自动扫描包,让指定包下的注解生效,由国际奥委会容器统一管理,——比;   & lt;才能上下文:component-scan 基础包=癱ontroller"/比;   ,,   & lt; !——, 1加载注解驱动,——比;   & lt; mvc:注解驱动/比;   2,& lt; !——,静态资源过滤,——比;   & lt; mvc: default-servlet-handler/比;   & lt; !——, 3视图解析器,——比;   & lt; bean  id=癷nternalResourceViewResolver"类=,“org.springframework.web.servlet.view.InternalResourceViewResolver"比;   & lt; property  name=皃refix", value=https://www.yisu.com/zixun/"/web - inf/jsp/"/>   <属性名="后缀" value=" . jsp "/>   

3,控制器层

新建一个HelloController类

怎么搭建一个SpringMVC工程

, package 控制器;      @ controller   public  class  HelloController  {      @RequestMapping (“/hello")   public  String 你好(Model 模型){   ,//Model 封装数据   ,model.addAttribute (“msg",“HELLO  MY  FIRST  SPRING  MVC  PROJECT");      ,//返回的字符串就是视图的名字,会被视图解析器处理   ,return “hello";   ,}   }

怎么搭建一个SpringMVC工程