Search code examples
javajavascriptstruts2jqgriddisplaytag

Update a particular record in a table using struts2 and javascript


How can I make the particular record editable and after changing the fields update to that particular record. I have never used JavaScript so I am facing a lot of perplexion between the javascript, Struts 2 and hibernate. Following are the required material to help me out. I want to:

  1. Make the particular record editable.
  2. Update that record

organizationTable.jsp

<display:table id="record" name="listOfOrganization" requestURI="/organization" pagesize="10">
    <display:caption>Organization Records</display:caption>
    <display:column property="id" />
    <display:column property="name" />
    <display:column property="ceo" />
    <display:column property="address" />
    <display:column property="establishedDate" />
    <display:column property="category" />
    <display:column property="url" />
    <display:column property="phoneNumbers" />
    <display:column property="fax" />
    <display:column property="status" />
    <display:column media="html">
        <form action="">
            <input type="submit" value="Update" />
        </form>
    </display:column>
</display:table>

Organization class

@Entity
@Table(name="ORGANIZATION")
public class Organization {
@Id @Column(name="UID")
    private String id;
    @Column(name="NAME")
    private String name;
    @Column(name="CEO")
    private String ceo;
    @Column(name="ADDRESS")
    private String address;
    @Column(name="ESTABLISHED_DATE")
    private String establishedDate;
    @Column(name="CATEGORY")
    private String category;
    @Column(name="URL")
    private String url;
    @Column(name="PHONE_NUMBERS")
    private String phoneNumbers;
    @Column(name="FAX")
    private String fax;
    @Column(name="STATUS")
    private String status;
// setters & getters
}

Snapshot

enter image description here


Solution

  • DisplayTag is quite old now, latest version is from 2008;

    If you can (eg. if you are NOT adding functionalities to an existing, huge legacy project, but instead you are at the beginning and/or starting a new project right now), I suggest you to invest some time and study jqGrid, probably the best grid out there today.

    Your specific case with examples and full code is available in the demo page, under

    1. Row Editing (new) Basic Example

    2. Row Editing (new) Custom Edit

    Note that, since you are using Struts2, and you don't know Javascript, you mihgt be interested in an easier approach, using the GridTag from the Struts2-jQuery-Grid-plugin created by jogep.

    Take a look at the Struts2-jqGrid Showcase to see what it can do,
    and at the documentation to see how (easy it is) to do it.