Thymeleafチュートリアル:第5章属性値の設定

目次

5属性値の設定


この章では、マークアップで属性値を設定(または変更)する方法について説明します。

5.1属性の値の設定


私たちのサイトがニュースレターを発行しており、ユーザーが購読できるようにしたいので、次の形式でテンプレート/WEB-INF/templates/subscribe.htmlを作成します。

<form action="subscribe.html"> <fieldset> <input type="text" name="email" /> <input type="submit" value="Subscribe!" /> </fieldset> </form> 

Thymeleafと同様に、このテンプレートは、Webアプリケーションのテンプレートというよりも静的なプロトタイプとして始まります。 まず、フォームのアクション属性はテンプレートファイル自体に静的にリンクされているため、便利なURL書き換えの余地はありません。 第二に、送信ボタンのvalue属性を使用すると、英語のテキストを表示できますが、国際化を希望します。

次に、インストールされているタグ属性の値を変更できるth:attr属性を使用します。

 <form action="subscribe.html" th:attr="action=@{/subscribe}"> <fieldset> <input type="text" name="email" /> <input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/> </fieldset> </form> 

概念は非常に単純です。th:attrは、実行する式を受け入れ、値を属性に割り当てます。 適切なコントローラーおよびメッセージファイルを作成すると、ファイル処理の結果は次のようになります。

 <form action="/gtvg/subscribe"> <fieldset> <input type="text" name="email" /> <input type="submit" value="¡Suscríbe!"/> </fieldset> </form> 

前の章で説明したように、新しい属性値に加えて、アプリケーションコンテキスト名が/ gtvg / subscribeのURLベースに自動的に追加されたことも確認できます。

しかし、一度に複数の属性を設定したい場合はどうでしょうか? XMLルールでは、タグに属性を2回設定することは許可されていないため、 th:attrは、コンマ区切りの割り当てリストを受け取ります。次に例を示します。

 <img src="../../images/gtvglogo.png" th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" /> 

必要なメッセージファイルが与えられると、これは出力します。

 <img src="/gtgv/images/gtvglogo.png" title="Logo de Good Thymes" alt="Logo de Good Thymes" /> 

5.2特定の属性の値の設定


今では、次のように考えるかもしれません:

 <input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/> 

...かなりupいマークアップです。 属性値内で宛先を指定することは非常に実用的ですが、常にこれを行う必要がある場合、テンプレートを作成する最もエレガントな方法ではありません。

Thymeleafはあなたに同意します。そのため、 th:attrはテンプレートでほとんど使用されません。 通常、他のth:*属性を使用します。そのタスクは、タグの特定の属性を構成することです(th:attrなどの属性だけではありません)。

たとえば、値属性を設定するには、値th:valueを使用します

 <input type="submit" value="Subscribe!" th:value="#{subscribe.submit}"/> 

それはずっと良く見えます! formタグのaction属性で同じことを試してみましょう:

 <form action="subscribe.html" th:action="@{/subscribe}"> 

そして、前にhome.htmlに入れたこれらのth:hrefを覚えていますか? これらはまったく同じ属性です。

 <li><a href="product/list.html" th:href="@{/product/list}">Product List</a></li> 

そのような属性は数多くあり、それぞれが特定のHTML5属性を対象としています。
th:abbrth:acceptth:accept-charset
th:accesskeyth:actionth:align
th:altth:archiveth:audio
th:autocompleteth:axisth:background
th:bgcolorth:borderth:cellpadding
th:cellspacingth:challengeth:charset
th:citeth:classth:classid
th:codebaseth:codetypeth:cols
th:colspanth:compactth:content
th:contenteditableth:contextmenuth:data
th:datetimeth:dirth:draggable
th:dropzoneth:enctypeth:for
th:formth:formactionth:formenctype
th:formmethodth:formtargetth:fragment
th:frameth:frameborderth:headers
th:heightth:highth:href
th:hreflangth:hspaceth:http-equiv
th:iconth:idth:inline
th:keytypeth:kindth:label
th:langth:listth:longdesc
th:lowth:manifestth:marginheight
th:marginwidthth:maxth:maxlength
th:mediath:methodth:min
th:nameth:onabortth:onafterprint
th:onbeforeprintth:onbeforeunloadth:onblur
th:oncanplayth:oncanplaythroughth:onchange
th:onclickth:oncontextmenuth:ondblclick
th:ondragth:ondragendth:ondragenter
th:ondragleaveth:ondragoverth:ondragstart
th:ondropth:ondurationchangeth:onemptied
th:onendedth:onerrorth:onfocus
th:onformchangeth:onforminputth:onhashchange
th:oninputth:oninvalidth:onkeydown
th:onkeypressth:onkeyupth:onload
th:onloadeddatath:onloadedmetadatath:onloadstart
th:onmessageth:onmousedownth:onmousemove
th:onmouseoutth:onmouseoverth:onmouseup
th:onmousewheelth:onofflineth:ononline
th:onpauseth:onplayth:onplaying
th:onpopstateth:onprogressth:onratechange
th:onreadystatechangeth:onredoth:onreset
th:onresizeth:onscrollth:onseeked
th:onseekingth:onselectth:onshow
th:onstalledth:onstorageth:onsubmit
th:onsuspendth:ontimeupdateth:onundo
th:onunloadth:onvolumechangeth:onwaiting
th:optimumth:patternth:placeholder
th:posterth:preloadth:radiogroup
th:relth:revth:rows
th:rowspanth:rulesth:sandbox
th:schemeth:scopeth:scrolling
th:sizeth:sizesth:span
th:spellcheckth:srcth:srclang
th:standbyth:startth:step
th:styleth:summaryth:tabindex
th:targetth:titleth:type
th:usemapth:valueth:valuetype
th:vspaceth:widthth:wrap
th:xmlbaseth:xmllangth:xmlspace

5.3一度に複数の値を設定する


th:alt-titleth:lang-xmllangという2つのかなり特殊な属性があり、これらを使用して2つの属性を同時に設定できます。 特に:

th:alt-titlealtおよびtitleに設定されます。
th:lang-xmllanglangxml:langをインストールします

GTVGホームページの場合、これによりこれを置き換えることができます。

 <img src="../../images/gtvglogo.png" th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" /> 

...またはこれ、次と同等:

 <img src="../../images/gtvglogo.png" th:src="@{/images/gtvglogo.png}" th:title="#{logo}" th:alt="#{logo}" /> 

...これに:

 <img src="../../images/gtvglogo.png" th:src="@{/images/gtvglogo.png}" th:alt-title="#{logo}" /> 

5.4前後に追加


Thymeleafはth:attrappendおよびth:attrprepend属性も提供します。これらの属性は 、既存の属性値に結果を追加(サフィックス)または追加(プレフィックス)します。

たとえば、ボタンの1つに追加するCSSクラスの名前をコンテキスト変数に保持したい場合があります。使用する特定のCSSクラスは、ユーザーが以前に行ったことに依存するためです。

 <input type="button" value="Do it!" class="btn" th:attrappend="class=${' ' + cssStyle}" /> 

cssStyle変数を「警告」に設定してこのテンプレートを処理すると、次の結果が得られます。

 <input type="button" value="Do it!" class="btn warning" /> 

標準の方言には、2つの特別な追加属性があります: th:classappendおよびth:styleappend属性は、既存のものを上書きせずにCSSクラスまたはスタイルフラグメントを要素に追加するために使用されます。

 <tr th:each="prod : ${prods}" class="row" th:classappend="${prodStat.odd}? 'odd'"> 

th:eachの値については心配しないでください。これは反復の属性です。これについては後で説明します)。

5.5固定値を持つブール属性


HTMLには、値がtrueの場合にのみアクティブ化される論理属性の概念があります。 XHTMLでは、これらの属性は値を1つだけ取ります。

たとえば、次を確認します。

 <input type="checkbox" name="option2" checked /> <!-- HTML --> <input type="checkbox" name="option1" checked="checked" /> <!-- XHTML --> 


標準的な方言には、条件によってこれらの属性を設定できる属性が含まれています。そのため、値が「true」の場合、属性は固定値に設定され、「false」の場合、属性は設定されません。

 <input type="checkbox" name="active" th:checked="${user.active}" /> 

標準の方言には、次の固定値を持つ論理属性があります。
th:asyncth:autofocusth:autoplay
th:checkedth:controlsth:declare
th:defaultth:deferth:disabled
th:formnovalidateth:hiddenth:ismap
th:loopth:multipleth:novalidate
th:nowrapth:openth:pubdate
th:readonlyth:requiredth:reversed
th:scopedth:seamlessth:selected

5.6属性の値の設定(デフォルトの属性プロセッサ)


Thymeleafは、 th:*が定義されていない場合でも、属性の値を設定できるデフォルトの属性プロセッサを提供します;標準的な方言の特定のプロセッサ。

このようなもの:

 <span th:whatever="${user.name}">...</span> 

結果は次のようになります。

 <span whatever="John Apricot">...</span> 

5.7属性、HTML5フレンドリ、および要素名のサポート


完全に異なる構文を使用して、HTML5のより便利な方法でプロセッサをテンプレートに適用することもできます。

 <table> <tr data-th-each="user : ${users}"> <td data-th-text="${user.login}">...</td> <td data-th-text="${user.name}">...</td> </tr> </table> 

data- {prefix}-{name}構文は、開発者がth:*などの名前空間を使用する必要なく、HTML5でカスタム属性を記述する標準的な方法です。 Thymeleafは、この構文を(標準だけでなく)すべての方言で自動的に使用可能にします。

カスタムタグを指定する構文もあります: {prefix}-{name} 。これは、W3Cカスタム要素仕様(大きなW3C Webコンポーネント仕様の一部)に準拠しています。 これは、たとえば、次のセクションで説明するth:block (またはth-block )要素に使用できます。

重要 :この構文は名前空間付きth:*に追加され、置き換えられません。 将来、名前空間構文を放棄するつもりはまったくありません。

Source: https://habr.com/ru/post/J351030/


All Articles