Action 中設定 getMonitorObj,JSP 用 <s:property value="getMonitorObj.idMon" /> 取值卻為空。
Struts2 預設只將 Action 實例本身 (this) 推入 ValueStack 栈頂。Action 的實例屬性 (getter/setter) 會被 OGNL 自動存取,但區域變數 或未暴露 getter 的欄位 不在 ValueStack 中。
public class PylAction extends ActionSupport {
private MonitorObj getMonitorObj; // 實例屬性
public MonitorObj getGetMonitorObj() { return getMonitorObj; } // 必須有 getter
public String pylShow() {
getMonitorObj = pylmonitormanager.pylgetmoninfo(...);
return SUCCESS;
}
}
ActionContext.getContext().getValueStack().set("getMonitorObj", getMonitorObj);
// 或
ActionContext.getContext().put("getMonitorObj", getMonitorObj); // 放入 Request Map
<!-- 直接用屬性名,OGNL 會呼叫 getGetMonitorObj() -->
<input name="test" value="<s:property value='getMonitorObj.idMon' />" />
<!-- 注意單引號包裹屬性值,避免雙引號衝突 -->
<s:debug></s:debug>,瀏覽器開啟除錯視窗檢視 ValueStack 完整結構。struts.xml 是否攔截器包含 params、modelDriven 等影響屬性注入的攔截器。採用 ModelDriven 介面或單純 POJO Action + Getter/Setter,讓框架自動管理 ValueStack,避免手動操作 ActionContext。