[英]How to handle the html and jsp pages in spring mvc from the same URI pattern?
we usually use different URI for HTML and JSP like我们通常对 HTML 和 JSP 使用不同的 URI,例如
"/static/help" for mapping HTML pages
"/jsp/deposit" for mapping jsp pages
How can i map both HTML and JSP from same URI like我怎么能 map HTML 和 JSP 来自同一个 URI
"/help" should map to "help.html"
"/deposit" should map to "deposit.jsp"
Suppose i have below URI(without extension) which needs to be mapped to given location假设我有以下需要映射到给定位置的 URI(无扩展名)
and also i don't want to put extensions like.html or.jsp in return value of controller's view name since it is not the best practice.而且我也不想在控制器视图名称的返回值中添加.html 或.jsp 这样的扩展名,因为这不是最佳做法。
How can i achieve it?我怎样才能实现它?
dispatcher servlet url mapping- "/"调度程序 servlet url 映射-“/”
Request Controller's Physical
URI logical File
view name location
------ ---------- ------------
"/" "home" WEB-INF/common/home.html
"/help" "help" WEB-INF/common/help.html
"/deposit" "deposit" WEB-INF/app/deposit.jsp
"/withdraw" "withdraw" WEB-INF/app/withdraw.jsp
Try this: Keep your suffix empty 尝试以下操作:保持后缀为空
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/yourDirectory/"/>
<property name="suffix" value=""/>
</bean>
And the in the controller 而在控制器
@RequestMapping("/home")
public String home() {
return "home.html";
}
@RequestMapping("/deposit")
public String deposit() {
return "deposit.jsp";
}
Step1第1步
Create A Class that extends org.springframework.web.servlet.view.InternalResourceView创建一个扩展 org.springframework.web.servlet.view.InternalResourceView 的 Class
Step2 Override the method checkResource Step2 重写方法 checkResource
The final Class will be like this:最终的 Class 会是这样的:
package com.hanz.view;
import org.springframework.web.servlet.view.InternalResourceView;
import java.io.File;
import java.util.Locale;
public class HtmlResourceView extends InternalResourceView {
@Override
public boolean checkResource(Locale locale) {
File file = new File(this.getServletContext().getRealPath("/") + getUrl());
return file.exists();
}
}
Step3 Make the config right Step3 正确配置
Open the spring-mvc.xml,add this class's bean config,like this:打开spring-mvc.xml,添加这个类的bean配置,像这样:
<bean id="viewResolverHtml" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="com.seeyon.wxapps.base.utils.view.HtmlResourceView"/>
<property name="contentType" value="text/html; charset=utf-8"/>
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".html"/>
<property name="order" value="3"/>
</bean>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.