<a th:href="@{/page2.html}">跳转到页面2</a> 这个跳转后报错404
时间: 2024-04-02 13:35:00 浏览: 23
如果使用Thymeleaf的`th:href`属性进行页面跳转出现404错误,可能是因为Thymeleaf没有正确解析URL路径导致的。要解决这个问题,可以尝试以下几个步骤:
1. 确认目标页面的路径是否正确。在使用`th:href`属性时,需要将目标页面的路径写成相对于当前请求路径的相对路径。例如,如果当前请求的URL是`http://localhost:8080/home.html`,而目标页面的路径是`/page2.html`,则应该将`th:href`设置为`@{/page2.html}`,这样Thymeleaf会自动将相对路径解析成绝对路径。
2. 确认目标页面是否存在。如果目标页面不存在,或者路径写错了,就会出现404错误。可以尝试手动在浏览器地址栏中输入目标页面的路径,看看是否能正确访问。
3. 确认Thymeleaf的配置是否正确。在使用Thymeleaf时,需要在项目中正确配置Thymeleaf的模板解析引擎。可以检查一下项目中的配置文件,看看是否有漏掉的配置项或者配置错误的地方。
如果以上步骤都没有解决问题,可以考虑使用其他方式进行页面跳转,例如使用重定向或者JavaScript进行跳转。
相关问题
给下面代码增加分页功能,要求细致,逻辑清楚,<form action="./../selectNews" method="post"> <input type="text" name="key" id="key" placeholder="请输入标题"/> <input type="submit" value="查询"/> <a href="zhuce.jsp">发布新闻</a> <a href="../user01/index.jsp">返回主页</a> </form> <table> <thead> <tr> <th>新闻编号</th> <th>新闻标题</th> <%-- <th>新闻内容</th>--%> <th>新闻类别</th> <th>发布人</th> <th>发布时间</th> <th>状态</th> <th>是否头条</th> <th colspan="4" align="center">操作</th> </tr> </thead> <tbody> <c:forEach items="${newslist}" var="u"> <tr> <td>${u.nid}</td> <td>${u.title}</td> <%-- <td>${u.content}</td>--%> <td>${u.type}</td> <td>${u.userName}</td> <td>${u.pubTime}</td> <td> <c:choose> <c:when test="${u.state==0}"><span style="color: ghostwhite">未审核</span></c:when> <c:when test="${u.state==1}"><span style="color: green">审核通过</span></c:when> <c:otherwise><span style="color: red">未通过</span></c:otherwise> </c:choose> </td> <td> <c:choose> <c:when test="${u.top==0}">否</c:when> <c:otherwise>是</c:otherwise> </c:choose> </td> <c:choose> <c:when test="${user.rule==1}"> <td> <a href="../NewsDetailServlet?nid=${u.nid}&op=show">查看</a> </td> <td> <a href="../NewsDetailServlet?nid=${u.nid}&op=update">修改</a> </td> <td> <a href="NewsDelete.jsp?nid=${u.nid}&title=${u.title}&userName=${user.userName}">删除</a> </td> <td> <a href="Newsshenhe.jsp?nid=${u.nid}&title=${u.title}&userName=${user.userName}">审核</a> </td> </c:when> <c:when test="${user.rule==0 && user.uid==u.uid}"> <td><a href="../NewsDetailServlet?nid=${u.nid}&op=show">查看</a></td> <td><a href="../NewsDetailServlet?nid=${u.nid}&op=update">修改</a></td> <td><a href="NewsDelete.jsp?nid=${u.nid}&title=${u.title}&userName=${user.userName}">删除</a></td> </c:when> <c:otherwise> <td><a href="../NewsDetailServlet?nid=${u.nid}&op=show">查看</a></td> </c:otherwise> </c:choose> </tr> </c:forEach> </table>
首先,我们需要在表单中添加一个分页控件,例如:
<form action="./../selectNews" method="post">
<input type="text" name="key" id="key" placeholder="请输入标题"/>
<input type="submit" value="查询"/>
<a href="zhuce.jsp">发布新闻</a>
<a href="../user01/index.jsp">返回主页</a>
<!-- 分页控件 -->
<div class="pagination">
<c:if test="${currentPage > 1}">
<a href="./../selectNews?page=${currentPage - 1}&key=${param.key}">上一页</a>
</c:if>
<c:forEach begin="${minPage}" end="${maxPage}" var="i">
<c:choose>
<c:when test="${i == currentPage}">
<span class="current">${i}</span>
</c:when>
<c:otherwise>
<a href="./../selectNews?page=${i}&key=${param.key}">${i}</a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:if test="${currentPage < totalPages}">
<a href="./../selectNews?page=${currentPage + 1}&key=${param.key}">下一页</a>
</c:if>
</div>
</form>
我们使用了一个名为 "pagination" 的 div 元素来包含分页控件。其中,currentPage 表示当前页数,totalPages 表示总页数,minPage 和 maxPage 分别表示当前页数左边和右边的页数范围。
在 Java Servlet 中,我们需要对请求参数进行解析,计算出分页所需的各个参数,并将它们传递给 JSP 页面。例如:
int page = Integer.parseInt(request.getParameter("page"));
int pageSize = 10; // 每页显示的记录数
int start = (page - 1) * pageSize; // 计算起始记录数
String key = request.getParameter("key");
List<News> newslist = NewsDAO.selectNews(start, pageSize, key);
int totalCount = NewsDAO.getNewsCount(key);
int totalPages = (int) Math.ceil(totalCount * 1.0 / pageSize);
int minPage = Math.max(1, page - 5);
int maxPage = Math.min(totalPages, page + 5);
request.setAttribute("newslist", newslist);
request.setAttribute("currentPage", page);
request.setAttribute("totalPages", totalPages);
request.setAttribute("minPage", minPage);
request.setAttribute("maxPage", maxPage);
request.getRequestDispatcher("newslist.jsp").forward(request, response);
在 JSP 页面中,我们需要根据传递过来的参数来生成分页控件。我们可以使用 JSTL 标签库中的 forEach 标签来循环生成分页链接。例如:
<c:if test="${currentPage > 1}">
<a href="./../selectNews?page=${currentPage - 1}&key=${param.key}">上一页</a>
</c:if>
<c:forEach begin="${minPage}" end="${maxPage}" var="i">
<c:choose>
<c:when test="${i == currentPage}">
<span class="current">${i}</span>
</c:when>
<c:otherwise>
<a href="./../selectNews?page=${i}&key=${param.key}">${i}</a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:if test="${currentPage < totalPages}">
<a href="./../selectNews?page=${currentPage + 1}&key=${param.key}">下一页</a>
</c:if>
注意,我们在分页链接中传递了当前页数和查询关键字两个参数,这样在用户点击分页链接时就可以保持查询关键字不变,同时跳转到相应的页数。
最后,我们需要在查询语句中添加 LIMIT 子句来限制返回的记录数。例如:
SELECT * FROM news WHERE title LIKE '%${key}%' LIMIT ${start}, ${pageSize};
其中,${start} 和 ${pageSize} 分别表示起始记录数和每页显示的记录数。这样就可以实现分页功能了。
创建一个success.html 页面,在首页通过 thymeleaf 超链接 <a th:href/>和浏览器超链接<a/>2种不同方式分别实现跳转到success.html页面。
好的,下面是实现步骤:
1. 在Web应用程序的“src/main/resources/templates”目录下创建一个名为“success.html”的Thymeleaf模板文件。在该文件中添加以下内容:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Success Page</title>
</head>
<body>
<h2>Success!</h2>
</body>
</html>
```
2. 在index.html文件中,添加两个超链接,一个使用Thymeleaf,一个使用常规的HTML链接。如下所示:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Home Page</title>
</head>
<body>
<h2>Welcome to the Home Page</h2>
<a th:href="@{/success}">Go to Success Page (Thymeleaf)</a>
<br />
<a href="/success">Go to Success Page (HTML)</a>
</body>
</html>
```
3. 在Web应用程序的“src/main/resources/application.properties”文件中添加以下内容:
```
spring.mvc.view.prefix=classpath:/templates/
spring.mvc.view.suffix=.html
```
4. 在Web应用程序的“src/main/java”目录下创建一个名为“com.example.demo”的Java包。
5. 在“com.example.demo”包中创建一个名为“DemoApplication.java”的类,并添加以下内容:
```java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
6. 在Web应用程序的“src/main/java”目录下创建一个名为“com.example.demo.controller”的Java包。
7. 在“com.example.demo.controller”包中创建一个名为“HomeController.java”的控制器类,并添加以下内容:
```java
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("/")
public String home() {
return "index";
}
@GetMapping("/success")
public String success() {
return "success";
}
}
```
现在你可以启动应用程序,并在浏览器中输入http://localhost:8080/来访问首页。点击超链接“Go to Success Page (Thymeleaf)”或“Go to Success Page (HTML)”将跳转到“success.html”页面。
希望这些步骤可以帮助到你实现要求的功能。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
@RequestMapping("/completed") public String Completed(Model model,HttpSession session) { User user = (User) session.getAttribute("user"); int userid=user.getUserId(); List<OrderForm> formList = orderService.FindAllbyidandstatus(userid,4); model.addAttribute(formList); return "order4"; }我这样写传了List到A页面,那么我在A页面中,写标签进行页面跳转,并且要用到List中的数据作为参数也在中传递到下一个控制器,我该怎么写
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)