2012-09-01 56 views
-1

我是Spring MVC框架的新手,在使用兩個不同控制器的基本URL映射時遇到了一些麻煩。我正在使用@Controller和@RequestMapping。Spring MVC 3.1:URL映射問題

以下結果爲/ people和/ accounts的404錯誤。

這裏是我的Spring MVC 3.1設置:

的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> 

控制器#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/"; 
    } 
} 

控制器#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/"; 
    } 

} 

我希望我的錯誤是較小的,我是新來的URL映射所以它可能是次要的......我會感謝任何教練,提前感謝。

回答

1

更改index.jsp到:

<%@ 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> 

錨定的URL不包括根上下文。 c:url標籤將爲您處理。

更改web.xml映射:

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

通常沒有必要,如果在你的web應用程序的一切由Spring MVC的控制你的控制器單獨映射。

和地圖你@Controllers這樣的:

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

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

} 

所有我在這裏所做的是失去對@RequestMapping//people

+0

謝謝。簡單的修復。你讓我今天一整天都感覺很好! – user1639888