Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I map a spring controller to a url with .jsp extension?

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).

We are doing this iteratively, so some pages need to stay jsp files (calls to welcome.jsp won't be handled by a controller).

To do that I mapped both the DispatcherDervlet and the HandlerMapping to *.jsp, and configured the JstlView in the standard way.

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'>

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:

<!-- 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>

Thanks a lot.

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.

like image 896
Matteo Caprari Avatar asked May 04 '10 10:05

Matteo Caprari


People also ask

How do I map a JSP page in Web XML?

jsp . If you want more control over how the JSP is mapped to a URL, you can specify the mapping explicitly by declaring it with a <servlet> element in the deployment descriptor. Instead of a <servlet-class> element, you specify a <jsp-file> element with the path to the JSP file from the WAR root.

Where should I put JSP files in spring boot?

As per convention, we place our JSP files in the ${project. basedir}/main/webapp/WEB-INF/jsp/ directory.


2 Answers

[blatantly stolen from http://forum.springsource.org/showthread.php?13263-Using-.jsp-extension]

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>

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.

like image 66
James Beninger Avatar answered Sep 27 '22 23:09

James Beninger


if it's really not working with .jsp extensions (although i can't personally see any reason for that), you could try using http://tuckey.org/urlrewrite/ to do a rewrite of the urls first.

like image 42
chris Avatar answered Sep 27 '22 23:09

chris