java - 直接读取 index.html 而不在 URL 中暴露其路径

标签 java spring spring-mvc

我有一个结构如下的 index.html 文件。

WEB-INF/dbName/type/id/index.html

另外,同路径下还有外部css和js文件。

我需要访问这个文件而不在浏览器中暴露它的路径。

下面是我的 Controller 类:

@Controller
public class MyController {

    private static final Logger logger = LoggerFactory.getLogger(MyController.class);

    @RequestMapping(value = "/{type}/{id}", method = RequestMethod.GET, produces = MediaType.ALL_VALUE)
    public String pod(HttpServletRequest request, @PathVariable String type, @PathVariable String id)
            throws IOException {
        String redirectUrl = "/dbName/" + type + "/" + id + "/index.html";
        return "redirect:" + redirectUrl;
    }
}

servlet-dispatcher.xml 中的资源映射:

<mvc:resources mapping="/dbName/**" location="/WEB-INF/dbName/" />

生成的 URL 如下所示:localhost:8080/project/dbName/type/id/index.html

但是,预期的 URL 是:localhost:8080/project/type/id/

此代码在浏览器中公开资源的 URL。然后,我尝试使用 forward 而不是 redirect。然后它不会暴露 URL,但不会加载 index.html 中使用的外部 CSS 和 js。

如有任何帮助,我们将不胜感激。

最佳答案

View 解析器的契约指定 View 解析器可以返回 null 以指示找不到 View 。然而,并非所有 View 解析器都这样做,因为在某些情况下,解析器根本无法检测 View 是否存在。例如,InternalResourceViewResolver 内部使用 RequestDispatcher,调度是判断 JSP 是否存在的唯一方法,但此操作只能执行一次。这同样适用于 VelocityViewResolver 和其他一些。检查特定 View 解析器的 javadoc 以查看它是否报告不存在的 View 。因此,将 InternalResourceViewResolver 放在链中的最后一个位置以外的位置会导致链中未被完全检查,因为 InternalResourceViewResolver 将始终返回一个 View !

重定向到 View

As mentioned previously, a controller typically returns a logical view name, which a view resolver resolves to a particular view technology. For view technologies such as JSPs that are processed through the Servlet or JSP engine, this resolution is usually handled through the combination of InternalResourceViewResolver and InternalResourceView, which issues an internal forward or include via the Servlet API’s RequestDispatcher.forward(..) method or RequestDispatcher.include() method. For other view technologies, such as Velocity, XSLT, and so on, the view itself writes the content directly to the response stream.

重定向 View

One way to force a redirect as the result of a controller response is for the controller to create and return an instance of Spring’s RedirectView. In this case, DispatcherServlet does not use the normal view resolution mechanism. Rather because it has been given the (redirect) view already, the DispatcherServlet simply instructs the view to do its work. The RedirectView in turn calls HttpServletResponse.sendRedirect() to send an HTTP redirect to the client browser.

如果你使用 RedirectView 并且 View 是由 Controller 自己创建的,建议你配置将重定向 URL 注入(inject)到 Controller 中,这样它就不会被烘焙到 Controller 中,而是与 View 一起配置在上下文中名字。名为“重定向:前缀”的部分有助于实现这种解耦。

重定向:前缀

While the use of RedirectView works fine, if the controller itself creates the RedirectView, there is no avoiding the fact that the controller is aware that a redirection is happening. This is really suboptimal and couples things too tightly. The controller should not really care about how the response gets handled. In general it should operate only in terms of view names that have been injected into it.

特殊的 redirect: 前缀可以让你完成这个。如果返回的 View 名称具有前缀 redirect:,则 UrlBasedViewResolver(和所有子类)会将此识别为需要重定向的特殊指示。 View 名称的其余部分将被视为重定向 URL。

最终效果与 Controller 返回 RedirectView 相同,但现在 Controller 本身可以简单地根据逻辑 View 名称进行操作。诸如 redirect:/myapp/some/resource 之类的逻辑 View 名称将相对于当前 Servlet 上下文进行重定向,而诸如 redirect: http://myhost.com/some/arbitrary/path 之类的名称将重定向。将重定向到一个绝对 URL。

前向:前缀

It is also possible to use a special forward: prefix for view names that are ultimately resolved by UrlBasedViewResolver and subclasses. This creates an InternalResourceView (which ultimately does a RequestDispatcher.forward()) around the rest of the view name, which is considered a URL. Therefore, this prefix is not useful with InternalResourceViewResolver and InternalResourceView (for JSPs for example). But the prefix can be helpful when you are primarily using another view technology, but still want to force a forward of a resource to be handled by the Servlet/JSP engine. (Note that you may also chain multiple view resolvers, instead.)

As with the redirect: prefix, if the view name with the forward: prefix is injected into the controller, the controller does not detect that anything special is happening in terms of handling the response.

文档:https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html

关于java - 直接读取 index.html 而不在 URL 中暴露其路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41887003/

相关文章:

java - Xpath 从错误节点返回值

java - 尝试在字符串数组中查找字符串元素

java - SOAP 响应编码所有字符串中的 '?' 个字符,而不是俄语 .Net 代理、Java 服务器(?)

spring - 忽略选择性实体字段的 spring mvc JSR-303 验证

java - 类型 'org/springframework/http/MediaType'(当前帧,堆栈 [1])不可分配给 'org/springframework/util/MimeType'

java - Spring MVC - 在 Controller 中默认设置值

java - JSP 临时存储重复数据库结果

java - Spring MVC, hibernate : Found shared references to collection when retreiving data

java - 如何在一个jsp页面引用另一个jsp页面?

java - 即使有数据,JPQL 也不返回结果