2

Could you help me, please, with one problem. I have a project JSP + Spring MVC. It shows table with some data. So i show some data on clients.jsp page, all my buttons have links like this: clients/add, clients/edit, etc. so they call servlets with these url's.

And i also added sort option with button clients/sort

When i push this button, my url becomes http://localhost:8080/Project/clients/sort and i see sorted data, but if i push any other buttons(add, edit) i will get a error, because Dispatcher is trying to find servlet with url clients/sort/add, not clients/add.

So i dont know how to manage with this problem, how to write links for buttons, that won't depend on page url?

Some code from my project:

Buttons:

            <div align="center">
                <a class="sort_firstName"
                   href="<c:url value="/clients/sort/firstnameup"/>">
                    <spring:message code="label.up"/>
                </a>
            </div> 

            <!-- ADD ORDER BUTTON -->
            <a class="add_order"
               href="<c:url value="/clients/addOrder/${client.id}"/>">
                <spring:message code="label.addOrder"/>
            </a> /

            <!-- EDIT CLIENT BUTTON -->
            <a href="<c:url value="/clients/edit/${client.id}"/>">
                <spring:message code="label.modify"/></a>

Controller:

@RequestMapping(value = "/add", method = RequestMethod.GET)
public String newClient(Model model) {
    Clients client = new Clients();
    client.setId(0);
    model.addAttribute("clientAdd", client);
    return "clientForm";
}

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addClient(@ModelAttribute("clientAdd") Clients client,
                        BindingResult result, Model model) {
    if (result.hasErrors()) {
        return "clientForm";
    }
    clientsService.createClient(new CreateClientEvent(client));
    return "redirect:/clients";
}

upd:

<form id="dialog-form" class="form-horizontal" action="clients/add" method="post">
    <table class="table table-condensed table-striped">
        ....
    </table>
    <div class="col-sm-offset-2 col-sm-10">
        <a class="pull-right">
            <button class="btn btn-primary" type="submit" id="addClient" ><c:out value="Create"/></button>
        </a>
    </div>
</form>
Cooler
  • 309
  • 3
  • 6
  • 17

2 Answers2

1

You should use c:url otherwise your link will be relative to current url.

<c:url value="/clients/add" var="addUrl"/> 
<form id="dialog-form" class="form-horizontal" action="${addUrl}" method="post">
alain.janinm
  • 19,951
  • 10
  • 65
  • 112
-1

First of all your name for the controller should be different. It should have only one controller named /add . IN the href you dont need C: taglib. you can use it in the following manner- href="add/${client.id}" and you need to catch the client ID in the controller with @RequestParam("Id") long Id. You controller cannot be matched because you have same name in two controllers and you have to only specify the controller name and not the full path like you have done.

Abhinav Pandey
  • 270
  • 8
  • 21
  • -1 because you CAN have 2 requestMapping with same value but different method. And also using `href="add/${client.id}` is exactly why the poster has pb. If he is on the `clients/sort` page then using your href will send user to `clients/sort/add` instead of `clients/add` – alain.janinm Oct 08 '14 at 11:06