2017-10-25 22 views
7

Spring MVC、JSP、およびTylesでカスタムタグライブラリを作成したので、いくつかのファイルがあります。.tagx 新しいプロジェクトでは、私はSpring BootとThymelafを試してみることにしましたが、カスタムライブラリを保存したいと思います...ThymeleafとSpring Bootでカスタムタグライブラリを使用するにはどうすればいいですか?

あなたはthymleafを使ってカスタムタグライブラリを作成することはできますか?または、カスタムタグライブラリをインポートすることはできますか?

EDIT

は私がより明確にするために、コードのいくつかの作品を追加します。以下の使用タグはカスタマイズされたタグです。だから私は、私はこのページの結果はこのようにフォームと標準のHTMLページ、それと送信ボタン内部のいくつかのフィールドとラベル..

あるxmlns:form="urn:jsptagdir:/WEB-INF/tags/form"

<form:create id="fu_utente" modelAttribute="utente" path="/utente" > 
      <div class="row"> 
       <div class="col-md-12 text-center"> 
        <h1 class="fa fa-user-plus" style="color:green;"><b>&#160;&#160;Stai creando un nuovo utente di tipo: <var class="varFont">&#160;${utente.ruolo}</var></b></h1> 
       </div> 
      </div> 
       <div class="row"> 
        <div class="col-xs-12 col-sm-12 col-md-4 col-md-offset-2"> 
         <field:input field="nome" id="c_utente_nome" required="true"/> 
        </div> 
        <div class="col-xs-12 col-sm-12 col-md-4"> 
         <field:input field="userName" id="c_utente_username" min="5" max="15" required="true"/> 
        </div> 
        <div class="col-xs-12 col-sm-12 col-md-8 col-md-offset-2"> 
         <field:input field="email" id="c_Utente_email" required="true" validationRegex="^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$"/> 
        </div> 
        <div class="col-xs-12 col-sm-12 col-md-4 col-md-offset-2"> 
         <field:input field="nuovaPassword" id="c_utente_password" min="6" max="15" required="true" type="password"/> 
        </div> 
        <div class="col-xs-12 col-sm-12 col-md-4"> 
         <field:input field="confermaNuovaPassword" id="c_utente_confirmPassword" required="true" type="password"/> 
        </div> 
        </div> 
       </div> 

</form> 

とJSP内部に含ますばやくたくさんのHTMLコードを書くことができます。たとえば、各フィールドに<label>..... </label><input....../>を書き込む代わりに、国際化も使用して<field:input......>と書くことができます。

私はThymeleafで同じことをしたいと思います。

はそうでない場合、あなたはコードと時間を節約するために避けるために、Thymeleafを使用する方法を知っていれば、...

答えて

2

を教えてください私は解決策/回避策の一種として、以下を使用。現時点では2つの単純なタグしか作成していませんが、これがより複雑なタグを実装する良い方法であるかどうかはわかりません。

INPUTタグ

<!DOCTYPE html> 
<html xmlns:th="http://www.thymeleaf.org"> 
<body> 
    <section th:fragment="input(name, value, type, required)" class="form-group" th:switch="${required}"> 
     <label th:text="#{${name}}" th:for="${name}" class="control-label"></label> 
     <input th:case="true" th:type="${type}" th:id="${name}" th:name="${name}" th:value="${value}" class="form-control" required="required"/> 
     <input th:case="false" th:type="${type}" th:id="${name}" th:name="${name}" th:value="${value}" class="form-control"/> 
    </section> 
</body> 
</html> 

表示タグ

<!DOCTYPE html> 
<html xmlns:th="http://www.thymeleaf.org"> 
<body> 
    <section th:fragment="display(name, value)" class="form-group"> 
     <label th:text="#{${name}}" th:for="${name}" class="control-label"></label> 
     <div class="box" th:id="${name}" th:text="${value}"></div> 
    </section> 
</body> 
</html> 

それから私は私が好きたいパラメータを渡すこれらの2個のタグを使用:

<section th:replace="~{/tags/input :: input(username, *{username}, text, true)}"></section> 

<section th:replace="~{/tags/display :: display(nome, *{nome})}"></section> 
関連する問題