![](/img/trans.png)
[英]How do I access values from forms on different JSP pages in the same controller in Spring MVC?
[英]How to handle the html and jsp pages in spring mvc from the same URI pattern?
我们通常对 HTML 和 JSP 使用不同的 URI,例如
"/static/help" for mapping HTML pages
"/jsp/deposit" for mapping jsp pages
我怎么能 map HTML 和 JSP 来自同一个 URI
"/help" should map to "help.html"
"/deposit" should map to "deposit.jsp"
假设我有以下需要映射到给定位置的 URI(无扩展名)
而且我也不想在控制器视图名称的返回值中添加.html 或.jsp 这样的扩展名,因为这不是最佳做法。
我怎样才能实现它?
调度程序 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
尝试以下操作:保持后缀为空
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/yourDirectory/"/>
<property name="suffix" value=""/>
</bean>
而在控制器
@RequestMapping("/home")
public String home() {
return "home.html";
}
@RequestMapping("/deposit")
public String deposit() {
return "deposit.jsp";
}
第1步
创建一个扩展 org.springframework.web.servlet.view.InternalResourceView 的 Class
Step2 重写方法 checkResource
最终的 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 正确配置
打开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.