Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在不破坏资源映射的情况下隐式映射文件夹视图?_Java_Spring_Spring Mvc - Fatal编程技术网

Java 如何在不破坏资源映射的情况下隐式映射文件夹视图?

Java 如何在不破坏资源映射的情况下隐式映射文件夹视图?,java,spring,spring-mvc,Java,Spring,Spring Mvc,我正在尝试为spring web应用程序设置一个非常简单的基本配置,该应用程序可以处理以下情况: 将资源根请求映射到/index,例如/映射到/index,/resource/映射到/resource/index 将/static/**映射到/static/(这是一个资源视图-css、js、图像) 使用控制器映射处理某些特定的请求路径 将所有其他请求映射到基于url的视图,例如/resource/page映射到/WEB-INF/views/resource/page.jsp 在我当前的配置中

我正在尝试为spring web应用程序设置一个非常简单的基本配置,该应用程序可以处理以下情况:

  • 将资源根请求映射到
    /index
    ,例如
    /
    映射到
    /index
    /resource/
    映射到
    /resource/index
  • /static/**
    映射到
    /static/
    (这是一个资源视图-css、js、图像)
  • 使用控制器映射处理某些特定的请求路径
  • 将所有其他请求映射到基于url的视图,例如
    /resource/page
    映射到
    /WEB-INF/views/resource/page.jsp
在我当前的配置中,我拥有的是:

<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<mvc:resources mapping="/static/**" location="/static/"/>
<mvc:view-controller path="/" view-name="index"/>

有没有办法同时做所有这些事情?它不需要是仅xml的解决方案,我可以使用代码配置或混合解决方案。

我通过实现自己的
控制器
配置
找到了一个解决方案

查看servlet.xml

<mvc:resources mapping="/static/**" location="/static/"/>
<context:component-scan base-package="com.example.web.view"/>
com.example.web.view.ViewController

@Controller
public class ViewController {

    @RequestMapping(value = "**/")
    public String all(HttpServletRequest request) {     
        String path = request.getRequestURI().replace(request.getContextPath(),"");
        return path.endsWith("/") ? path +"index" : path;
    }

}
神奇之处在于在
ViewConfig
中设置适配器上的顺序值。Spring创建了一个默认的映射处理程序,组件扫描找到的所有
RequestMapping
s都被添加到该处理程序中。我的初始设置失败的原因是,此处理程序的顺序要在由
mvc:resources
注册的处理程序之前匹配。创建我自己的适配器并将其顺序设置为在
mvc:resources
之后处理,这两者都可以工作

有关这方面的信息可以在spring文档中找到

当然,ViewConfig也可以完全用xml实现:

<mvc:resources mapping="/static/**" location="/static/"/>

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="order" value="2"/>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<context:component-scan base-package="com.example.web.view"/>

更新:
DefaultAnnotationHandlerMapping
AnnotationMethodHandlerAdapter
更改为
RequestMappingHandlerMapping
RequestMappingHandlerAdapter
,以反映Spring 3.1.x及更高版本中所做的更改

@Controller
public class ViewController {

    @RequestMapping(value = "**/")
    public String all(HttpServletRequest request) {     
        String path = request.getRequestURI().replace(request.getContextPath(),"");
        return path.endsWith("/") ? path +"index" : path;
    }

}
<mvc:resources mapping="/static/**" location="/static/"/>

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="order" value="2"/>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<context:component-scan base-package="com.example.web.view"/>