现在终于把 redbook 看完了

作者在看完《Redbook》后对于未来职业道路感到迷茫,不确定是否应该寻找与OpenGL相关的工作,还是选择其他方向。表达了对深入底层技术和实际操作的渴望。

终于把《redbook 》看完了,觉得收获很大,也觉得没有收获!

这让我觉得更加迷茫!

到底自己该选择怎样的道路呢?

毕业了就找工作,找到工作就走了呢?

还是找一个与opengl相关的工作呢?

现在想找一个opengl的工作,多么的难啊!

我或许还不想习惯被别人管的生活吧,还不想离开学校!

明天 东软的 公司来招聘。这到底意味着什么呢?

是意味着机会呢?还是意味着诱惑呢?

我又迷茫了!

没有人给我指点,我总是自己在逃避!

其实学了这么多,总是感觉学的不够深入,总是觉得应该更加深入到底层,那才行!

可是软件哪里有尽头啊!哪里可以让你一个不停的,盲目的学习啊!

我从今天起就要开始找实习了,只要能动手的,我都做!

我不能停止于看书的阶段,看一辈子书,也不能写出一个好的程序!

我不能再封闭自己了!

我要面对新的生活!

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="com.example.model.Book" %> <%@ page import="java.util.List" %> <html> <head> <title>图书列表</title> </head> <body> <h2>图书列表</h2> <% // 获取用户 com.example.model.User user = (com.example.model.User) session.getAttribute("user"); if (user != null) { %> <p>欢迎,<%= user.getUsername() %> | <a href="logout">退出</a></p> <% } else { response.sendRedirect("index.jsp"); // 未登录,重定向到登录页面 return; } // 获取图书列表 List<Book> books = (List<Book>) request.getAttribute("books"); %> <% if (books != null && !books.isEmpty()) { %> <table border="1"> <thead> <tr> <th>书名</th> <th>作者</th> <th>类别</th> <th>ISBN</th> <th>库存</th> <th>操作</th> </tr> </thead> <tbody> <% for (Book book : books) { %> <tr> <td><%= book.getTitle() %></td> <td><%= book.getAuthor() %></td> <td><%= book.getCategory() %></td> <td><%= book.getIsbn() %></td> <td><%= book.getAvailableStock() %> / <%= book.getTotalStock() %></td> <td> <a href="bookDetail?bookId=<%= book.getBookId() %>">查看详情</a> <% if (book.getAvailableStock() > 0) { %> <a href="borrow?bookId=<%= book.getBookId() %>">借阅</a> <% } else { %> <span>已借完</span> <% } %> </td> </tr> <% } %> </tbody> </table> <% } else { %> <p>暂无图书</p> <% } %> <br> <a href="myBorrowings">我的借阅</a> <% if ("admin".equals(user.getRole())) { %> | <a href="addBook.jsp">添加图书</a> <% } %> </body> </html>
12-13
你的 `bookList.jsp` 页面功能完整,实现了: - 用户登录状态判断 - 图书列表展示 - 借阅操作控制(库存 > 0 才可借) - 角色权限控制(仅管理员显示“添加图书”) 但和之前一样,使用了 **JSP 脚本片段 `<% %>`** 来处理逻辑。虽然能运行,但从现代 Web 开发角度看,存在一些问题。 --- ## ✅ 当前代码的问题分析 | 问题 | 说明 | |------|------| | ❌ 大量 Java 代码嵌入 HTML | 违反 MVC 分离原则,难以维护 | | ❌ 在 JSP 中执行重定向 `response.sendRedirect()` | 可能导致响应已提交后再次写入的异常(`IllegalStateException`) | | ❌ 未对输出进行 XSS 防护 | 如 `book.getTitle()` 若含 `<script>` 会执行 JS | | ❌ 缺少空指针防护 | 比如 `user.getRole()` 前未判空 | | ❌ 表格样式原始 | 无 CSS 样式,用户体验差 | --- ## ✅ 改进建议:优化后的 `bookList.jsp`(不使用 JSTL) ```jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="com.example.model.User" %> <%@ page import="com.example.model.Book" %> <%@ page import="java.util.List" %> <html> <head> <title>图书列表</title> <meta charset="UTF-8"> <style> body { font-family: Arial, sans-serif; margin: 40px; } .header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; padding-bottom: 10px; border-bottom: 1px solid #eee; } .welcome { color: #333; font-size: 1em; } table { width: 100%; border-collapse: collapse; margin-top: 20px; } th, td { padding: 12px; text-align: left; border-bottom: 1px solid #ddd; } th { background-color: #f7f7f7; font-weight: bold; } tr:hover { background-color: #f9f9f9; } .btn { padding: 6px 10px; font-size: 0.9em; text-decoration: none; color: white; background-color: #007bff; border-radius: 4px; } .btn:hover { background-color: #0056b3; } .btn-disabled { color: #aaa; pointer-events: none; } .actions a { margin-right: 10px; } .no-books { color: #999; font-style: italic; } .links { margin-top: 20px; } .links a { margin-right: 15px; text-decoration: none; color: #007bff; } .links a:hover { text-decoration: underline; } </style> </head> <body> <% // 获取当前用户 User user = (User) session.getAttribute("user"); if (user == null) { response.sendRedirect("index.jsp"); return; // ⚠️ 必须 return,防止后续 HTML 输出 } // 获取图书列表 List<Book> books = (List<Book>) request.getAttribute("books"); %> <div class="header"> <div class="welcome">欢迎,<%= user.getUsername() %> | <a href="logout" style="color: red;">退出</a></div> <h2>📚 图书列表</h2> </div> <% if (books != null && !books.isEmpty()) { %> <table> <thead> <tr> <th>书名</th> <th>作者</th> <th>类别</th> <th>ISBN</th> <th>库存 / 总数</th> <th>操作</th> </tr> </thead> <tbody> <% for (Book book : books) { String title = book.getTitle() != null ? book.getTitle() : "未知"; String author = book.getAuthor() != null ? book.getAuthor() : "未知"; String category = book.getCategory() != null ? book.getCategory() : "未知"; String isbn = book.getIsbn() != null ? book.getIsbn() : "无"; int available = book.getAvailableStock(); int total = book.getTotalStock(); %> <tr> <td><%= title %></td> <td><%= author %></td> <td><%= category %></td> <td><%= isbn %></td> <td><%= available %> / <%= total %></td> <td class="actions"> <a href="bookDetail?bookId=<%= book.getBookId() %>" class="btn">查看详情</a> <% if (available > 0) { %> <a href="borrow?bookId=<%= book.getBookId() %>" class="btn" style="background-color:#28a745;">借阅</a> <% } else { %> <span class="btn btn-disabled">已借完</span> <% } %> </td> </tr> <% } %> </tbody> </table> <% } else { %> <p class="no-books">暂无图书信息。</p> <% } %> <div class="links"> <a href="myBorrowings">📘 我的借阅</a> <% if ("admin".equals(user.getRole())) { %> <a href="addBook.jsp">➕ 添加图书</a> <% } %> </div> </body> </html> ``` --- ## ✅ 改进点说明 | 改进 | 说明 | |------|------| | ✅ 添加 CSS 样式 | 提升美观性和用户体验 | | ✅ 使用 `class` 控制按钮样式 | 更易维护 | | ✅ 对 `null` 字段做默认值处理 | 防止页面出现 `null` 文本 | | ✅ `return` 后不再输出 HTML | 避免 `IllegalStateException` | | ✅ 结构清晰:头部 → 表格 → 操作链接 | 易读易改 | | ✅ 使用实体字符如 ` `、`©` 等(可选) | 更规范 | --- ## ⚠️ 关键注意事项 ### ❗ 不要在 JSP 中写业务逻辑! JSP 应只负责 **展示数据**,不要做以下事情: - 查询数据库 - 处理借阅逻辑 - 校验权限(除简单角色判断外) ✅ 正确做法是: - 在 `BookListServlet` 中查询图书列表并放入 `request.setAttribute("books", books)` - 在 `doGet()` 中检查登录状态,未登录则 `sendRedirect` - JSP 只负责读取 `request` 和 `session` 中的数据进行展示 --- ### 示例:BookListServlet 中预处理 ```java // BookListServlet.java protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 检查登录 User user = (User) request.getSession().getAttribute("user"); if (user == null) { response.sendRedirect("index.jsp"); return; } // 查询图书列表(从 Service 层获取) List<Book> books = bookService.getAllBooks(); request.setAttribute("books", books); // 转发到 JSP request.getRequestDispatcher("/WEB-INF/views/bookList.jsp").forward(request, response); } ``` 然后将 JSP 移动到 `/WEB-INF/views/` 目录下,防止被直接访问。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值