html解析Model与ModelandView测试

解析

关于html解析这个的话我使用的是thymeleaf解析模板,这里贴上ssm引入thymeleaf的代码

Maven导入

    <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf</artifactId>
      <version>3.0.11.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf-spring5</artifactId>
      <version>3.0.11.RELEASE</version>

视图解析器:

注意,有这个解析器以后将jsp的解析器注释掉

    <bean id="templateResolver"
          class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".html"/>
        <property name="templateMode" value="HTML5"/>
    </bean>
    <bean id="templateEngine"
          class="org.thymeleaf.spring5.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver"/>
    </bean>

    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine"/>
        <property name="characterEncoding" value="UTF-8"/>
    </bean>

测试Demo

后端
@Controller
public class HelloController {
    @RequestMapping(path = "/hello.do")
    public String sayHello(Model model){
        System.out.println("hello springmvc");
        model.addAttribute("hello","pyx");
        return "success";
    }
}
前端
<!DOCTYPE html>
<html lang="zh-CN"
    xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="JS/model.js" type="text/javascript"></script>
    <script src="JS/jquery-3.5.1.js" type="text/javascript"></script>
</head>
<body>
<div th:text="${hello}">你好
</div>
</body>
</html>
显示

在这里插入图片描述

正题

强推大佬文章
一下全是从该文章我了解的总结

Model与ModelMap

Model与ModelMap:就是一个模型,他在前端向后端请求时自动建立。
对于这个模型我们可以通过addAttribute()设置属性。

ModelandView

他有一个模型与视图,所以我们可以对其设置视图,也可以在其里面添加属性,在稍后的视图解析器里面,对其自动解析后返回新视图。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值