Pergunta

I am trying to make an MVC project using gradle and Spring4.

@Bean
public UrlBasedViewResolver viewResolver() {
  InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  resolver.setPrefix("/WEB-INF/jsp/");
  resolver.setSuffix(".jsp");
  return resolver;
}

...
@RequestMapping("/home")
public String welcome() {
  return "index";
}

My Project

But when I run using gradle jettyRun I get...

http://localhost:8080/personal-war/home
HTTP ERROR 404

Problem accessing /personal-war/WEB-INF/jsp/index.jsp. Reason:
  NOT_FOUND

Update Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
  <display-name>Spring MVC Application</display-name>
  <servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
      </init-param>
      <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.proj.spring.config</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
      <servlet-name>mvc-dispatcher</servlet-name>
      <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

I added this line

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}

but then only html renders the server side stuff isn't working

Foi útil?

Solução

First, you need to know that Servlet containers (I'll assume Jetty too) have a Servlet for rendering JSPs. This Servlet is typically extension mapped to *.jsp.

The Servlet Specification gives the order of url-pattern matching

  1. The container will try to find an exact match of the path of the request to the path of the servlet. A successful match selects the servlet.
  2. The container will recursively try to match the longest path-prefix. This is done by stepping down the path tree a directory at a time, using the ’/’ character as a path separator. The longest match determines the servlet selected.
  3. If the last segment in the URL path contains an extension (e.g. .jsp), the servlet container will try to match a servlet that handles requests for the extension. An extension is defined as the part of the last segment after the last ’.’ character.
  4. If neither of the previous three rules result in a servlet match, the container will > attempt to serve content appropriate for the resource requested. If a "default" servlet is defined for the application, it will be used. Many containers provide an implicit default servlet for serving content.

In your case, when you forward to

/WEB-INF/jsp/index.jsp

The Servlet container will match that path to your DispatcherServlet mapped to

/*

This happens before the container could match the JSP servlet mapped to *.jsp.

It therefore uses your DispatcherServlet to service(..) the request. But your DispatcherServlet does not have a handler for

/WEB-INF/jsp/index.jsp

The simple solution would be to map your DispatcherServlet to

/

and have it be the fallback Servlet if no match is found.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top