Spring MVC中的请求映射 - 返回上一个URL

时间:2013-01-03 18:06:42

标签: spring redirect model-view-controller request-mapping

我在Spring MVC中遇到重定向问题。我有一个控制器,只有很少的方法:

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

当我在我的程序中使用请求映射中带有“print”的示例方法时,我的URL是:

http://localhost:8080/WWP/print

但是当我在请求映射中使用我的方法“edit / {worker}”之后,我使用了“print”方法,我得到了一个URL:

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

因为在“编辑”之后添加了“打印”,这是不必要的。如何返回上一个网址:

http://localhost:8080/WWP/print

我想我必须更改RequestMapping注释。但我不知道该怎么做。

编辑: 好的,这是我的web.xml文件:

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

1 个答案:

答案 0 :(得分:0)

我在JSP中使用了“c:url”,但它确实有效。

例如:

会记住以前的网址(这是错误的):

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

会很好:

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

网址始终链接到:_http:// localhost:8080 / WWP / events / my