Spring MVC导航路径

Pau*_*nis 0 java spring spring-mvc

尝试在spring mvc中进行一些简单的导航时遇到问题.我有一个导航控制器:

@Controller
@RequestMapping("/secure")
public class NavigationController {

    @RequestMapping("/operation")
    public String processOperationPage() {
        //Some logic goes here
        return "corpus/operation";
    }

    @RequestMapping("/configuration")
    public String processConfigurationPage() {
        //Some logic goes here
        return "corpus/configuration";
    }

}
Run Code Online (Sandbox Code Playgroud)

并且有我的链接到达该控制器:

<a href="secure/operation.htm">Operation</a>
<a href="secure/configuration.htm">Configuration</a>
Run Code Online (Sandbox Code Playgroud)

第一次点击链接时,一切正常.在浏览器中,我看到了正常的路径,正如我所料.例如:http://localhost/obia/secure/configuration.htm.但是如果我在这个页面,并且从这个页面我想要在单击操作链接时到达operation.htm,路径就像这样:http://localhost/obia/secure/secure/operation.htm.

secure出现两次.我怎么解决这个问题?

小智 5

你的链接是相对的.在它们前面添加斜杠将修复它.


Pau*_*Wee 5

如果您使用的是JSP,请改用JSTL:

<c:url value="/secure/operation.htm" />
Run Code Online (Sandbox Code Playgroud)

请记住在JSP文件中包含taglib:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
Run Code Online (Sandbox Code Playgroud)

通过使用JSTL,您可以避免在应用程序部署到不同的上下文(例如http://host/和)后更改URLhttp://host/myapp

第一个将生成http://host/secure/operation.htm,第二个将为http://host/myapp/secure/operation.htm您生成.