[Spring]配置多个视图解析器

xiaoxiao2021-02-27  302

第一步:spring.xml配置多个试图解析器

<beans:bean id="htmlViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="viewClass" value="test.jia.com.view.HtmlResourceView"/> <beans:property name="order" value="0" /> <beans:property name="prefix" value="/WEB-INF/pages/" /> <beans:property name="suffix" value=".html" /> <beans:property name="contentType" value="text/html;charset=UTF-8"/> </beans:bean> <beans:bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="order" value="1" /> <beans:property name="prefix" value="/WEB-INF/pages/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean>

第二步:重写视图解析器

import java.io.File; import java.util.Locale; import org.springframework.web.servlet.view.InternalResourceView; /** * Spring视图解析器 * @author Jia * */ public class HtmlResourceView extends InternalResourceView { @Override public boolean checkResource(Locale locale) { File file = new File(this.getServletContext().getRealPath("/") + getUrl()); return file.exists(); } }

注:web.xml中,DispatcherServlet要配置

<servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping>
转载请注明原文地址: https://www.6miu.com/read-3335.html

最新回复(0)