Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC - Map controller to context root (/) while using mvc:resources

Morning,

Having issues mapping a controller to / (i.e. localhost:8080/someApp/ would map to @Controller("/")) while also using mvc:resources

web.xml mapping:

  <servlet-mapping>
    <servlet-name>springServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

mvc:resources

<mvc:resources mapping="/resources/**" location="/resources/" />

The server loads the page correctly, but when I map to an asset i.e.

<link type="text/css" rel="stylesheet" href="<c:url value="/resources/css/blueprint/print.css"/>" />

When clicking the css file via view-source in a web browser, the server response maps back to the index page, rather than the resource. Leads me to believe it's related to the servlet-mapping.

Any help with this would be great!

Thanks!

Edit: Forgot to mention, if I bind the controller to say:

@Controller("/pages")

Everything works fine, just would rather have the context root be able to respond correctly.

like image 850
dardo Avatar asked Mar 01 '12 16:03

dardo


1 Answers

@Controller("/)

and

@RequestMapping("/")

are not the same thing.

Since @RequestMapping may be placed at a class level, placing the mapping on the class will have the desired affect.

Example:

@Controller
@RequestMapping("/")
public class RootController
{

  @RequestMapping(method=RequestMethod.GET)
  public String index()
  {
    return "index";
  }

}

This will work correctly, and also works with the mvc:resources bean.

like image 183
dardo Avatar answered Sep 24 '22 12:09

dardo