Question

I use

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

and I have seen replies is not null and have elements in topic.replies;

Topic topic = topicService.getTopicById(topicId);
ActionContext actionContext = getActionContext();
actionContext.put("topic", topic);

and in JSP:

<s:iterator value="#topic.replies">
  <s:property value="title"/>
</s:iterator>

no title display. and then I change my code

Topic topic = topicService.getTopicById(topicId);
ActionContext actionContext = getActionContext();
actionContext.put("replies", topic.getReplies);

in JSP

<s:iterator value="#replies">
  <s:property value="title"/>
</s:iterator>

the value of title is displayed.

I don't know why title isn't displayed in first way.

Was it helpful?

Solution 2

Replies are configured as lazy, so they are not available until you call topic.getReplies. This is actually initializes the lazy collection via accessing entity's proxy. On the other hand you are trying to use OGNL to access the entity and it find it in other way, so the collection is not initialized.

OTHER TIPS

Why are you using ActionContext like that ?

ActionContext is useful for accessing data (already available to the Action) from somewhere else, for example an Helper class, without the need to pass any parameters.

From the documentation: 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 properties are usually read from the Action, and the Action must expose those properties through accessors (or getters); just map your Hibernate result to some DTO in your Action.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top