The use of jump address in thmeleaf

Recently, when using thmeleaf, I encountered a problem, such as the address to jump to is localhost:8080/pro/a/b , jump and find out

localhost:8080/pro/path/a/b , so there is more path in the address, which is obviously wrong. In fact, it's different to add / or not add / before the jump address.

Before the page jump address, add /, which means to jump to the root directory of the project, without /, which means to jump from the current page address.

The solution is very simple, just get the context path. Due to the use of thymeleaf, the following jsp is used.

Here are the ways to jump.

1.jsp

jsp get context path: the following is just a way for jsp to get context path

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

 <c:set var="basePath"

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

Use example:<html><a href="${basePath}/jsp/index.jsp"></a></html>

 

2.thymeleaf

2.1.ajax

Use ajax submission in thymeleaf, URL: [@ {/ index / ajaxtest}]] so that you can.

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

2.2.form form submission

<form class="form-group" th:action="@{/login}" method="post">
      <div class="form-group">
           <label>user name</label>
           <input class="form-control" name="userName" type="text" placeholder="">
      </div>
      <div class="form-group">
           <label>password</label>
           <input class="form-control" name="password" type="password" placeholder="">
      </div>
      <div class="text-right">
           <button class="btn btn-primary" type="submit">Sign in</button>
           <button class="btn btn-danger" data-dismiss="modal">cancel</button>
      </div>
 </form>

2.3.a label

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

In this way, using thmeleaf will automatically get the context parameters.

Tags: Javascript JSP Thymeleaf Java

Posted on Tue, 26 May 2020 10:13:04 -0400 by retoknaak