好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

SpringMVC的工程搭建步骤实现

一、创建项目

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

右键项目名选择add framework support

2、选择web application

3、配置springmvc

pom.xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<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后再加入如下图所示代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<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>

二、配置核心文件

1、

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<?xml version= "1.0" encoding= "utf-8" ?>

<beans xmlns= "http://HdhCmsTestspringframework.org/schema/beans"

  xmlns:xsi= "http://HdhCmsTestw3.org/2001/xmlschema-instance"

  xmlns:context= "http://HdhCmsTestspringframework.org/schema/context"

  xmlns:mvc= "http://HdhCmsTestspringframework.org/schema/mvc"

  xsi:schemalocation="http: //HdhCmsTestspringframework.org/schema/beans

   http: //HdhCmsTestspringframework.org/schema/beans/spring-beans.xsd

   http: //HdhCmsTestspringframework.org/schema/context

   https: //HdhCmsTestspringframework.org/schema/context/spring-context.xsd

   http: //HdhCmsTestspringframework.org/schema/mvc

   https: //HdhCmsTestspringframework.org/schema/mvc/spring-mvc.xsd

  ">

 

<!-- bean definitions here -->

2、添加springmvc配置内容

?

1

2

3

4

5

6

7

8

9

10

11

12

13

<!-- 自动扫描包,让指定包下的注解生效,由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类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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

?

1

2

3

4

5

6

7

8

9

<%@ page contenttype= "text/html;charset=utf-8" language= "java" %>

  <html>

  <head>

<title>title</title>

  </head>

  <body>

${msg}

  </body>

  </html>

三、web.xml

1、配置前端控制器

?

1

2

3

4

<!-- 配置前端控制器 -->

<servlet>

<servlet-name>springmvc</servlet-name>

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

2、配置初始化参数

?

1

2

3

4

5

<!-- 配置初始化参数 -->

<init-param>

<param-name>contextconfiglocation</param-name>

<param-value>classpath:applicationcontext.xml</param-value>

</init-param>

3、设置启动级别

?

1

2

3

<!-- 设置启动级别 -->

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

</servlet>

4、设置springmvc拦截请求

?

1

2

3

4

5

<!-- 设置springmvc拦截请求 -->

<servlet-mapping>

  <servlet-name>springmvc</servlet-name>

  <url-pattern> / </url-pattern> <!--拦截除.jsp的请求-->

</servlet-mapping>

5、乱码过滤

?

1

2

3

4

5

6

7

8

9

10

11

12

13

<!-- 乱码过滤 -->

<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

删除默认的包

点ok→ok

四、配置tomcat

1、点击 add configuration… 进入运行配置框

2、点 + 选择tomcat server 下的 local

3、点击 configure 选择我们自己的tomcat

五、运行tomcat

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

外链:https://mvnrepository测试数据/

到此这篇关于springmvc的工程搭建步骤实现的文章就介绍到这了,更多相关springmvc的工程搭建 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/Yudjk/article/details/115441210

查看更多关于SpringMVC的工程搭建步骤实现的详细内容...

  阅读:24次