1

I have a web app. My controller looks like this:

@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";
   }
}

And I have a form like:

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

My application is accessed via link: http://localhost:8080/Client
But when I submit form it submits to http://localhost:8080/add when it should be http://localhost:8080/Client/add
I figured, that i could use ${pageContext.request.contextPath} before every link, but is there any other way?

MariuszS
  • 30,646
  • 12
  • 114
  • 155
qiGuar
  • 1,726
  • 1
  • 18
  • 34

2 Answers2

3

Use c:url to automatically prepend the context root to your URL.

<c:url var="formUrl" value="/add" />
<form:form method="POST" action="${formUrl}" commandName="user">
Beau Grantham
  • 3,435
  • 5
  • 33
  • 43
  • Yeah, it works, but I will have to do this for every link i have. Is there another way? Also added `web.xml` if it helps. – qiGuar Nov 21 '13 at 20:05
2

Try this:

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

Read more:

Related questions:

Community
  • 1
  • 1
MariuszS
  • 30,646
  • 12
  • 114
  • 155
  • Also works. It seems that in spring 3.2.3(I'm using) this option was removed, instead they made `servletRelativeAction` tag, but when i set it to true still getting error, but with `${requestContext.pathToServlet}` works fine. Am I doing something wrong? – qiGuar Nov 21 '13 at 20:55
  • Maybe `servletRelativeAction` is for [portlets only](https://github.com/spring-projects/spring-framework/commit/399f887128f2dc0dcaadb7b7df2826ce0b9dcf79)? I think `${requestContext.pathToServlet}` is recomended way to do this. – MariuszS Nov 21 '13 at 20:57
  • I see, but how does requestContext work if it was rolled back? Or am I missing something? – qiGuar Nov 21 '13 at 20:59
  • 1
    `requestContext` was not rolled back. Rolled back was automate prepending `contextPath` to action url. This was added in 3.2.2 and reverted in 3.2.3 :) – MariuszS Nov 21 '13 at 21:00
  • Now i get it. Thank you :) – qiGuar Nov 21 '13 at 21:02