解析
关于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
他有一个模型与视图,所以我们可以对其设置视图,也可以在其里面添加属性,在稍后的视图解析器里面,对其自动解析后返回新视图。