Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing REST and JSP in Spring MVC, Cannot find JSP

I'm sure this is a noob question, and I've spent the better part of an hour trawling stackoverflow for an answer but nobody seems to have my case so here we go...

I have a new webapp that uses Spring MVC. Most of the app (99%) is pure REST, so it doesn't have a "view" as such but rather simply sends JSON back down the wire, or sends an alternate HTTP Status for errors etc.

The exception is the login page which needs to be an actual JSP, but somehow the configuration I am using to map my REST controllers is leaving me in a state where normal JSP mappings fail.

Here's what I've got:

In my dispatcher servlet config, the relevant portions are:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
</bean>

In my attempts to get it working, I have also added a mapping to the "HomeController" which currently just redirects to my login JSP:

<bean name="/" class="com.somepackage.HomeController"/>

Now, in the web.xml I have:

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

<servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring-dispatcher-servlet.xml
    </param-value>
</context-param>

This works fine for my RESTful controllers, which look like this:

@Controller
@RequestMapping(value = "/api/user")
public class BlahBlahController {...

My "HomeController", which just looks like this:

@Controller
@RequestMapping(value = "/")
public class HomeController extends AbstractController {
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return new ModelAndView("login");
    }
}

IS triggered when I hit the "/" url, but I get this error in the logs:

WARNING: No mapping found for HTTP request with URI [/WEB-INF/pages/login.jsp] in DispatcherServlet with name 'spring-dispatcher'

Now I get what it's saying, it doesn't know how to resolve /WEB-INF/pages/login.jsp (this page does exist btw), but I'm stuck as to how I need to alter things to get this to work.

I'm a little confused on how it's supposed to work. Anyone got any clues?

Thanks.

like image 809
Jason Polites Avatar asked Apr 16 '13 06:04

Jason Polites


People also ask

Is JSP supported by Spring MVC?

Serving a Simple Web Page According to our previous configuration in application. properties, Spring MVC will look for view-books. jsp inside the /WEB-INF/jsp/ directory. The above example shows us how to use the JSTL <c:url> tag to link to external resources such as JavaScript and CSS.

Does Spring use Servlets?

Yes it is. Spring-MVC uses DispatcherServlet under the hood. Central dispatcher for HTTP request handlers/controllers, e.g. for web UI controllers or HTTP-based remote service exporters.

What is a Servlet Spring boot?

Overview. The DispatcherServlet is the front controller in Spring web applications. It's used to create web applications and REST services in Spring MVC. In a traditional Spring web application, this servlet is defined in the web. xml file.


1 Answers

OK.. I found the answer, it's the url-pattern in the dispatcher config.

Instead of:

<servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

It should be

<servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

I had actually found this answer elsewhere and tried it but "thought" it wasn't working, then realized the reason I thought this was unrelated to the root cause.

No idea why this would work and the other wouldn't.. but one problem at a time...

like image 156
Jason Polites Avatar answered Oct 29 '22 15:10

Jason Polites