Alex Spurling
Alex Spurling

Reputation: 55924

Introducing Spring MVC

I'd like to introduce Spring MVC to an application that has up till now used simple direct access to JSP files i.e www.example.com/login.jsp which contains the business logic and presentation details.

I'd like to strip out the business logic and keep only the presentation in the JSP. To do this, I've moved the jsp file from webapp/login.jsp to webapp/WEB-INF/jsp/login.jsp and then mapped all urls with the pattern *.jsp to Spring's DispatchServlet which goes to a Controller and then a view which (should) forward to /WEB-INF/jsp/login.jsp.

The web.xml looks like this:

<servlet>
    <servlet-name>springapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>springapp</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>

springapp-context.xml

<bean id="urlFilenameViewController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <value>
            /*.jsp=urlFilenameViewController
        </value>
    </property>
</bean>

<bean id="viewResolver" 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"></property>
</bean>

However, the fundamental problem with this is that I'm mapping external URL requests ending in .jsp to internal web requests that also map to .jsp. The internal .jsp request then goes through the DispatchServlet for a second time which returns an error as it cannot find anything to handle the request:

WARN - DispatcherServlet.noHandlerFound(1077) | No mapping found for HTTP request with URI [/myapp/WEB-INF/jsp/login.jsp] in DispatcherServlet with name 'springapp'

Given that I cannot change the external URLs, is there a way to get round this issue when mapping external file types to the same internal file type?

Upvotes: 0

Views: 1992

Answers (3)

stephen.hanson
stephen.hanson

Reputation: 9624

I'm not sure how to tell Spring to ignore its internal requests for JSP files, but intercept other JSP requests. Why don't you keep your old jsps, but just have them forward to a controller. E.g. "page.jsp":

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<c:redirect url="/page.htm" />

This way, you can keep your old URLs intact, but the only function is to redirect to the controllers.

Upvotes: 0

Robert Munteanu
Robert Munteanu

Reputation: 68318

I suggest you:

  • map Spring MVC requests to a different pattern ( e.g *.do);
  • use a UrlRewriteFilter or your application server's url rewrite functionality to map *.jsp calls to *.do;

Upvotes: 0

Rob H
Rob H

Reputation: 15010

We address this in our application by using a different pattern for request URLs (*.htm instead of *.jsp). This avoids the problem and it is good practice anyway because there may not be a 1-to-1 relationship between a URL and a JSP.

Upvotes: 2

Related Questions