Spring控制器映射配置和静态资源 [英] Spring controller mapping configuration and static resources
问题描述
我在通过控制器拦截对静态资源的请求时遇到问题.
这是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>
这里是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>
这是控制器类源代码:
@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";
}
}
以及使用JavaScript声明静态文件的doView.jsp和getView.jsp文件的最后一个片段:
<script src="testing/resources/js/jquery.js"></script>
有一件事我不理解-为什么我只输入http://localhost:8080/app/testing
来获取doView.jsp,但是为了获得getView.jsp,我需要输入http://localhost:8080/app/testing/testing/getSomething
("testing"键入两次)./p>
现在是该主题的主要原因-当我从类定义(@RequestMapping(value = "/testing")
中删除请求映射注释,并将其保留在方法上时,我根本无法获取jquery.js文件.当我键入
有人知道解决方案如何使mvc:resources
标签在这种情况下工作吗?而且,我不能将整个测试servlet的URL映射设置为"/". ;)
首先是您问题的第一部分,这是正常现象.您已将/testing/*
声明为Dispatcher servlet的url-pattern,这意味着spring会考虑/testing/之后出现的所有事物",因此会被拦截.然后添加了@RequestMapping
批注,并用testing
填充了它的value参数,这可能会引起混淆.您可能考虑将/ANOTHER_NAME 用作url-pattern
而不是进行测试,并且将对控制器定义的请求映射为 testing .
对于第二部分,在我看来,您已将js文件放在privte安全文件夹/src/main/resources 中,您可以考虑将其放在/src中/webapp/public-resources ,然后按照以下步骤配置 :
<mvc:resources mapping="/resources/**"
location="/, classpath:/WEB-INF/public-resources/"
cache-period="10000" />
I have an issue with intercepting request to static resources by controller.
Here is web.xml (part related with the problem):
<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:
<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:
<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).
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. ;)
Does anyone know solution how to make mvc:resources
tag working in such situation? And no I can't set URL mapping of whole testing servlet to "/". ;)
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. you had then add a @RequestMapping
annotation and you fill the value parameter of it with testing
which may lead to a confusion. You may consider to use /ANOTHER_NAME as url-pattern
instead of testing and keey the request mapping over your controller definition as testing.
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 as follow :
<mvc:resources mapping="/resources/**"
location="/, classpath:/WEB-INF/public-resources/"
cache-period="10000" />
这篇关于Spring控制器映射配置和静态资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!