當前位置:才華齋>計算機>java語言>

springmvc如何設定多檢視器呢

java語言 閱讀(3.01W)

導語:springmvc如何設定多檢視器呢?下面是小編給大家整體的程式碼,大家可以參考練習,更多詳情請關注應屆畢業生考試網。

springmvc如何設定多檢視器呢

在做頁面靜態化處理時,有時候我們需要 兩種或者兩種以上的檢視解析方式,比如 jsp,html,json,jstl,ftl等等,顯然預設的 springmvc 只配置一種檢視解析方式是滿足不了我們的,但是放心,springmvc提供了配置多檢視解析的方式:

比如:一種檢視解析用來 解析 freemarker靜態化後的html,另一種檢視解析用來解析 jsp(jstl)

網上好多方式都有提到用 order 來設定解析器的優先順序,但經試驗,優先順序低的還是生效不了,不知道是否有其他的'解決方法?最終檢視原始碼才發現原來springmvc 只給我們提供了 待我們重寫的方法,看原始碼:

/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen ) ***/

package ;

import le;

import ializingBean;

public abstract class AbstractUrlBasedView extends AbstractView implements

InitializingBean {

private String url;

protected AbstractUrlBasedView() {

}

protected AbstractUrlBasedView(String url) {

= url;

}

public void setUrl(String url) {

= url;

}

public String getUrl() {

return ;

}

public void afterPropertiesSet() throws Exception {

if ((isUrlRequired()) && (getUrl() == null))

throw new IllegalArgumentException("Property 'url' is required");

}

protected boolean isUrlRequired() {

return true;

}

<span style="background-color: rgb(255, 255, 204);">public boolean checkResource(Locale locale) throws Exception {

return true;

}</span>

public String toString() {

StringBuilder sb = new StringBuilder(ring());

nd("; URL [")nd(getUrl())nd("]");

return ring();

}

}  /*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen ) ***/

package ;

import le;

import ializingBean;

public abstract class AbstractUrlBasedView extends AbstractView implements

InitializingBean {

private String url;

protected AbstractUrlBasedView() {

}

protected AbstractUrlBasedView(String url) {

= url;

}

public void setUrl(String url) {

= url;

}

public String getUrl() {

return ;

}

public void afterPropertiesSet() throws Exception {

if ((isUrlRequired()) && (getUrl() == null))

throw new IllegalArgumentException("Property 'url' is required");

}

protected boolean isUrlRequired() {

return true;

}

<span style="background-color: rgb(255, 255, 204);">public boolean checkResource(Locale locale) throws Exception {

return true;

}</span>

public String toString() {

StringBuilder sb = new StringBuilder(ring());

nd("; URL [")nd(getUrl())nd("]");

return ring();

}

}

所以怎麼做就很明確了:

  第一步:新建一個html的解析器並繼承 InternalResourceView 後重寫 checkResource

package ;

import ;

import le;

import rnalResourceView;

/**

*

* @ClassName: HtmlResourceView

* @author caixl

* @date 2016-6-8 上午11:01:41

*

*/

public class HtmlResourceView extends InternalResourceView {

@Override

public boolean checkResource(Locale locale) {

File file = new File(ervletContext()ealPath("/") + getUrl());

return ts();// 判斷該頁面是否存在

}

}

package ;

import ;

import le;

import rnalResourceView;

/**

*

* @ClassName: HtmlResourceView

* @author caixl

* @date 2016-6-8 上午11:01:41

*

*/

public class HtmlResourceView extends InternalResourceView {

@Override

public boolean checkResource(Locale locale) {

File file = new File(ervletContext()ealPath("/") + getUrl());

return ts();// 判斷該頁面是否存在

}

}

  第二步:在xml配置檔案中 指定解析器的 viewClass為該解析類

<!-- 定義HTML檔案的位置 -->

<bean id="htmlviewResolver"

class="rnalResourceViewResolver">

<property name="viewClass" value="ResourceView"/>

<property name="order" value="0" />

<property name="prefix" value="/cms/"/>

<property name="suffix" value="" />

<property name="contentType" value="text/html;charset=UTF-8"></property>

</bean>

<!-- 定義JSP檔案的位置 -->

<bean id="jspViewResolver" class="rnalResourceViewResolver">

<property name="order" value="1" />

<property name="prefix" value="/views/"/>

<property name="suffix" value=""/>

</bean>

<!-- 定義HTML檔案的位置 -->

<bean id="htmlviewResolver"

class="rnalResourceViewResolver">

<property name="viewClass" value="ResourceView"/>

<property name="order" value="0" />

<property name="prefix" value="/cms/"/>

<property name="suffix" value="" />

<property name="contentType" value="text/html;charset=UTF-8"></property>

</bean>

<!-- 定義JSP檔案的位置 -->

<bean id="jspViewResolver" class="rnalResourceViewResolver">

<property name="order" value="1" />

<property name="prefix" value="/views/"/>

<property name="suffix" value=""/>

</bean>