简体   繁体   English

Spring控制器映射配置和静态资源

[英]Spring controller mapping configuration and static resources

I have an issue with intercepting request to static resources by controller. 我在通过控制器拦截对静态资源的请求时遇到问题。

Here is web.xml (part related with the problem): 这是web.xml(与问题有关的部分):

<servlet>
    <servlet-name>testing</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>testing</servlet-name>
    <url-pattern>/testing/*</url-pattern>
</servlet-mapping>

Here is testing-servlet.xml: 这是testing-servlet.xml:

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

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

Here is controller class source code: 这是控制器类的源代码:

@Controller
@RequestMapping(value = "/testing")
public class TestingController {
    @RequestMapping(method = RequestMethod.GET)
    public String doSomething() {
        return "doView";
    }

    @RequestMapping(value = "/getSomething", method = RequestMethod.GET)
    public String getSomething(@RequestParam String id) {
        return "getView";
    }
}

And the last snipet of doView.jsp and getView.jsp files with declaration of static file with JavaScript: 以及使用JavaScript声明静态文件的doView.jsp和getView.jsp文件的最后一个片段:

<script src="testing/resources/js/jquery.js"></script>

There is one thing I don't understand - why do I get doView.jsp by entering only http://localhost:8080/app/testing but in order to get getView.jsp I need to enter http://localhost:8080/app/testing/testing/getSomething ("testing" typed twice). 我不了解一件事-为什么只输入http://localhost:8080/app/testing来获取doView.jsp,但是要获取getView.jsp,我需要输入http://localhost:8080/app/testing/testing/getSomething (两次键入“ testing”)。

And now the main reason of this topic - when I remove request mapping annotation from class definition ( @RequestMapping(value = "/testing") and leave those on methods then I'm not able to get jquery.js file at all. When I type http://localhost:8080/app/testing/resources/js/jquery.js I get doView.jsp. There isn't any issue reported by developer tool in browser (missing jquery.js file or something) - this request is just intercepted by Spring's dispatcher servlet. The only advantage in this configuration is that I don't have to type "testing" twice in order to open getView.jsp. ;) 现在,这个主题的主要原因是-当我从类定义中删除请求映射注释( @RequestMapping(value = "/testing")并将那些保留在方法上时,我根本无法获取jquery.js文件。我输入http://localhost:8080/app/testing/resources/js/jquery.js我得到doView.jsp,浏览器中的开发人员工具没有报告任何问题(缺少jquery.js文件或其他内容)-这请求只是被Spring的调度程序servlet拦截。这种配置的唯一优点是,我不必两次键入“测试”即可打开getView.jsp。

Does anyone know solution how to make mvc:resources tag working in such situation? 有谁知道解决方案如何使mvc:resources标签在这种情况下工作? And no I can't set URL mapping of whole testing servlet to "/". 而且,我不能将整个测试servlet的URL映射设置为“ /”。 ;) ;)

first the first part of you question, this is a normal behaviour. 首先是您提问的第一部分,这是正常现象。 you declared /testing/* as url-pattern for your Dispatcher servlet which means that all "things" appearing after the /testing/ is considered by spring and so intercepted. 您将/testing/*声明为Dispatcher servlet的url-pattern,这意味着spring会考虑/ testing /之后出现的所有“事物”,因此会被拦截。 you had then add a @RequestMapping annotation and you fill the value parameter of it with testing which may lead to a confusion. 然后添加了@RequestMapping批注,并使用testing填充了它的value参数,这可能会导致混淆。 You may consider to use /ANOTHER_NAME as url-pattern instead of testing and keey the request mapping over your controller definition as testing . 您可以考虑使用/ ANOTHER_NAME作为url-pattern ,而不是测试和keey在你的控制器定义测试请求映射。

for the second part , it seems to me you put you js file within /src/main/resources which is a privte secured folder, you may consider to put it within /src/webapp/public-resources then configure your 对于第二部分,在我看来,您将js文件放在/ src / main / resources中 ,这是一个受保护的私有文件夹,您可以考虑将其放在/ src / webapp / public-resources中,然后配置 as follow : 如下 :

<mvc:resources mapping="/resources/**"
           location="/, classpath:/WEB-INF/public-resources/"
           cache-period="10000" />

Please add this jar 请添加这个罐子

<mvc:resources mapping="/resources/**" location="/resources/"/>
<mvc:annotation-driven/>

this jar will automatically download in maven/pom.xml structure but in case of your own lib then you have to put this jar hamcrest-core-1.3.jar 这个jar会自动以maven / pom.xml结构下载,但是如果您有自己的lib,则必须将这个jar hamcrest-core-1.3.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM