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?