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>