Spring controller mapping configuration and static resources
I have a problem intercepting a request for static resources by a controller.
Here is the web.xml (part related to 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 test-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 the source code for the controller class:
@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 files doView.jsp and getView.jsp with a static file declaration with JavaScript:
<script src="testing/resources/js/jquery.js"></script>
One thing I don't understand is why I can get doView.jsp by typing only http://localhost:8080/app/testing
, but in order to get getView.jsp I need to type http://localhost:8080/app/testing/testing/getSomething
("test" typed twice).
And now the main reason for this thread is when I remove the query mapping annotation from the class definition ( @RequestMapping(value = "/testing")
) and leave them on the methods, I can't get the jquery.js file at all. When I type http://localhost:8080/app/testing/resources/js/jquery.js
I get doView.jsp. There is no problem in the browser with the developer post (missing jquery.js file or whatever) - this request is simply intercepted by the Spring dispatcher servlet. The only advantage with this configuration is that I don't have to type "test" twice to open getView.jsp.;)
Does anyone know how to make a tag mvc:resources
work in a situation like this? And no, I can't set the url of the whole servlet for testing to "/".;)
source to share
first the first part of your question is normal behavior. you declared /testing/*
as a url template for your dispatcher servlet, which means that all "things" after / testing / are considered by spring and therefore intercepted. you added the annotation @RequestMapping
and you fill in the value parameter testing
, which can be confusing. You can use / ANOTHER_NAME like url-pattern
instead of testing and displaying the query mapping by defining your controller as testing .
for the second part, it seems to me you put the js file in / src / main / resources which is a private protected folder, you might consider putting it in / src / webapp / public-resources , then configure in the following way:
<mvc:resources mapping="/resources/**"
location="/, classpath:/WEB-INF/public-resources/"
cache-period="10000" />
source to share