0

I have a problem with redirect in Spring MVC. I have a controller with few methods:

    //PRINT ALL WORKERS
@RequestMapping("/print")
public String listWorkers(Model model)
{
    model.addAttribute("workerList", workerService.getAllWorkers());
    return "print";
}


//EDIT WORKER DATA
@RequestMapping("/edit")
public String redirectWorker(HttpServletRequest request)
{
    String parameter = request.getParameter("workers");
    String path = "redirect:/edit/" + parameter;
    return path;
}


@RequestMapping("/edit/{worker}")
public String editWorker(@PathVariable("worker")
String login, Model model)
{
    model.addAttribute("worker", workerService.getWorker(login));
    return "edition";
}

When I'm using in my program for example method with "print" in request mapping, my URL is:

http://localhost:8080/WWP/print

But when I'm using my method with "edit/{worker}" in request mapping and after that I used method with "print" I got an URL:

http://localhost:8080/WWP/edit/print

Cause my "print" was added after "edit" which isn't necessary. How to return to previous URL:

http://localhost:8080/WWP/print

I suppose that I have to change my RequestMapping annotation. But I don't know how to do this.

EDIT: Ok, here is my web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml, /WEB-INF/spring/appServlet/security.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <!-- Filtry -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> 


</web-app>
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
caro
  • 381
  • 3
  • 5
  • 20

1 Answers1

0

I used the "c:url" in the JSP and it worked.

For example:

It will be remember the previous URL (It's wrong):

<a href="/events/my">My Events</a>

It will be good:

<c:url var="myEventsUrl" value="/events/my" />
<a href="${myEventsUrl}">My Events</a>

URL is always link to: _http://localhost:8080/WWP/events/my

Atesz
  • 1