我是 Spring MVC 框架的新手,在使用两个不同的控制器进行基本 URL 映射时遇到了一些问题。我正在使用@Controller 和@RequestMapping。
以下导致 /people 和 /accounts 的 404 错误。
这是我的 Spring MVC 3.1 设置:
索引.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 映射的新手,所以它可能是小错误......我会很感激任何指导,在此先感谢。