GVKun编程网logo

Spring JSP MVC和Thymeleaf MVC之间的区别(springmvc与jsp)

13

针对SpringJSPMVC和ThymeleafMVC之间的区别和springmvc与jsp这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展ASP.NETMVC和FubuMVC之间的主要区别

针对Spring JSP MVC和Thymeleaf MVC之间的区别springmvc与jsp这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展ASP.NET MVC和FubuMVC之间的主要区别是什么?、eclipse springmvc+Thymeleaf、Idea Spring boot webmvc Thymeleaf namespace not found、Spring - MVC - thymeleaf 缓存关闭等相关知识,希望可以帮助到你。

本文目录一览:

Spring JSP MVC和Thymeleaf MVC之间的区别(springmvc与jsp)

Spring JSP MVC和Thymeleaf MVC之间的区别(springmvc与jsp)

spring JSP MVC和Thymeleaf MVC有什么区别?哪种是spring网页设计的最佳方法?

答案1

小编典典

它们都是Spring MVC的视图层。首先,最基本的区别是文件扩展名。(.jsp.html

Branislav在评论中是正确的,JSP不是模板引擎。它被编译到servlet,然后servlet正在提供Web内容。另一方面,Thymeleaf是一个模板引擎,可获取HTML文件,解析文件然后生成要提供的Web内容。

  • 将Thymeleaf与JSP视图进行比较时,它更像是HTML-ish视图。

  • 我们可以在thymeleaf中使用原型代码:http : //www.dineshonjava.com/2015/01/thymeleaf-vs-jsp-spring-mvc-view-layer.html#.WEkLzLKLTig

  • 由于它是HTML风格的代码,因此百里香叶代码更具可读性(当然,您可以打乱它并创建不可读的代码,但是最后,将其与.jsp文件进行比较时,可读性更高)

  • 标准方言( 表达语言 )比JSP表达语言强大得多

  • 如果我们把所有这些都放在一边,百里香是这里的慢者。

我建议您看一下此文档:http :
//www.thymeleaf.org/doc/articles/thvsjsp.html

ASP.NET MVC和FubuMVC之间的主要区别是什么?

ASP.NET MVC和FubuMVC之间的主要区别是什么?

我知道的开发者只是显示我 FubuMVC,有一个问题,我们是否应该尝试使用它在一个真正的现场项目或不。目前为止的设计选择是ASP.NET MVC。

我有兴趣发现是:

ASP.NET MVC和FubuMVC之间的核心区别是什么?

任何框架的收益/损失是什么?

FubuMVC准备好了黄金时间吗?

您将在新项目中使用以下哪两个?

解决方法

@maz它取决于你的应用程序将要做什么,它的时间框架,其临界性等。

首先,我鼓励你混淆两者。没有比其他整体更好或更差,他们是不同的,每个都有重要的优势和弱点,你应该考虑。

在这一点上,ASP.NET MVC更成熟,有更多的测试者和眼球看着它。所以,如果你正在做任何严肃的项目(即你得到报酬),我鼓励你,在这一点上,使用ASP.NET MVC,因为它将“运送”更早和更多的支持。

(更新[2011年2月26日]):FubuMVC现在被几个公司和团队用于成熟的生产应用程序,已经有几个主要的发布版本基于FubuMVC。虽然文档仍然有点缺乏,但框架的质量现在已经确定为生产有价值的)

如果你开始一个爱好项目(即个人博客,个人/家庭相册等),然后我会邀请你拿起FubuMVC,并开始玩它。记住这一点,这基本上是一个爱好,我的,杰里米的,和一些其他人,所以没有时间轴上的保证等。它没有真正被公开宣布(它的泄漏,更好或更坏)所以有’一直是大量的贡献。我希望随着它成熟,我们达到第一个里程碑,然后宣布它的广泛,宽的世界,贡献将上升。

最后,使用任何一个都可能鼓励更好的开发实践,比如ASP.NET WebForms和熟悉一个,将使你更容易拿起另一个很快。

它真的归结到你正在工作的项目的关键,所以选择负责任。

eclipse springmvc+Thymeleaf

eclipse springmvc+Thymeleaf

修改pom.xml引入Thymeleaf相关包:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>xuan</groupId>
    <artifactId>springmvc</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>springmvc Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <spring.version>4.1.3.RELEASE</spring.version>
        <thymeleaf.version>2.1.2.RELEASE</thymeleaf.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring4</artifactId>
            <version>${thymeleaf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>${thymeleaf.version}</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>springmvc</finalName>
    </build>
</project>

修改springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd              
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <!-- 激活@Controller模式 -->
    <mvc:annotation-driven />
    <!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 需要更改 -->
    <context:component-scan
        base-package="org.xuan.springmvc.controller" />

    <mvc:resources location="/static/" mapping="/static/**" />

    <!-- 模板解析器 -->
    <bean id="templateResolver"
        class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
        <property name="prefix" value="/WEB-INF/templates/" />
        <property name="suffix" value=".html" />
        <property name="templateMode" value="HTML5" />
        <property name="cacheable" value="false" />
        <property name="characterEncoding" value="UTF-8" />
    </bean>

    <bean id="templateEngine"
        class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver" />
    </bean>

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

</beans>

在WEB-INF下面新建templates目录,在templates下面新建hello.html:

<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<script type="text/javascript" src="static/js/jquery-1.10.2.min.js"
    th:src="@{/static/js/jquery-1.10.2.min.js}"></script>

<script th:inline="javascript">
  $(function(){
   var _ctx = [[${application.ctx}]];
    alert("Project ContextPath:"+_ctx);
    alert("路径:"+$("#ctx").val());
  });
</script>
<title>Spring MVC + Thymeleaf Example</title>
</head>
<body>
    <!-- Project ContextPath -->
    <input type="hidden" id="ctx" th:value="${application.ctx}" /> Hello,
    <span th:text="${name}" />!
    <br /> Hello,
    <span th:text="${query}" />!
    <br /> Hello,
    <span th:text="${submit}" />!
    <br />
    <a th:href="@{/query?name=a_href}"> query</a>
    <br />
    <form th:action="@{/submit}">
        <input type="text" name="name" /><input type="text" name="age" />
        <button type="submit">submit</button>
    </form>

</body>
</html>

修改MainController.java:

package org.xuan.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MainController {
    @RequestMapping(value = "/{name}", method = RequestMethod.GET)
    public String getMovie(@PathVariable String name, ModelMap model) {
        model.addAttribute("name", name);
        model.addAttribute("query", "");
        model.addAttribute("submit", "");
        return "hello";
    }

    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public String query(@RequestParam("name") String name, ModelMap model) {
        model.addAttribute("name", "");
        model.addAttribute("query", name);
        model.addAttribute("submit", "");
        return "hello";
    }

    @RequestMapping(value = "/submit", method = RequestMethod.GET)
    public String submit(@RequestParam("name") String name, @RequestParam("age") String age, ModelMap model) {
        model.addAttribute("name", "");
        model.addAttribute("query", "");
        model.addAttribute("submit", name + age);
        return "hello";
    }
}

整个项目的目录结构如下:

 

运行效果:

1

2

3

 

Idea Spring boot webmvc Thymeleaf namespace not found

Idea Spring boot webmvc Thymeleaf namespace not found

解决方法:

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
</html>

 

Spring - MVC - thymeleaf 缓存关闭

Spring - MVC - thymeleaf 缓存关闭

1. 概述

  1. spring 配合 thymeleaf 关闭页面缓存

2. 背景

  1. 最近复习 spring

  2. 找了本书叫 spring in action 5th

    1. 本人水平有限
    2. 书还写得那么难
  3. 调试中遇到了问题

    1. 问题1: 我每次改代码, 都要重启
    2. 问题2: 我每次改页面, 都要重启
  4. 因为 问题2 对我来说, 优先级最高, 我首先解决它

3. 环境

  1. os

    1. win10
  2. jdk

    1. 1.8
  3. ide

    1. ida 2018.1
  4. spring

    1. spring boot
      1. 2.1.7 release
    2. 组件
      1. thymeleaf
      2. starter-web
      3. devtool
      4. starter-test
  5. browser

    1. firefox
      1. 70.0
  6. ref

    1. spring in action 5th

4. 问题

  1. 期望

    1. 在 ide 修改页面
    2. 在浏览器直接刷新, 就能看到页面调整
  2. 需要保证

    1. 浏览器的正确配置
      1. 浏览器
        1. 确保每次请求, 都是新鲜的页面
    2. spring 和 thymeleaf 的正确配置
      1. spring
        1. 确保 thymeleaf 的修改能及时

5. 问题解决

  1. 浏览器

    1. 思路1: 每次访问后清空缓存

      1. 结果
        1. 否决
          1. 太麻烦了
    2. 思路2: 通过浏览器配置, 确保每次请求, 都是新页面

      1. 结果

        1. 这个方案不错
        2. 而且也有配置项可以做到
        3. 因为很顺利, 就不细说了, 照着配置一步步来就行
      2. ref

        1. 设置火狐浏览器不缓存js与html
  2. thymeleaf

    1. 思路: 找到配置项, 配置一下就好了
      1. 结果

        1. 妈的找死我了
        2. 百度了好多, 都没用
          1. 千篇一律, 只有一条参数

            spring.thymeleaf.cache=false
            
          2. 结果配置了根本不生效

          3. 这感觉太难受了, 真的想骂人

      2. 最后结果

        1. ref
          1. Thymeleaf templates cache even when spring.template.cache: false

            1. 一个叫 oak1980 的丹麦老哥, 给了一个配置如下

              # Templates reloading during development
              spring.thymeleaf.prefix=file:src/main/resources/templates/
              spring.thymeleaf.cache=false
              
              # Static resources reloading during development
              spring.resources.static-locations=file:src/main/resources/static/
              spring.resources.cache-period=0
              
          2. 按照这个老哥的做法, 配置终于生效

ps

  1. 妈的为啥我百度的头几个, 没有一个把事说明白

    1. 大部分只给了 cache = false 的那条配置
    2. 其他提到 prefix 的, 还都把 prefix 注释了
    3. 配置的 ide, 组件, 版本也没有
    4. 找了好半天, 问题是这种都是调试常识之类的东西
  2. spring 如何确认运行阶段生效了哪些配置项

    1. 这个找了半天, 也没找到
    2. 我总觉得可以...

今天关于Spring JSP MVC和Thymeleaf MVC之间的区别springmvc与jsp的介绍到此结束,谢谢您的阅读,有关ASP.NET MVC和FubuMVC之间的主要区别是什么?、eclipse springmvc+Thymeleaf、Idea Spring boot webmvc Thymeleaf namespace not found、Spring - MVC - thymeleaf 缓存关闭等更多相关知识的信息可以在本站进行查询。

本文标签: