1

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 "/". ;)

Bananan
  • 613
  • 5
  • 19

2 Answers2

1

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" />
Abderrazak BOUADMA
  • 1,526
  • 2
  • 16
  • 38
  • Thanks, for your answer. Accordring to first part of question I thought the same but when I put something different than "/testing" in annotation value then request to `http://localhost:8080/app/testing` doesn't work and I have to add that new value after /. That's the reason why I don't understand such behaviour. Regarding second part static resources are located inside WEB-INF directory and, as I wrote before - there isn't any issue reported by developer tool like missing file or something like that. When I want to manually access static resources I'm redirected to doView.jsp page. – Bananan Nov 13 '13 at 15:23
  • I usually put my static resources (assets) like images, css, ... within $root/webapp/resources as WEB-INF is a private/protected directory this means, when ever you'll try to access to resources within WEB-INF manually (through URL) you'll be denied and redirected (in your case) to default view which is in your case doView.jsp as you defined that inside your controller – Abderrazak BOUADMA Nov 13 '13 at 16:58
  • Till now I always put such files inside WEB-INF/resources and used mvc:resources to make them available for requests but there was one difference in web.xml descriptor - url-pattern was set to "/" not to "/anyValue/*". But for this application I can't do that and I'm trying to find any solution for this issue. I'll try your answer. Moreover, I have one question, why do you specify location as `/WEB-INF/public-resources/` when your files are in public-resources directory that is on the same filesystem's level as WEB-INF - directly inside webapp directory? – Bananan Nov 14 '13 at 09:52
0

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

indivisible
  • 4,892
  • 4
  • 31
  • 50