SpringMVC 的相关知识梳理

作者: 右耳菌 | 来源:发表于2022-05-17 23:09 被阅读0次
1. SpringMVC的知识脉络

2. SpringMVC的简单使用
  • SpringMVC的简单使用


    SpringMVC的简单使用
  • SpringMVC的操作相关参考

1、spring、SpringMVC、servlet的依赖

    <!--Spring的核心依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.1.6.RELEASE</version>
    </dependency>
    <!--SpringMVC-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.1.6.RELEASE</version>
    </dependency>
    <!--spring-web-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.1.6.RELEASE</version>
    </dependency>
    <!--servlet相关依赖-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
    <!--jsp相关依赖-->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.3.3</version>
      <scope>provided</scope>
    </dependency>

2、springmvc.xml文件的约束

<?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:p="http://www.springframework.org/schema/p"
       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-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

</beans>

3、jackson的依赖(在使用@ResponseBody 的情况下需要引入以下内容)
如果用2.75以下的版本,就有可能出现兼容性问题

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.5</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.5</version>
    </dependency>
3. SpringMVC+FreeMarker完成业务操作
  • SpringMVC+FreeMarker的整合


    SpringMVC+FreeMarker的整合的操作步骤
  1. 在上方的SpringMVC项目中加入FreeMarker的依赖
    <!-- 添加freemarker的相关依赖 -->
    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.23</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>3.2.9.RELEASE</version>
    </dependency>
  1. 配置springmvc.xml文件中的FreeMarker相关配置
<!-- 配置Freemarker属性文件路径 -->
    <bean id="freemarkerConfiguration"
          class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="location" value="classpath:freemarker.properties" />
    </bean>
    <!-- 配置freeMarker模板加载地址 -->
    <bean id="freemarkerConfig"
          class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <!-- 视图解析器在/WEB-INF/ftl/路径下扫描视图文件 -->
        <property name="templateLoaderPath" value="/WEB-INF/ftl/" />
        <!--设置一些常用的全局变量-->
        <property name="freemarkerVariables">
            <map>
                <entry key="xml_escape" value-ref="fmXmlEscape" />
            </map>
        </property>
    </bean>
    <!-- 对给定的模板片段执行XML转义-->
    <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />
    <!-- 配置freeMarker视图解析器 -->
    <bean id="freemakerViewResolver"
          class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="viewClass"
                  value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
        <!-- 扫描路径內所有以ftl結尾的文件 -->
        <property name="viewNames">
            <array>
                <value>*.ftl</value>
            </array>
        </property>
        <property name="contentType" value="text/html; charset=UTF-8" />
        <property name="exposeRequestAttributes" value="true" />
        <property name="exposeSessionAttributes" value="true" />
        <property name="exposeSpringMacroHelpers" value="true" />
        <property name="requestContextAttribute" value="request" />
        <!-- 给视图解析器配置优先級,你可以给之前jsp视图解析器的值配为2 -->
        <property name="order" value="1" />
    </bean>
  1. 创建freemarker.properties
#设置标签类型([],<>)
tag_syntax=auto_detect
#模板缓存时间
template_update_delay=2
#默认编码
default_encoding=UTF-8
#输出编码
output_encoding=UTF-8
#本地化
locale=zh_CN
#Date格式化
date_format=yyyy-MM-dd
#Time格式化
time_format=HH:mm:ss
#Datetime格式化
datetime_format=yyyy-MM-dd HH:mm:ss
  1. 创建helloftl.ftl
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    Hello, ${name}!!!
</body>
</html>
  1. 创建HelloFtlController
package controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Author: Neco
 * @Description:
 * @Date: create in 2022/5/17 23:03
 */
@Controller
public class HelloFtlController {
    @RequestMapping("helloftl")
    public String helloFtl(Model model) {
        String name = "Neco";
        model.addAttribute("name", name);
        return "helloftl.ftl";
    }
}
  1. 访问并测试结果
访问并测试结果
  1. 对于后续的类似列表和数据库的集成,不再过多描述

如果觉得有收获就点个赞吧,更多知识,请点击关注查看我的主页信息哦~

相关文章

网友评论

    本文标题:SpringMVC 的相关知识梳理

    本文链接:https://www.haomeiwen.com/subject/pvgturtx.html