其他分享
首页 > 其他分享> > SpringMVC的搭建

SpringMVC的搭建

作者:互联网

1.创建项目

2.引入依赖(pom.xml)

<!-- SpringMVC -->
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>5.3.9</version>
</dependency>
<!-- 日志 -->
<dependency>
   <groupId>ch.qos.logback</groupId>
   <artifactId>logback-classic</artifactId>
   <version>1.2.3</version>
</dependency>
<!-- ServletAPI -->
<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>3.1.0</version>
   <scope>provided</scope>
</dependency>
<!-- Spring5和Thymeleaf整合包 -->
<dependency>
   <groupId>org.thymeleaf</groupId>
   <artifactId>thymeleaf-spring5</artifactId>
   <version>3.0.12.RELEASE</version>
</dependency>

 3.在web.xml添加配置

<!-- 配置SpringMVC的前端控制器,对浏览器发送的请求同一进行处理 -->
<servlet>
  <servlet-name>SpringMVC</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!-- 配置SpringMVC配置文件的位置和名称 -->
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:springMVC.xml</param-value>
  </init-param>
  <!-- 将前端控制器DispatcherServlet的初始胡时间提前到服务器启动时 -->
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>SpringMVC</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

4.在Sources类型文件夹下创建一个类作为控制器,并添加注解@Controller 

5.springMVC.xml配置

在Resources类型文件夹下新建一个xml文件springMVC.xml,打开,找到<beans>标签,在其中添加相应配置,具体如下:

 <!-- 扫描组件 -->
    <!-- com.springmvc.controller:此为创建类的包路径 -->
    <context:component-scan base-package="com.springmvc.controller"></context:component-scan>
    <!-- 配置Thymeleaf视图解析器 -->
    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="order" value="1"/>
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                <property name="templateResolver">
                    <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
                        <!-- 视图前缀 -->
                        <!-- /WEB-INF/templates/:此为html文件存放位置 -->
                        <property name="prefix" value="/WEB-INF/templates/"/>
                        <!-- 视图后缀 -->
                        <property name="suffix" value=".html"/>
                        <property name="templateMode" value="HTML5"/>
                        <property name="characterEncoding" value="UTF-8" />
                    </bean>
                </property>
            </bean>
        </property>
    </bean>

6.测试

打开控制器类,添加处理请求方法:

     @RequestMapping(value = "/")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView();
        mav.addObject("test", "Hello SpringMVC");
        //设置视图,实现页面跳转
        mav.setViewName("index");
        return mav;
    }

结果:

 

 

 

 

 

标签:xml,mav,SpringMVC,ModelAndView,org,servlet,搭建
来源: https://blog.csdn.net/CSDNzc007/article/details/120595605