【】Springmvc thymeleaf springmvc整合thymeleaf

アウトライン

あなたはJSPフル機能の代替でのSpring MVCアプリケーションとして使用できるようにThymeleaf春は、統合のセットを提供します。

統合ができるようになります:

  • JSP、テンプレートThymeleaf管理に前方マッピング法スプリングMVCオブジェクトを使用するのと同じ@Controller。
  • 代わりに、OGNLのテンプレートで使用春の式言語(EL春)。
  • フォームテンプレートやフォームのサポートBeanを作成してバインドプロパティエディタ、変換および検証サービスのエラー処理の使用を含む完全に統合された結果、。
  • (従来のMessageSourceオブジェクトによって)春の国際メッセージ表示メッセージファイル管理。
  • あなたのテンプレートを解決するためにSpringの独自のリソースの解決メカニズムを使用してください。

自身thymeleaf我々は多くの設定を行う必要はありませんので、あなたは私たちが望む結果を得ることができ、統合の春を作りました。Javaコード、②、構成されたXMLファイルは、この記事では、2番目のXMLコンフィギュレーションに焦点を当てていること、①、コンフィギュレーションノート:thymeleaf統合は、2つのメソッドを提供します。

あなたは知識のポイントを取得することができます:

thymeleaf 1、springmvc統合
スプリングによって提供される3つのモデルを使用して2、
3、フロントthymeleaf力にHTMLエントリを解決しない(質問1参照)
4、ゴミ問題を解決するHTMLフロントディスプレイが(質問2を参照します)

@

springmvc統合thymeleaf

A:追加の依存

追加するほかに、springmvcではthymeleaf主に依存、必要以外のthymeleaf-spring4、そうでない場合は、彼らが報告されますorg.thymeleaf.spring4.view.ThymeleafViewResolver、パーサがthymeleafを見つけることができない、それはthymeleaf-spring4また不可欠です。

Thymeleafは、2つの独立したライブラリがthymeleaf-spring3を呼ぶことによって、スプリングフレームワーク3.xおよび4.xのための統合されthymeleaf-spring4を提供しました。これらのライブラリは別の.jarファイル(thymeleaf-spring3- {バージョン}の.jarとthymeleaf-spring4- {バージョン}の.jar)にパッケージ化され、アプリケーションThymeleafスプリング統合で使用するために、クラスパスを追加する必要があります

        <!--        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>

2:設定thymeleafパーサ

パーサthymeleaf Springmvc構成プロファイル、公式文書Thymeleafは、上記の二つのインターフェースの実装を提供します。

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

しかし、今、それはされたorg.thymeleaf.spring4.view.ThymeleafViewResolver上記の構成にも影響を取ることができますかどうかについて、置き換え、それはしようとするのはあなた次第です。

 <!-- 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>

ViewResolversは、オブジェクト固有の操作およびロケールViewオブジェクトを取得する責任があります。典型的には、コントローラは、特定の名前を持つビュー(制御文字列によって返された方法)に転送ViewResolversを必要とし、ビューリゾルバチェーン内のすべてのアプリケーションは、一つには、このビューを解決することができるまで行う順序であろう。あなたは、Viewオブジェクトを返し、HTMLをレンダリングするオブジェクトに制御を渡した場合。

注:これは、ことは注目に値する彼らは、ビューリゾルバの春を設定した場合、それ以外のパーサthymeleaf効果的ではないかもしれないが、コメントアウトする必要があり、このために私の長い時間をデバッグ、最終的に問題を発見しました。

    <!--    配置视图解析器 prefix:前缀, suffix:后缀   使用thymeleaf需要将其注释掉-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".html"/>
    </bean>

3:ライト・コントローラ

ビューへの制御層からの転送データをする必要がある、我々はモデルを使用しますが、一般的に使用される3つのモデルがある:モデル、ModelMap、のModelAndViewあなたはこれらの3つのモデルを使用する場合は、Springフレームワークが自動的に参照としてコントローラのインスタンスを作成し、ユーザーが独自に作成する必要はありません

1、モデル

    /**
     * 在Model里存入一个用户信息
     * @return hello页面
     */
    @GetMapping("returnModelAndView")
    public String returnModelAndView(Model model){
        model.addAttribute("userInfo",new UserInfo("lomtom","123",new Address("湖南","邵阳")));
        return "hello";
    }

モデルは、インタフェース、ある
モデル出典:

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();
}

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);
    }
}

3、使用してのModelAndView

    /**
     * 在ModelAndView里存入一个用户信息
     * @return ModelAndView 
     */
    @GetMapping("returnModelAndView")
    public ModelAndView returnModelAndView(ModelAndView modelAndView){
        modelAndView.setViewName("hello");
        modelAndView.addObject("userInfo",new UserInfo("lomtom","123",new Address("湖南","邵阳")));
        return modelAndView;
    }

ModelAndView名がモデルとしようとの組み合わせであることを示唆しています。
ModelAndView出典:

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

	......
}

4:書き込みのhtml

まず、リンク、要求書き込みreturnModelAndView要求を。

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

その後、検証のためのhello.htmlページを書きます

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

5:結果

6:私の問題を記録

質問1:あなたはすべてを設定した後、thymeleafは解決できない、すべてのthymeleafに表示が有効ではありません。
解決:私は効果的であることができなかった解決しようとのthymeleaf結果として表示解像度の春を、設定されているので、そのビューの解像度を取り除く春を取得します。

thmelafはSpringmvcビューの解像度を紹介:
すぐにそれが設定されている方法を理解するのに十分なそのプロパティを参照します。

  • ビューviewClassは、クラスのインスタンスを確立します。必要であるが、我々はThymeleafと協力する場合、必要のないJSPパーサ、。
  • suffixThymeleaf TemplateResolverと同様の作業が同じname属性のオブジェクトの前に付けます。
  • ViewResolverためのクエリの順序は、チェーンで決定されます。
  • これは(ワイルドカード)ビュー名を許可するViewResolver解決viewNames。

質問2:フロントは文字化けは、特定の性能が文字化けバックグラウンドで渡されますが、HTMLに存在することを文字化けされていません。
解決:パラメータを追加しようとする試みでは、テンプレートパーサパーサ:<property name="characterEncoding" value="UTF-8"/>

著者は言いたいことがあります

私は、公式の説明thymeleaf詳細な構成を参照してくださいどのように詳細に記述しないことがあります。ポータルを終わり、それをすべて読むには、あなたが助けを持っている場合は、賞賛にそれをポイントしてください。

おすすめ

転載: www.cnblogs.com/lomtom/p/12635945.html