- 相關(guān)推薦
springmvc如何設(shè)置多視圖器呢
導(dǎo)語:springmvc如何設(shè)置多視圖器呢?下面是小編給大家整體的代碼,大家可以參考練習(xí),更多詳情請關(guān)注應(yīng)屆畢業(yè)生考試網(wǎng)。
在做頁面靜態(tài)化處理時(shí),有時(shí)候我們需要 兩種或者兩種以上的視圖解析方式,比如 jsp,html,json,jstl,ftl等等,顯然默認(rèn)的 springmvc 只配置一種視圖解析方式是滿足不了我們的,但是放心,springmvc提供了配置多視圖解析的方式:
比如:一種視圖解析用來 解析 freemarker靜態(tài)化后的html,另一種視圖解析用來解析 jsp(jstl)
網(wǎng)上好多方式都有提到用 order 來設(shè)置解析器的優(yōu)先級,但經(jīng)試驗(yàn),優(yōu)先級低的還是生效不了,不知道是否有其他的解決方法?最終查看源碼才發(fā)現(xiàn)原來springmvc 只給我們提供了 待我們重寫的方法,看源碼:
/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package org.springframework.web.servlet.view;
import java.util.Locale;
import org.springframework.beans.factory.InitializingBean;
public abstract class AbstractUrlBasedView extends AbstractView implements
InitializingBean {
private String url;
protected AbstractUrlBasedView() {
}
protected AbstractUrlBasedView(String url) {
this.url = url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUrl() {
return this.url;
}
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(super.toString());
sb.append("; URL [").append(getUrl()).append("]");
return sb.toString();
}
} /*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package org.springframework.web.servlet.view;
import java.util.Locale;
import org.springframework.beans.factory.InitializingBean;
public abstract class AbstractUrlBasedView extends AbstractView implements
InitializingBean {
private String url;
protected AbstractUrlBasedView() {
}
protected AbstractUrlBasedView(String url) {
this.url = url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUrl() {
return this.url;
}
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(super.toString());
sb.append("; URL [").append(getUrl()).append("]");
return sb.toString();
}
}
所以怎么做就很明確了:
第一步:新建一個(gè)html的解析器并繼承 InternalResourceView 后重寫 checkResource
package com.izhbg.typz.common.springmvc.view;
import java.io.File;
import java.util.Locale;
import org.springframework.web.servlet.view.InternalResourceView;
/**
*
* @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(this.getServletContext().getRealPath("/") + getUrl());
return file.exists();// 判斷該頁面是否存在
}
}
package com.izhbg.typz.common.springmvc.view;
import java.io.File;
import java.util.Locale;
import org.springframework.web.servlet.view.InternalResourceView;
/**
*
* @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(this.getServletContext().getRealPath("/") + getUrl());
return file.exists();// 判斷該頁面是否存在
}
}
第二步:在xml配置文件中 指定解析器的 viewClass為該解析類
<!-- 定義HTML文件的位置 -->
<bean id="htmlviewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="com.izhbg.typz.common.springmvc.view.HtmlResourceView"/>
<property name="order" value="0" />
<property name="prefix" value="/cms/"/>
<property name="suffix" value=".html" />
<property name="contentType" value="text/html;charset=UTF-8"></property>
</bean>
<!-- 定義JSP文件的位置 -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="1" />
<property name="prefix" value="/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 定義HTML文件的位置 -->
<bean id="htmlviewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="com.izhbg.typz.common.springmvc.view.HtmlResourceView"/>
<property name="order" value="0" />
<property name="prefix" value="/cms/"/>
<property name="suffix" value=".html" />
<property name="contentType" value="text/html;charset=UTF-8"></property>
</bean>
<!-- 定義JSP文件的位置 -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="1" />
<property name="prefix" value="/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
【springmvc如何設(shè)置多視圖器呢】相關(guān)文章:
路由器如何設(shè)置wifi密碼08-10
無限路由器如何設(shè)置連接10-01
如何設(shè)置電腦IP連接路由器上網(wǎng)07-05
如何設(shè)置磊科無線路由器04-16
tplink路由器如何設(shè)置靜態(tài)IP地址09-09
如何檢測和設(shè)置路由器MTU值10-24
如何安裝和設(shè)置無線路由器06-29