国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

springmvc整合thymeleaf

這篇具有很好參考價(jià)值的文章主要介紹了springmvc整合thymeleaf。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

概述

Thymeleaf提供了一組Spring集成,使您可以將其用作Spring MVC應(yīng)用程序中JSP的全功能替代品。

這些集成將使您能夠:

  • @Controller像使用JSP一樣,將Spring MVC 對(duì)象中的映射方法轉(zhuǎn)發(fā)到Thymeleaf管理的模板。
  • 在模板中使用Spring表達(dá)式語(yǔ)言(Spring EL)代替OGNL。
  • 在與表單支持Bean和結(jié)果綁定完全集成的模板中創(chuàng)建表單,包括使用屬性編輯器,轉(zhuǎn)換服務(wù)和驗(yàn)證錯(cuò)誤處理。
  • 顯示Spring管理的消息文件中的國(guó)際化消息(通過(guò)常規(guī)MessageSource對(duì)象)。
  • 使用Spring自己的資源解析機(jī)制解析您的模板。

thymeleaf自己也做了spring的集成,所以我們并不需要做太多的配置,就可以達(dá)到我們想要的結(jié)果。thymeleaf提供了兩種集成方法:①、注解配置,也就是java代碼,②、xml文件配配置,本文主要介紹第二種xml配置。

你能get到的知識(shí)點(diǎn):

1、springmvc整合thymeleaf

2、spring提供的三種model的使用

3、解決html前端thymeleaf不生效問(wèn)題(見(jiàn)問(wèn)題1)

4、解決html前端顯示亂碼問(wèn)題(見(jiàn)問(wèn)題2)

springmvc整合thymeleaf

springmvc整合thymeleaf

一:加入依賴

在springmvc里面,除了要加入 thymeleaf的主依賴之外,還需要 thymeleaf-spring4,否則會(huì)報(bào) org.thymeleaf.spring4.view.ThymeleafViewResolver,找不到thymeleaf解析器,所以 thymeleaf-spring4也是必不可少的。

Thymeleaf具有針對(duì)Spring Framework 3.x和4.x的集成,由兩個(gè)獨(dú)立的庫(kù)分別稱為thymeleaf-spring3和提供thymeleaf-spring4。這些庫(kù)打包在單獨(dú)的.jar文件(thymeleaf-spring3-{version}.jar和thymeleaf-spring4-{version}.jar)中,需要添加到類路徑中,以便在應(yīng)用程序中使用Thymeleaf的Spring集成

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

復(fù)制

在springmvc配置文件中配置thymeleaf解析器,官方文檔中Thymeleaf提供了上述兩個(gè)接口的實(shí)現(xiàn):

    org.thymeleaf.spring4.view.ThymeleafView
    org.thymeleaf.spring4.view.ThymeleafViewResolver

復(fù)制

不過(guò)現(xiàn)在都已經(jīng)被 org.thymeleaf.spring4.view.ThymeleafViewResolver所代替,至于以上配置是否還能夠生效,就要靠你來(lái)試試了。

 <!-- thymeleaf 模板解析器 -->
    <bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
        <property name="prefix" value="/" />
        <property name="suffix" value=".html" />
        <property name="templateMode" value="HTML" />
        <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 id="viewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine" />
        <property name="characterEncoding" value="UTF-8"/>
    </bean>

復(fù)制

ViewResolvers是負(fù)責(zé)獲取特定操作和語(yǔ)言環(huán)境的View對(duì)象的對(duì)象。通常,控制器要求ViewResolvers轉(zhuǎn)發(fā)到具有特定名稱的視圖(由controller方法返回的String),然后應(yīng)用程序中的所有視圖解析器將按有序鏈執(zhí)行,直到其中一個(gè)能夠解析該視圖為止。如果返回了View對(duì)象,并且將控件傳遞給該對(duì)象以呈現(xiàn)HTML。

注:值得注意的是,如果自己設(shè)置了spring的視圖解析器,需要將其注釋掉,否則thymeleaf解析器可能不會(huì)生效,我就是因?yàn)檫@個(gè)調(diào)試了好久,最后才發(fā)現(xiàn)這個(gè)問(wèn)題。

<!--    配置視圖解析器 prefix:前綴, suffix:后綴   使用thymeleaf需要將其注釋掉-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".html"/>
    </bean>

復(fù)制

:編寫(xiě)控制器

需要從控制層傳數(shù)據(jù)到視圖時(shí),我們就會(huì)使用model,常用的三種model就是:Model、ModelMap、ModelAndView。使用這三種model時(shí),spring框架自動(dòng)創(chuàng)建實(shí)例并作為controller的入?yún)?,用戶無(wú)需自己創(chuàng)建

1、使用Model

/**
     * 在Model里存入一個(gè)用戶信息
     * @return hello頁(yè)面
     */
    @GetMapping("returnModelAndView")
    public String returnModelAndView(Model model){
        model.addAttribute("userInfo",new UserInfo("lomtom","123",new Address("湖南","邵陽(yáng)")));
        return "hello";
    }

復(fù)制

Model是一個(gè)接口, Model源碼:

public interface Model {
    Model addAttribute(String var1, Object var2);

    Model addAttribute(Object var1);

    Model addAllAttributes(Collection<?> var1);

    Model addAllAttributes(Map<String, ?> var1);

    Model mergeAttributes(Map<String, ?> var1);

    boolean containsAttribute(String var1);

    Map<String, Object> asMap();
}

復(fù)制

2、使用ModelMap

ModelMap繼承LinkedHashMap

ModelMap源碼:

public class ModelMap extends LinkedHashMap<String, Object> {
    public ModelMap() {
    }

    public ModelMap(String attributeName, Object attributeValue) {
        this.addAttribute(attributeName, attributeValue);
    }

    public ModelMap(Object attributeValue) {
        this.addAttribute(attributeValue);
    }

    public ModelMap addAttribute(String attributeName, Object attributeValue) {
        Assert.notNull(attributeName, "Model attribute name must not be null");
        this.put(attributeName, attributeValue);
        return this;
    }

    public ModelMap addAttribute(Object attributeValue) {
        Assert.notNull(attributeValue, "Model object must not be null");
        return attributeValue instanceof Collection && ((Collection)attributeValue).isEmpty() ? this : this.addAttribute(Conventions.getVariableName(attributeValue), attributeValue);
    }

    public ModelMap addAllAttributes(Collection<?> attributeValues) {
        if (attributeValues != null) {
            Iterator var2 = attributeValues.iterator();

            while(var2.hasNext()) {
                Object attributeValue = var2.next();
                this.addAttribute(attributeValue);
            }
        }

        return this;
    }

    public ModelMap addAllAttributes(Map<String, ?> attributes) {
        if (attributes != null) {
            this.putAll(attributes);
        }

        return this;
    }

    public ModelMap mergeAttributes(Map<String, ?> attributes) {
        if (attributes != null) {
            Iterator var2 = attributes.entrySet().iterator();

            while(var2.hasNext()) {
                Entry<String, ?> entry = (Entry)var2.next();
                String key = (String)entry.getKey();
                if (!this.containsKey(key)) {
                    this.put(key, entry.getValue());
                }
            }
        }

        return this;
    }

    public boolean containsAttribute(String attributeName) {
        return this.containsKey(attributeName);
    }
}

復(fù)制

3、使用ModelAndView

/**
     * 在ModelAndView里存入一個(gè)用戶信息
     * @return ModelAndView
     */
    @GetMapping("returnModelAndView")
    public ModelAndView returnModelAndView(ModelAndView modelAndView){
        modelAndView.setViewName("hello");
        modelAndView.addObject("userInfo",new UserInfo("lomtom","123",new Address("湖南","邵陽(yáng)")));
        return modelAndView;
    }

復(fù)制

ModelAndView顧名思義就是模型和試圖的結(jié)合。ModelAndView源碼:

public class ModelAndView {
    private Object view;
    private ModelMap model;
    private HttpStatus status;
    private boolean cleared = false;

    ......
}

復(fù)制

四:編寫(xiě)html

首先,寫(xiě)一個(gè)鏈接,請(qǐng)求 returnModelAndView請(qǐng)求。

<a href="returnModelAndView">ModelAndView</a>

復(fù)制

然后,寫(xiě)hello.html頁(yè)面用于驗(yàn)證

<h2>你好啊,你成功了</h2>
<p th:text="${userInfo.userName}+'來(lái)自'+${userInfo.address.province}+${userInfo.address.city}"></p>

復(fù)制

五:結(jié)果

springmvc整合thymeleaf

六:記錄我遇到的問(wèn)題

問(wèn)題1:配置好一切后,thymeleaf無(wú)法解析,所有關(guān)于thymeleaf的顯示都無(wú)法生效。解決:由于我配置了spring的視圖解析,所以導(dǎo)致thymeleaf的試圖解析無(wú)法生效,所以去掉spring的視圖解析。

thmelaf介紹Springmvc的視圖解析:快速瀏覽其屬性足以了解其配置方式:

  • viewClass建立View實(shí)例的類。對(duì)于JSP解析器,這是必需的,但是當(dāng)我們與Thymeleaf合作時(shí),根本不需要。
  • prefix與suffixThymeleaf的TemplateResolver對(duì)象中相同名稱的屬性的工作方式相似。
  • order 確定在鏈中查詢ViewResolver的順序。
  • viewNames 允許使用此ViewResolver解析視圖名稱(帶通配符)。

問(wèn)題2:前端顯示亂碼,具體表現(xiàn)為后臺(tái)傳入的不亂碼,但是html中原本存在的亂碼。解決:在試圖解析器和模板解析器中加入?yún)?shù):<propertyname="characterEncoding"value="UTF-8"/>文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-484075.html

到了這里,關(guān)于springmvc整合thymeleaf的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • SpringBoot整合模板引擎Thymeleaf(4)

    SpringBoot整合模板引擎Thymeleaf(4)

    本文原創(chuàng)作者:谷哥的小弟 作者博客地址:http://blog.csdn.net/lfdfhl 在之前的教程中,我們介紹了Thymeleaf的基礎(chǔ)知識(shí)。在此,以案例形式詳細(xì)介紹Thymeleaf的基本使用。 要點(diǎn)概述: 1、在static下創(chuàng)建css文件夾用于存放css文件 2、在static下創(chuàng)建img文件夾用于存放圖片文件 請(qǐng)?jiān)趐om.xml文

    2024年02月10日
    瀏覽(18)
  • 前端模板引擎Thymeleaf的整合和使用

    目錄 一、添加依賴 1.1首先,在項(xiàng)目的構(gòu)建文件中(比如 Maven 或 Gradle)添加 Thymeleaf 的依賴。例如,對(duì)于 Maven 項(xiàng)目,在 pom.xml 文件中添加以下依賴 1.2保存并更新項(xiàng)目依賴 二、配置Thymeleaf 2.1模板位置配置 2.2模板緩存配置 2.3自定義標(biāo)簽配置 三、創(chuàng)建模板文件 3.1創(chuàng)建一個(gè)HTML文

    2024年04月27日
    瀏覽(31)
  • SpringBoot 整合Thymeleaf教程及使用 springboot配合thymeleaf,調(diào)用接口不跳轉(zhuǎn)頁(yè)面只顯示文本

    Thymeleaf 是一款用于渲染 XML/XHTML/HTML5 內(nèi)容的模板引擎。它與 JSP,Velocity,F(xiàn)reeMaker 等模板引擎類似,也可以輕易地與 Spring MVC 等 Web 框架集成。與其它模板引擎相比,Thymeleaf 最大的特點(diǎn)是,即使不啟動(dòng) Web 應(yīng)用,也可以直接在瀏覽器中打開(kāi)并正確顯示模板頁(yè)面 。 目錄 一、整合

    2024年02月08日
    瀏覽(38)
  • 【Springboot】SpringBoot基礎(chǔ)知識(shí)及整合Thymeleaf模板引擎

    【Springboot】SpringBoot基礎(chǔ)知識(shí)及整合Thymeleaf模板引擎

    ??博客x主頁(yè):己不由心王道長(zhǎng)??! ??文章說(shuō)明:spring?? ?系列專欄:spring ??本篇內(nèi)容:對(duì)SpringBoot進(jìn)行一個(gè)入門(mén)學(xué)習(xí)及對(duì)Thymeleaf模板引擎進(jìn)行整合(對(duì)所需知識(shí)點(diǎn)進(jìn)行選擇閱讀呀~)?? ??每日一語(yǔ):在人生的道路上,即使一切都失去了,只要一息尚存,你就沒(méi)有絲毫理

    2023年04月23日
    瀏覽(24)
  • idea利用spring框架整合thymeleaf展現(xiàn)數(shù)據(jù)庫(kù)數(shù)據(jù)

    idea利用spring框架整合thymeleaf展現(xiàn)數(shù)據(jù)庫(kù)數(shù)據(jù)

    idea初步利用thymeleaf展現(xiàn)列表 上一篇文章簡(jiǎn)單展現(xiàn)自己寫(xiě)的列表; 這篇文章連接mysql數(shù)據(jù)庫(kù)實(shí)現(xiàn)數(shù)據(jù)庫(kù)數(shù)據(jù)展現(xiàn) 主要三個(gè)文件 controller指定html界面 mapper寫(xiě)數(shù)據(jù)庫(kù)sql查詢語(yǔ)句 pojo中的user寫(xiě)具體數(shù)據(jù)庫(kù)中的表包含哪些字段(這部分最好的方式寫(xiě)出變量名字然后alt+insert自動(dòng)生成g

    2024年02月05日
    瀏覽(27)
  • springboot整合security,mybatisPlus,thymeleaf實(shí)現(xiàn)登錄認(rèn)證及用戶,菜單,角色權(quán)限管理

    springboot整合security,mybatisPlus,thymeleaf實(shí)現(xiàn)登錄認(rèn)證及用戶,菜單,角色權(quán)限管理

    本系統(tǒng)為springboot整合security,mybatisPlus,thymeleaf實(shí)現(xiàn)登錄認(rèn)證及用戶,菜單,角色權(quán)限管理。頁(yè)面為極簡(jiǎn)模式,沒(méi)有任何渲染。 源碼:https://gitee.com/qfp17393120407/spring-boot_thymeleaf 架構(gòu)截圖 此處以用戶表為例,其他表數(shù)據(jù)可在源碼獲取。 用戶表 共用屬性 共用屬性自動(dòng)填充配置

    2024年02月07日
    瀏覽(21)
  • 【Spring Boot】Thymeleaf模板引擎 — Thymeleaf入門(mén)

    主要介紹什么是Thymeleaf以及Spring Boot如何集成使用Thymeleaf模板,最后介紹Spring Boot支持的Thymeleaf的一些常用的配置參數(shù)。 Thymeleaf是一款非常優(yōu)秀的服務(wù)器端頁(yè)面模板引擎,適用于Web和獨(dú)立環(huán)境,具有豐富的標(biāo)簽語(yǔ)言和函數(shù),能夠處理HTML、XML、JavaScript甚至文本。 Thymeleaf相較于

    2024年02月05日
    瀏覽(24)
  • Thymeleaf模版引擎初嘗試

    模版引擎雖然不能夠?qū)崿F(xiàn)代碼與視圖解耦,但是其適合于個(gè)人開(kāi)發(fā)者使用,而且如果存在前后端項(xiàng)目中,前端大量請(qǐng)求后端時(shí),模版引擎無(wú)疑也存在優(yōu)勢(shì)。 SpringBoot 整合步驟: 引入依賴 編寫(xiě) yml 配置 編寫(xiě) html 模版文件 編寫(xiě) Controller 接口

    2024年02月13日
    瀏覽(51)
  • thymeleaf模板引擎

    thymeleaf模板引擎

    ThymeleafProperties 配置類 1.默認(rèn)編碼 2.前綴 3.后綴 相當(dāng)于視圖解析器? ? 這是學(xué)SpringBoot的必經(jīng)之路,非常重要?。。。ǔ悄闶菍W(xué)前端的) ? 只改了前端代碼點(diǎn)一下這個(gè)就可以刷新? ? 傳值過(guò)來(lái)了? th:text=\\\"${msg}\\\"爆紅,但是可以顯示,F(xiàn)ile-Settings-Editor-Inspection ?取消“Expression

    2024年02月14日
    瀏覽(19)
  • SpringBoot Thymeleaf模板引擎

    SpringBoot Thymeleaf模板引擎

    前端交給我們的頁(yè)面,是 html 頁(yè)面。如果是我們以前開(kāi)發(fā),我們需要把他們轉(zhuǎn)成jsp頁(yè)面,jsp好處就是當(dāng)我們查出一些數(shù)據(jù)轉(zhuǎn)發(fā)到JSP頁(yè)面以后,我們可以用jsp輕松實(shí)現(xiàn)數(shù)據(jù)的顯示,及交互等。 jsp支持非常強(qiáng)大的功能,包括能寫(xiě)Java代碼,但是呢,我們現(xiàn)在的這種情況,SpringBoot這

    2024年02月13日
    瀏覽(56)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包