java - 运行它但在 s :iterator tag 中没有显示

标签 java hibernate jsp struts2 actioncontext

我用

<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/

相关文章:

java - 使用没有任何值(value)的 CDI @Named CDI 有什么意义吗?

java - 返回结果时保持提交任务的顺序

java - 使用 AJAX 更新 JSP 页面标签值

java - 无法解析 oracle.jdbc.OracleDriver

java - 调用@Cacheable注解的方法(org.springframework.cache.annotation.Cacheable)

java.util.list 的 Java EE 7 Batch (JSR-352) 语法?

mysql - 属性访问异常 : Error accessing field

html - 如何在任意子JSP页面的html头部添加元素

java - 线程中的异常 "main"org.hibernate.MappingException : Unknown entity: org. hibernate.employee

java - 如何在 Hibernate Criteria API 中添加 SQL-Server 查询提示