user1889665
user1889665

Reputation:

spring mvc - how to map multiple subfolders

Spring MVC: I have my jsp files in path WEB-INF/jsp. I want to get the url
_http://mydomainname:8080/igloo/sports/scoreboard to resolve using spring mvc. The jsp page is under /WEB-INF/jsp/sports/scoreboard.jsp

Here are the details of the installation, the error I am getting the at the bottom:

WEB.XML:
<servlet-mapping>
    <servlet-name>springapp</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

springapp-servlet.xml:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
    <property name="prefix" value="WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

@RequestMapping({"/sports/scoreboard"})
public String scoreboard(Map<String,Object> model, HttpServletRequest request, HttpServletResponse response)
{
    return "sports/scoreboard";
}

System File Path:
WEB-INF/jsp/sports/scoreboard.jsp

Browser Url:
http://localhost:8080/igloo/sports/scoreboard


Error:
    HTTP Status 404 - /igloo/sports/WEB-INF/jsp/sports/scoreboard.jsp

Upvotes: 2

Views: 3726

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280168

Simply add a leading / to your InternalResourceViewResolver's prefix.

<property name="prefix" value="/WEB-INF/jsp/"></property>

Internally, the JstlView uses HttpServletRequest#getRequestDispatcher(String) to locate the jsp. Note the javadoc

The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root.

So if you have no leading /, it is relative to your current path, which happens to be /sports, so

sports/WEB-INF/jsp/sports/scoreboard.jsp

for which your application has no mapping and therefore returns a 404.

Upvotes: 4

Related Questions