【发布时间】:2013-11-26 03:11:45
【问题描述】:
我在拦截控制器对静态资源的请求时遇到问题。
这里是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";
}
}
doView.jsp 和 getView.jsp 文件的最后一段使用 JavaScript 声明静态文件:
<script src="testing/resources/js/jquery.js"></script>
有一件事我不明白 - 为什么我只输入 http://localhost:8080/app/testing 会得到 doView.jsp 但为了得到 getView.jsp 我需要输入 http://localhost:8080/app/testing/testing/getSomething (“测试”键入两次)。
现在这个主题的主要原因 - 当我从类定义中删除请求映射注释(@RequestMapping(value = "/testing") 并将它们留在方法上时,我根本无法获取 jquery.js 文件。当我输入 @987654328 @我得到doView.jsp。浏览器中的开发人员工具没有报告任何问题(缺少jquery.js文件或其他东西)-这个请求只是被Spring的调度程序servlet拦截了。这个配置的唯一优点是我没有'不必输入“测试”两次才能打开 getView.jsp。;)
有谁知道如何使mvc:resources 标签在这种情况下工作?不,我不能将整个测试 servlet 的 URL 映射设置为“/”。 ;)
【问题讨论】:
-
/ in servlet-mapping -
正如我所写的——我不想这样做,因为我做不到。该应用程序中有更多 servlet。
标签: java spring spring-mvc url-pattern static-resource