<a href="" </a>与<a th:href="" </a>有什么区别
时间: 2023-10-31 10:06:51 浏览: 36
`<a href=""></a>`是HTML标签中用于表示超链接的标签,其中href属性用于指定链接目标的URL地址。
而`<a th:href=""></a>`则是在使用Thymeleaf模板引擎时,用于在HTML标签中嵌入Thymeleaf表达式的语法。在这种情况下,th:href属性用于指定链接目标的URL地址,但是该属性中可以包含Thymeleaf表达式,以便根据需要在运行时动态生成链接目标URL。
因此,两者的区别在于`<a th:href=""></a>`具有动态生成链接目标URL的能力,而`<a href=""></a>`只能静态地指定链接目标的URL地址。
相关问题
给下面代码增加分页功能,要求细致,逻辑清楚,<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} 分别表示起始记录数和每页显示的记录数。这样就可以实现分页功能了。
<h1 align="center">欢迎进入图书网站首页</h1> <div sec:authorize="isAnonymous()"> <h2 align="center">游客你好!<a th:href="@{/login}">请登录</a></h2> <h2 align="center">如果还未注册,<a th:href="@{/user/regix}">请注册</a></h2> </div> <div sec:authorize="isAuthenticated()"> <h2 align="center"> <span sec:authentication="name" style="color:#007bff"></span> 你好,你的用户权限为<span sec:authentication="principal.authorities"></span> 你有权进行下面的操作 </h2> <h3><a th:href="@{/logout}">退出登录</a></h3> </div> <div sec:authorize="hasRole('admin')"> <h3>作为管理员,你可以</h3> <a th:href="@{/oss/books}">查看所有图书</a> <a th:href="@{/order/showorder}">查看所有订单</a> </div> <div sec:authorize="hasRole('user')"> <h3>作为普通用户,你可以</h3> <a th:href="@{/user/book/findAllBooks}">查看所有图书</a> <a th:href="@{/order/userorder/{uid}(uid=${session.user})}">查看自己的订单</a> </div>修改这段代码美化页面
好的,以下是修改后的代码,美化了页面的样式:
```
<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
<meta charset="UTF-8">
<title>图书网站首页</title>
<style>
body {
font-family: Arial, sans-serif;
font-size: 16px;
line-height: 1.5;
margin: 0;
padding: 0;
background-color: #f0f0f0;
}
header {
background-color: #007bff;
color: #fff;
padding: 20px;
text-align: center;
}
h1 {
margin: 0;
font-size: 30px;
}
h2 {
margin: 0;
font-size: 24px;
}
h3 {
font-size: 20px;
margin-top: 30px;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.logout {
margin-top: 20px;
}
.btn {
display: inline-block;
margin-right: 10px;
padding: 10px 20px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
text-decoration: none;
}
.btn:hover {
background-color: #0069d9;
}
.btn-primary {
background-color: #007bff;
color: #fff;
}
.btn-danger {
background-color: #dc3545;
color: #fff;
}
.btn-success {
background-color: #28a745;
color: #fff;
}
.btn-warning {
background-color: #ffc107;
color: #212529;
}
</style>
</head>
<body>
<header>
<h1>欢迎进入图书网站首页</h1>
</header>
<div class="container">
<div sec:authorize="isAnonymous()">
<h2>游客你好!<a class="btn btn-primary" th:href="@{/login}">请登录</a></h2>
<h2>如果还未注册,<a class="btn btn-warning" th:href="@{/user/regix}">请注册</a></h2>
</div>
<div sec:authorize="isAuthenticated()">
<h2>
<span sec:authentication="name"></span>
你好,你的用户权限为<span sec:authentication="principal.authorities"></span>
你有权进行下面的操作
</h2>
<div class="logout">
<a class="btn btn-danger" th:href="@{/logout}">退出登录</a>
</div>
<div sec:authorize="hasRole('admin')">
<h3>作为管理员,你可以</h3>
<a class="btn btn-primary" th:href="@{/oss/books}">查看所有图书</a>
<a class="btn btn-success" th:href="@{/order/showorder}">查看所有订单</a>
</div>
<div sec:authorize="hasRole('user')">
<h3>作为普通用户,你可以</h3>
<a class="btn btn-primary" th:href="@{/user/book/findAllBooks}">查看所有图书</a>
<a class="btn btn-success"
th:href="@{/order/userorder/{uid}(uid=${session.user})}">查看自己的订单</a>
</div>
</div>
</div>
</body>
</html>
```
我添加了一些 CSS 样式,使得页面更加美观和易于阅读。我还修改了一些 HTML 标签,使用了按钮样式代替了链接。同时,我还将登出按钮放在了一个独立的 div 中,并添加了一些间距,以便更好地区分不同的区块。