如何将弹簧控制器映射到扩展名为.jsp的URL? [英] How can I map a spring controller to a url with .jsp extension?
问题描述
我们正在将仅限jsp的应用程序迁移到Spring-MVC。由于各种原因,我们无法更改当前页面的扩展名。 (对login.jsp的调用需要由将使用jsp文件作为视图的spring控制器处理。)
We are in the process of migrating a jsp-only application to Spring-MVC. For various reasons we can't change the extension of the current pages. (calls to login.jsp need to handled by a spring controller that will use a jsp file as view).
我们正在迭代地执行此操作,因此某些页面需要保留jsp文件(对welcome.jsp的调用不会由控制器处理)。
We are doing this iteratively, so some pages need to stay jsp files (calls to welcome.jsp won't be handled by a controller).
为此,我将DispatcherDervlet和HandlerMapping映射到* .jsp,并且以标准方式配置JstlView。
To do that I mapped both the DispatcherDervlet and the HandlerMapping to *.jsp, and configured the JstlView in the standard way.
不幸的是,如果我浏览到//login.jsp,我会收到错误消息
Unfortunately, if I browse to //login.jsp I get an error saying
<No mapping found for HTTP request with URI [/<context>/WEB-INF/jsp/login.jsp] in DispatcherServlet with name 'spring'>
如果我将.jsp更改为DispatcherServlet和HandlerMapping中的任何其他扩展名,则一切正常。
It all works if I change .jsp to any other extension in DispatcherServlet and HandlerMapping.
web.xml:
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
spring-servlet.xml:
spring-servlet.xml:
<!-- View resolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- URL Mapping -->
<bean id="publicUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/login.jsp" value-ref="loginController"/>
</map>
</property>
</bean>
非常感谢。
UPDATE :我刚刚验证过,如果我将.jsp文件重命名为其他(.jst)并相应地更新viewResolver,那么一切正常。显然,如果视图被解析为扩展名为.jsp的文件,则spring会尝试将视图转发给另一个控制器。
UPDATE: I just verified that if I rename my .jsp files to something else (.jst) and update the viewResolver accordingly, than it all works. Apparently if the view is resolved to a file with extension .jsp, spring tries to forward the view to another controller.
推荐答案
[公然偷偷从 http://forum.springsource.org/showthread.php ?13263-Using-.jsp-extension ]
这对我有用。尝试将此添加到您的web.xml文件中:
This worked for me. Try adding this to your web.xml file:
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>/WEB-INF/jsp/*</url-pattern>
</servlet-mapping>
请注意,即使链接中的信息,我也不明白为什么这有帮助。如果一些Spring专家可以匆匆解释,我很想知道。
Please note that even with the information in the link, I don't understand why this is helping. If some Spring expert could drop by and explain it, I'd love to know.
我也不能保证这可能造成潜在的安全/可靠性问题,所以使用风险自负。
I also can't guarantee that there are no potential security/reliability issues this could create, so use at your own risk.
这篇关于如何将弹簧控制器映射到扩展名为.jsp的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!