我用
<class name="Topic" table="topic">
.......
<set name="replies" inverse="true" lazy="false" cascade="save-update">
<key column="TOPIC_ID"/>
<one-to-many class="Reply"/>
</set>
</class>
而且我看到 replies 不是 null 并且在 topic.replies
中有元素;
Topic topic = topicService.getTopicById(topicId);
ActionContext actionContext = getActionContext();
actionContext.put("topic", topic);
在 JSP 中:
<s:iterator value="#topic.replies">
<s:property value="title"/>
</s:iterator>
无标题显示。 然后我更改我的代码
Topic topic = topicService.getTopicById(topicId);
ActionContext actionContext = getActionContext();
actionContext.put("replies", topic.getReplies);
在 JSP 中
<s:iterator value="#replies">
<s:property value="title"/>
</s:iterator>
显示标题的值。
我不知道为什么标题不是第一种方式显示。
最佳答案
为什么要那样使用 ActionContext?
ActionContext 对于从其他地方(例如 Helper 类)访问数据(已对 Action 可用)很有用,无需传递任何参数。
来自文档:What is the ActionContext
To keep signatures brief, and methods useful, the framework uses two techniques: Dependency Injection and Thread Local, both of which, in turn, rely on the ActionContext.
From anywhere within an Struts 2 application, you can obtain a reference to the [ActionContext] by calling
ActionContext context = ActionContext.getContext();
For example, if a helper class is called from an Action, and if it happens to need access to ServletContext (maybe it is writing a file and needs ServletContext to get a path to it), the helper can obtain the ActionContext directly. Nothing needs to be passed from the Action.
JSP 属性通常从 Action 中读取,并且 Action 必须通过访问器(或 getter )公开这些属性;只需将 Hibernate 结果映射到 Action 中的某个 DTO。
关于java - 运行它但在 s :iterator tag 中没有显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16010069/