Spring controller mapping configuration and static resources

本小妞迷上赌 提交于 2019-12-13 01:27:22

问题


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


回答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" />



回答2:


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



来源:https://stackoverflow.com/questions/19955117/spring-controller-mapping-configuration-and-static-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!