Hi Prashant,

For simple objects, the Type Conversion[1] feature is intended to convert, say, a string value in into an object before its set in your action.

In your case however, there's no automatic way for struts 2 to load the selected Entity instance. At best, the key/id from the select is set in a property and you will need to load the corresponding Entity instance yourself. You could do this in your execute method, or if you use the "params-prepare-params" technique [2] you can do it in the prepare method so you can also reference properties of the Entity.

My preference is to not allow a JSP to modify an Entity directly because I prefer to validate it before allowing the Entity to become "dirty". It's not a big issue but it affects how validation errors or exceptions are handled.

In summary, you do have the lookup the entity by ID yourself.

regards,
Jeromy Evans

[1] http://struts.apache.org/2.x/docs/type-conversion.html
[2] http://struts.apache.org/2.x/docs/interceptors.html

Prashant Khanal wrote:
Hello all
I just started working on struts 2 and stuck in a problem.
I have a page that consists of a select element and a submit button:
<s:form name="conceptSelection" action="sequenceSelection" method="post">
    <li><s:select label="Select Concept" name="concept" list="banners"
listKey="id"
        listValue="bannerName" /></li>
    <li><s:submit cssClass="button" /></li>
</s:form>
The select element populates list of Banner objects.
@Entity
@Table(name="DTC_BANNER")
public class Banner implements Serializable {

    private Long id;
    private String bannerName;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="banner_id")
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    @Column(name="banner_name")
    public String getBannerName() {
        return bannerName;
    }
    public void setBannerName(String bannerName) {
        this.bannerName = bannerName;
    }

}

So i fetch the list of available banners from the database and populate in
the select element. The select element displays correctly.

Now after the user clicks on the submit button the
SequenceSelectionAction.java comes into act and now i want to fetch the
banner selected in the previous page. In fact i could only fetch the id of
the banner object selected as the listKey property is assigned "id".

Is there any way i can get the Banner object rather than just id?

------------------------------------------------------------------------

No virus found in this incoming message.
Checked by AVG Free Edition. Version: 7.5.516 / Virus Database: 269.20.4/1275 - Release Date: 12/02/2008 3:20 PM


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to