首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

SSH 框架 JSP 頁面無法顯示 Action 值:Hibernate 正常但 Struts2 值棧問題

     編輯:程式設計疑難解答

問題現象

Hibernate 查詢、插入正常,但 JSP 頁面取不到 Action 中的屬性值(如 `list`、`pager`)。

常見原因排查

  1. Action 未實現 `ModelDriven` 或 getter 缺失:JSP 通過 OGNL 訪問 Action 實例屬性,必須有 `public List getList()` 等 getter。
  2. 攔截器棧配置錯誤:`struts.xml` 中 `` 缺失,導致參數注入、值棧準備失效。
  3. JSP 標籤庫引用錯誤:`<%@ taglib prefix="s" uri="/struts-tags" %>` 缺失或 URI 錯誤,導致 `` 不解析。
  4. Action 返回結果類型錯誤:應返回 `dispatcher`(默認)轉發到 JSP,若用 `redirect` 會丟失請求屬性。
  5. Session/Request 作用域混淆:代碼中用 `request.getSession().setAttribute("lists", ...)`,JSP 應用 `${lists}` 或 `` 取值,而非直接 ``(後者找值棧)。

修正建議

// Action 中
public String findAll() {
    List list = studentService.findAll(pager);
    // 方式1:放值棧(推薦,配合 getter)
    this.list = list; 
    this.pager = pager;
    return SUCCESS; // struts.xml /adminOperator.jsp
    
    // 方式2:放 Session(需 JSP 用 #session 取)
    // request.getSession().setAttribute("lists", list);
    // return SUCCESS;
}

// JSP 中(方式1)
<s:iterator value="list" var="stu">
    <s:property value="#stu.name"/> ...
</s:iterator>
<s:property value="pager.currentPage"/>

// JSP 中(方式2)
<c:forEach items="${sessionScope.lists}" var="stu"> ... </c:forEach>

調試技巧

  • 在 JSP 頂部加 `` 查看值棧內容。
  • 檢查控制台是否有 OGNL 異常堆疊。
  • 確認 `struts.xml` 中 `` 繼承正確。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved