Search code examples
jqueryjsonstruts2struts2-jquerystruts2-json-plugin

How to do partial page refresh using struts2-jquery plugin in struts2?


I want to do partial page refresh with the help of this. Take a scenario, we have a dropdown list according to select option of it, I want to refresh a div section of a page with data populated according to dropdown selection .

How to do this?

Updated :

i have tried this:

JSP Code:
On this Dropdown selection i want to populate (refresh) div.

<s:form id="RoleListForm">
      <s:label value="Roles"/>
      // JSON Action populating roleNameList on page Load
      <s:url id="fetchJsonRoleListUrl" action="fetchJsonRoleList"/> 
      <sj:select
         name="idRoleInfo"
         id="idRoleInfoList"
         href="%{fetchJsonRoleListUrl}"
         list="roleNameList"
         onChangeTopics="reloadRolePrivilegesDiv"
         listKey="idRoleInfo"
         listValue="roleName"
         emptyOption="true"/>
  </s:form>

Here is the div code that i want to populate according to DD selection, But i am not getting textfields value filled:

// JSON Action on page Load

 <s:url id="roleDetailsUrl" action="roleDetailsAction" />
 <sj:div href="%{roleDetailsUrl}" formIds="RoleListForm"  reloadTopics="reloadRolePrivilegesDiv">
    <s:textfield id="idRoleName" name="roleName" />
    <s:textfield id="idRolePrivileges" name="privileges"/>
 </sj:div>

I am getting this in div section On Browser:

{"roleName":"IT User","privileges":"IT User"}

Updated Part:

Action Class:

 public class GraphsAction extends ActionSupport {

private String startDate;
private String endDate;
private String bodyStats;
HomeService homeService = new HomeService();
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");

 public String reloadDatePicker() {
    Date date1 = new Date();
    Date date2 = new Date();
    Map session = ActionContext.getContext().getSession();
    Integer loginId = (Integer) session.get("loginId");
    if (loginId != null) {
        List list = homeService.getAllGraphData(loginId);
        List chestList = homeService.getChestGraphData(loginId);
        List waistList = homeService.getWaistGraphData(loginId);
        List hipsList = homeService.getHipsGraphData(loginId);
        List bicepsList = homeService.getBicepsGraphData(loginId);
        if (bodyStats.equals("")) {
            UserStats usetsts = (UserStats) list.get(0);
            date1 = usetsts.getUpadtedDate();
            this.startDate = formatter.format(date1);
            UserStats usetsts1 = (UserStats) list.get(list.size() - 1);
            date2 = usetsts1.getUpadtedDate();
            this.endDate = formatter.format(date2);
        }
        if (bodyStats.equals("0")) {
            UserStats usetsts = (UserStats) list.get(0);
            date1 = usetsts.getUpadtedDate();
            this.startDate = formatter.format(date1);
            UserStats usetsts1 = (UserStats) list.get(list.size() - 1);
            date2 = usetsts1.getUpadtedDate();
            this.endDate = formatter.format(date2);
        }
        if (bodyStats.equals("1")) {
            UserStats usetsts = (UserStats) list.get(0);
            date1 = usetsts.getUpadtedDate();
            this.startDate = formatter.format(date1);
            UserStats usetsts1 = (UserStats) list.get(list.size() - 1);
            date2 = usetsts1.getUpadtedDate();
            this.endDate = formatter.format(date2);
        }
        if (bodyStats.equals("2")) {
            UserStats usetsts = (UserStats) list.get(0);
            date1 = usetsts.getUpadtedDate();
            this.startDate = formatter.format(date1);
            UserStats usetsts1 = (UserStats) list.get(list.size() - 1);
            date2 = usetsts1.getUpadtedDate();
            this.endDate = formatter.format(date2);
        }
         return SUCCESS;
}

public String getEndDate() {
    return endDate;
}

public void setEndDate(String endDate) {
    this.endDate = endDate;
}

public String getStartDate() {
    return startDate;
}

public void setStartDate(String startDate) {
    this.startDate = startDate;
}
  public String getBodyStats() {
    return bodyStats;
}

public void setBodyStats(String bodyStats) {
    this.bodyStats = bodyStats;
}

}

Struts.xml:

<action name="jsonReloadDatePickerAction"  class="com.ebhasin.fitnessbliss.actions.GraphsAction" method="reloadDatePicker">
     <result  name="success">/jsps/datePicker.jsp</result>
 </action>

Solution

  • One of the options is to put content which is currently inside your <sj:div> to separate JSP page and configure your roleDetailsAction action to return that page instead of json result.