我们一起做网站,装修材料厂家哪家好,黑龙江网站建设公司,seo求职替代jsp
功能#xff1a;服务器渲染#xff08;就是将服务器的数据展示在网页上#xff09;
1、MVC概念
model 模型
javaBean(User/Book/Order...)
View视图
html 服务器的动态数据
Controller控制器
Servlet
MVC是在表述层开发运用的一种设计理念。主张把封装数据…替代jsp
功能服务器渲染就是将服务器的数据展示在网页上
1、MVC概念
model 模型
javaBean(User/Book/Order...)
View视图
html 服务器的动态数据
Controller控制器
Servlet
MVC是在表述层开发运用的一种设计理念。主张把封装数据的【模型】、显示用户界面的【视图】、协调调度的【控制器】分开。
好处
进一步实现各个组件之间的解耦让各个组件可以单独维护将视图分离出来后我们后端工程师和前端工程师的对接更方便
MVC和层架构之间关系 2、Thymeleaf的简介 概念 Thymeleaf是一款用于渲染 XML/XHTML/HTML5 内容的模板引擎。类似 JSP、Velocity、FreeMaker等他也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎。它的主要作用是在静态页面上渲染显示动态数据。 优势
SpringBoot官方推荐使用的视图模板技术和SpringBoot完美结合不经过服务器运算仍然可以直接查看原始数据对前端工程师更友好
!DOCTYPE html
html langen xmlns:thhttp://www.thymeleaf.org
headmeta charsetUTF-8titleTitle/title
/head
bodyp th:text${username}Original Value/p/body
/html
3、Thymeleaf的HelloWorld
①加入jar包 ②配置上下文路径
!-- 在上下文参数中配置视图前缀和视图后缀 --
context-paramparam-nameview-prefix/param-nameparam-value/WEB-INF/view//param-value
/context-param
context-paramparam-nameview-suffix/param-nameparam-value.html/param-value
/context-param
在web.xml中配置
③创建Servlet基类 ViewBaseServlet类
这个类复制粘贴过来将来使用框架后这些代码将被取代。
上面①②③步我们完成了配置工作然后
④创建index.html文件 ⑤index.html编写超链接访问Servlet
a hrefhello点击访问HelloServlet/a
⑥ 创建HelloServlet并在web.xml配置 servletservlet-nameHelloServlet/servlet-nameservlet-classcom.atguigu.servlet.HelloServlet/servlet-class/servletservlet-mappingservlet-nameHelloServlet/servlet-nameurl-pattern/hello/url-pattern/servlet-mapping
⑦修改Servlet让其继承ViewServlet并给请求域中设置参数
public class HelloServlet extends ViewBaseServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request,response);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//给响应跳转至admin.html,并且将msg的数据展示在网页上String msg 这是服务器的数据;//取到Thymeleaf让Themeleaf对页面做渲染操作request.setAttribute(msg,msg);this.processTemplate(admin,request,response);//原理也是转发}
}
⑧跳转到Thymeleaf页面
!DOCTYPE html
html langen xmlns:thhttp://thymeleaf.org
headmeta charsetUTF-8titleTitle/title
/head
body
h1admin/h1
h2 th:text${msg}放服务器传过来的msg数据/h2
/body
/html
admin.html页面结果 网页一开始访问到了index.html页面在点击超链接后程序进入到HelloServlet类再给请求域设置一个msg参数由于继承了ViewBaseServlet类且调用父类的processTemplate()方法将渲染好的网页msg参数转发给浏览器页面我们就看到了“这是服务器的数据”。
4、Thymeleaf的基本语法
①名称空间 thymeleaf所有的的表达是都是通过属性的方法添加th:表达式内容 位置html标签的属性上 xmlns:thhttp://www.thymeleaf.org ②修改标签的文本值双标签
th:text新值
案例h2 th:text${msg}放服务器传来的msg数据/h2
③修改标签的属性值单标签和双标签
th:属性名新值
案例input typetext th:value${msg} value这是原始值 /④解析url中的上下文路径 {/}
用途a.用在base标签上base href/day07_Thymeleaf_war_exploded123/ th:href{/}b.作为请求的路径a th:href{/root(id101,namejack,age20)}RootServlet02/a 如果你的网页想使用thymeleaf表达式的话必须经过Servlet然后再经过Thymeleaf进行渲染才可以 项目内所有的网页都需要thymeleaf渲染都需要过Servlet再过Thymeleaf模板引擎