thymeleaf中跳转地址的使用

最近在使用thymeleaf时,遇到一个问题,好比要跳转的地址是localhost:8080/pro/a/b,跳转后发现变成了html

localhost:8080/pro/path/a/b,这样地址中多了个path,显然是错误的。其实在跳转地址前加 / 和不加 / 是不同的。java

页面跳转地址前加 /,意味着跳转到项目的根目录,不加 / ,意味着从当前页地址跳转。ajax

解决的办法也很简单,只要获取上下文路径就能够了。因为使用的thymeleaf下面jsp一笔带过。jsp

下面是各类跳转的方式。async

1.jsp

jsp获取上下文路径:下面只是jsp获取上下文路径的一种方式post

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>url

 <c:set var="basePath"spa

 value="${pageContext.request.scheme}://${pageContext.request.serverName}: ${pageContext.request.serverPort}${pageContext.request.contextPath}" />code

使用示例:<html><a href="${basePath}/jsp/index.jsp"></a></html>orm

 

2.thymeleaf

2.1.ajax

在thymeleaf中使用ajax提交,url:[[@{/index/ajaxtest}]]这样就能够。

$.ajax({
        async:true,
        url:"[[@{/findById/}]]"+id,
        type:"POST",
        success:function(data){
           alert("操做成功!");
        }
 })

2.2.form表单提交

<form class="form-group" th:action="@{/login}" method="post">
      <div class="form-group">
           <label>用户名</label>
           <input class="form-control" name="userName" type="text" placeholder="">
      </div>
      <div class="form-group">
           <label>密码</label>
           <input class="form-control" name="password" type="password" placeholder="">
      </div>
      <div class="text-right">
           <button class="btn btn-primary" type="submit">登陆</button>
           <button class="btn btn-danger" data-dismiss="modal">取消</button>
      </div>
 </form>

2.3.a标签

<a th:href="@{'searchInfo/'+${info.id}}">查看</a>

这样使用thymeleaf会自动获取上下文参数。

相关文章
相关标签/搜索