1

I have a problem with Struts 2.

  1. Retrieve necessary data from database.
  2. Show the data in JSP
  3. In the JSP, I will do submit the form.
  4. The retrieved data doesn't exist in the JSP page.
  5. To show, I need to retrieve the data from database again.

So, I need to retrieve the data from database in every submit action of the same page. But I don't want to retrieve like it. How can I maintain the data in submit action?


JSP Page:

<s:form name="XXXXXX" action="XXXXXXXX" method="post">
<div align="right" style="width: 80%">
    <display:table name="userSearchForms" pagesize="${pageSize}" 
        cellpadding="5px;" cellspacing="5px;" requestURI=""
        class="displayTable" id="displayTable"
        style="border: 3px solid #D9D9D9; border-collapse: collapse; width: 100%;height: 100%;align: right;border: 3px solid #D9D9D9;">
        <display:column title="#" style="border:1px solid black;">
            <input type="radio" name="prep"
                onclick="selectRadio(this.form, '<s:property value="#attr.displayTable.companyId"/>','<s:property value="#attr.displayTable.userId"/>')">
        </display:column>
        <display:column title="companyName" property="companyName" value="companyName"
            style="border:1px solid black;"></display:column>
        <display:column title="departmentName" property="departmentName" value="departmentName"
            style="border:1px solid black;"></display:column>
        <display:column title="userName" property="userName" value="userName"
            style="border:1px solid black;"></display:column>
        <display:column title="email" property="email" value="email"
            style="border:1px solid black;"></display:column>
    </display:table>
</div>

<div style="margin-left: 10%; width: 80%; text-align: right;">
    <s:submit value="Register" action="OtherAction1"/> 
    <s:submit value="Update" action="OtherAction2" /> 
    <s:submit value="Delete" action="OtherAction3"/>
</div>

</s:form>

Action Class:

public class sampleAction extends ActionSupport{
private List<searchForm> userSearchForms;
public List<searchForm> getUserSearchForms() {
        if (userSearchForms == null) {
            userSearchForms = new ArrayList<searchForm>();
        }
        return userSearchForms;
    }

public void setUserSearchForms(List<searchForm> userSearchForms) {
        this.userSearchForms = userSearchForms;
    }

public String execute(){
 // Retrieve the data from database;
 // Set to the userSearchForms;
 return SUCCESS;
}

public String OtherAction1(){
  // Other Program Logic;
  return SUCCESS;
}

public String OtherAction2(){
  // Other Program Logic;
  return SUCCESS;
}

public String OtherAction3(){
  // Other Program Logic;
  return SUCCESS;
}

}

I need to retrieve data from database in OtherAction1, OtherAction2 and OtherAction3 method.

Roman C
  • 49,761
  • 33
  • 66
  • 176
sithu
  • 11
  • 4

1 Answers1

0

You have to retrieve the data from the database. This is how web application works, it persist some data to unload from memory. Keeping in memory such data is expensive, because the memory itself is expensive and the server has a limited physical memory capacity. If the data is dependent from the user is browsing then unload the data becomes requirement #1 because the number of possible users concurrently working in application may vary and the memory requirement might increase.

Submit the form is a usual process of passing data entered by user to a web application. Before submit the JSP might render some data to view by the user. If the data required doesn't load means that a web application has errors and them should be resolved.

You should take it into account that using a server operating memory is expensive. If you will keep such data in session or application scope the data will hold in the operating memory. More data you save in those scopes more memory requirement needed. Keep it in mind while designing your web application.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Thank you for your answer. I wouldn't also like to keep the data in application server as you explain. Now, I can't submit the data. I can submit the data of textbox, but I can't submit the s:select and display:table of Struts2. – sithu Nov 24 '13 at 09:02
  • Please check the modified question and suggest me. – sithu Nov 24 '13 at 09:32
  • Look at [this](http://www.simplecodestuffs.com/pagination-example-in-struts-2/) example. You should initialize `userSearchForms` list before the action return JSP. – Roman C Nov 24 '13 at 10:29
  • I initialized userSearchForms in execute action firstly. Do I need to initialize again in other action(1,2,3)?Please suggest me. – sithu Nov 24 '13 at 11:28
  • You probably should read [this](http://stackoverflow.com/a/20074326/573032) answer to to get the values preinitialized if you want to use different actions to use with the same JSP. – Roman C Nov 24 '13 at 11:39
  • As my idea, I don't want to retrieve the data from database more than one time. I used JSF framework and it is ok for the problem. Now I need to retrieve the data in every action as preparable method. And I'd like to ask that it is ok? "many action in one class for one jsp page". – sithu Nov 24 '13 at 12:49
  • It's ok, but the `defaultStack` of interceptors might not suit your needs and you can change to `paramsPrepareParamsStack`. Look at [this](http://stackoverflow.com/a/13491559/573032) for description and usage. – Roman C Nov 24 '13 at 13:24