springmvc如何设置多视图器呢
来源:才华咖 本文已影响2.97W人
来源:才华咖 本文已影响2.97W人
导语: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>
Spring Is Coming初中英语作文
Spring is coming小学英语作文
My Happy Times During Winter Vacation初中英语作文
Unit six Opening and Closing Ceremonies口译材料
初中英语写作范文:My Happy Times During Winter Vacation
英语阅读:VIT declines to acquire Ang Mo Kio property
关于Thieving Bird Apes Its Victims的英语作文范文
英语阅读:Covergirl unveils empowering brand update
Time To Stop Worrying About Invasive Species美文欣赏
SpringMVC教程之json交互使用
论melogin.cn路由器如何设置登录密码
JavaScript如何实现JSON.stringify
2017小学英语听力Improving Reading Skills练习
MS SQL Server Management Studio Express安装图文具体步骤
FilmsandTVProgrammes教案设计
Passage Eighteen (Strictly Ban smoking)英语阅读理解
如何设置Linksys路由器
My Spring Festival Plans英语作文
京东如何设置到货通知图解
华为HCNP-MSTP Transmission认证考试主要内容
christmas is coming的英语作文
My Spring Festival英语作文
英语作文My Experience With Foreign Coins
英语阅读:TheTwelveDancingPrincesses
如何设置标准试卷格式
javascript中String对象的slice()方法
短视频剪辑就业前景如何呢?
Why Public Speaking Is So Important 公共演说的重要性大学英语
Cisco路由器里SNMP的配置方法
An Exciting Sports Meeting英语作文
My Spring Festival为题目的作文
My Spring Festival的英语作文
Efforts Urged to Improve Womens Reproductive Health英语阅读
The spring is in my mind英语作文
英语文化阅读:the only surviving calligraphy
英语作文my spring festival
Chinese spring festival英语作文
英语高分作文:The importance of protecting the environment
英语作文:HelpMyFatherGiveUpSmoking
Christmas Is Coming为题目的作文