springmvc如何设置多视图器呢
- java语言
- 关注:2.83W次
导语: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>
- 文章版权属于文章作者所有,转载请注明 https://xuezhezhai.com/zh-sg/jsj/java/qrk3ez.html