Spring错误的链接位置

时间:2013-11-21 19:50:21

标签: java spring

我有一个网络应用程序。我的控制器看起来像这样:

@Controller
@RequestMapping(value = "/")
public class MainController {    

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

   @RequestMapping(value = "/add", method = RequestMethod.POST)
   public String add(ModelMap model, User user) {
      userService.create(user);
      model.addAttribute("message", "User successfully created.");
      return "index";
   }
}

我的表格如下:

<form:form method="POST" action="/add" commandName="user">

的web.xml

<servlet>
    <servlet-name>client</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

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

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/client-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

我的应用程序可通过链接访问:http://localhost:8080/Client
但是当我提交表单时,它应该http://localhost:8080/add提交http://localhost:8080/Client/add 我想,我可以在每个链接之前使用${pageContext.request.contextPath},但还有其他方法吗?

2 个答案:

答案 0 :(得分:3)

使用c:url自动将上下文根添加到您的网址。

<c:url var="formUrl" value="/add" />
<form:form method="POST" action="${formUrl}" commandName="user">

答案 1 :(得分:2)

试试这个:

<form:form method="POST" action="${requestContext.pathToServlet}/add" commandName="user">

了解更多:

相关问题: