Search code examples
springspring-mvcurl-mapping

Spring MVC 3.1: Trouble with url mapping


I'm new to Spring MVC framework, having some trouble with basic URL mapping using two different controllers. I'm using @Controller and @RequestMapping.

The following results in 404 error for both /people and /accounts.

Here is my Spring MVC 3.1 setup:

index.jsp

<!DOCTYPE html>
<html>
<head>
    <title>t-diggity</title>
</head>
<body>
    <h1>integration</h1>
    <a href="/people/">Contact List</a><br>
    <a href="/accounts/">Account List</a><br>
</body>
</html>

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_2_5.xsd"
     version="2.5">

<display-name>t-diggity</display-name>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
            <param-name>contextAttribute</param-name>
            <param-value>org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

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

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

</web-app>

controller # 1: PersonController.java

@Controller
@RequestMapping("/people/")
public class PersonController {

    @Autowired
    private PersonService personService;

    @RequestMapping("/")
    public String listPeople(Map<String, Object> map) 
    {
        map.put("person", new Person());
        map.put("peopleList", personService.listPeople());
        return "people";
    }

    @RequestMapping(value = "/addPerson", method = RequestMethod.POST)
    public String addPerson(@ModelAttribute("person") Person person, BindingResult result
    {
        personService.addPerson(person);
        return "redirect:/people/";
    }

    @RequestMapping("/delete/{personId}")
    public String deletePerson(@PathVariable("personId") String personId) 
    {
        personService.removePerson(personId);
        return "redirect:/people/";
    }
}

controller #2: AccountController.java

@Controller
@RequestMapping("/accounts")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @RequestMapping("/")
    public String listAccounts(Map<String, Object> map) 
    {
        map.put("account", new Account());
        map.put("accountList", accountService.listAccounts());
        return "accounts";
    }

    @RequestMapping(value = "/addAccount", method = RequestMethod.POST)
    public String addPerson(@ModelAttribute("account") Account account, 
                            BindingResult result) 
    {
        accountService.addAccount(account);
        return "redirect:/accounts/";
    }

    @RequestMapping("/delete/{accountId}")
    public String deleteAccount(@PathVariable("accountId") String accountId) {
        accountService.removeAccount(accountId);
        return "redirect:/accounts/";
    }

}

I am hoping my error is a minor one, I'm new to URL mapping so it likely is minor... I'd appreciate any coaching, thanks in advance.


Solution

  • Change index.jsp to:

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    
    <!DOCTYPE html>
    <html>
    <head>
        <title>t-diggity</title>
    </head>
    <body>
        <h1>integration</h1>
        <a href="<c:url value="/people/"/>">Contact List</a><br>
        <a href="<c:url value="/accounts/"/>">Account List</a><br>
    </body>
    </html>
    

    The anchor URLs were not including the root context. The c:url tag will handle this for you.

    Change the web.xml mapping to:

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

    There is usually no need to map your controllers separately if everything in your web app is controlled by Spring MVC.

    And map your @Controllers like this:

    @Controller
    @RequestMapping("/people")
    public class PersonController {
    
        @RequestMapping("/")
        public String listPeople(Map<String, Object> map) {
            ...
            return "people";
        }
    
    }
    

    All I've done here is to lose the trailing / on the @RequestMapping for /people.