SpringMVC3.1:url映射问题
我是SpringMVC框架的新手,在使用两个不同的控制器进行基本URL映射时遇到了一些问题。我正在使用@Controller和@RequestMapping 以下结果导致/people和/accounts都出现404错误 以下是我的Spring MVC 3.1设置: index.jspSpringMVC3.1:url映射问题,spring,spring-mvc,url-mapping,Spring,Spring Mvc,Url Mapping,我是SpringMVC框架的新手,在使用两个不同的控制器进行基本URL映射时遇到了一些问题。我正在使用@Controller和@RequestMapping 以下结果导致/people和/accounts都出现404错误 以下是我的Spring MVC 3.1设置: index.jsp <!DOCTYPE html> <html> <head> <title>t-diggity</title> </head> <
<!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>
t-二指性
整合
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>
t-二指性
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
上下文属性
org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring
springSecurityFilterChain
/*
index.jsp
春天
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
类路径:applicationContext.xml
1.
春天
/人/*
春天
/资产/*
春天
/帐目/*
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
@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/";
}
}
@控制器
@请求映射(“/people/”)
公共类个人控制器{
@自动连线
私人私人服务;
@请求映射(“/”)
公共字符串listPeople(地图)
{
map.put(“person”,newperson());
map.put(“peopleList”,personService.listPeople());
回归“人”;
}
@RequestMapping(value=“/addPerson”,method=RequestMethod.POST)
公共字符串addPerson(@modeldattribute(“person”)person,BindingResult
{
personService.addPerson(人);
返回“重定向:/people/”;
}
@请求映射(“/delete/{personId}”)
公共字符串deletePerson(@PathVariable(“personId”)字符串personId)
{
personService.removePerson(personId);
返回“重定向:/people/”;
}
}
controller#2:AccountController.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
@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/";
}
}
@控制器
@请求映射(“/accounts”)
公共类帐户控制器{
@自动连线
私人帐户服务;
@请求映射(“/”)
公共字符串列表帐户(映射)
{
map.put(“帐户”,新帐户());
map.put(“accountList”,accountService.listcounts());
返回“账户”;
}
@RequestMapping(value=“/addAccount”,method=RequestMethod.POST)
公共字符串addPerson(@modeldattribute(“account”)account,
BindingResult(结果)
{
accountService.addAccount(account);
返回“重定向:/accounts/”;
}
@请求映射(“/delete/{accountId}”)
公共字符串deleteCount(@PathVariable(“accountId”)字符串accountId){
accountService.removeAccount(accountId);
返回“重定向:/accounts/”;
}
}
我希望我的错误是一个小错误,因为我不熟悉URL映射,所以它可能很小…我希望得到任何指导,提前感谢。将
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>
如果web应用程序中的所有内容都由SpringMVC控制,则通常不需要单独映射控制器
并将您的@控制器映射如下:
@Controller
@RequestMapping("/people")
public class PersonController {
@RequestMapping("/")
public String listPeople(Map<String, Object> map) {
...
return "people";
}
}
@控制器
@请求映射(“/people”)
公共类个人控制器{
@请求映射(“/”)
公共字符串listPeople(地图){
...
回归“人”;
}
}
我在这里所做的就是丢失/people
的@RequestMapping
上的尾随/