我在使用 Struts 2 时遇到问题。
- 从数据库中检索必要的数据。
- 在JSP中显示数据
- 在 JSP 中,我将提交表单。
- JSP 页面中不存在检索到的数据。
- 为了演示,我需要再次从数据库检索数据。
因此,我需要在同一页面的每个提交操作中从数据库检索数据。 但我不想像这样找回。 如何维护提交操作中的数据?
<小时/>JSP 页面:
<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 类:
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;
}
}
我需要使用 OtherAction1
、OtherAction2
和 OtherAction3
方法从数据库检索数据。
最佳答案
您必须从数据库中检索数据。这就是 Web 应用程序的工作原理,它会保留一些数据以从内存中卸载。将此类数据保存在内存中的成本很高,因为内存本身很昂贵并且服务器的物理内存容量有限。如果数据依赖于正在浏览的用户,则卸载数据就成为要求#1,因为应用程序中同时工作的可能用户数量可能会有所不同,并且内存要求可能会增加。
提交表单是将用户输入的数据传递到网络应用程序的常见过程。在提交之前,JSP 可能会呈现一些数据以供用户查看。如果所需的数据未加载,则意味着 Web 应用程序存在错误,应予以解决。
您应该考虑到使用服务器操作内存的成本很高。如果您将此类数据保留在 session 或应用程序范围内,则该数据将保存在操作内存中。在这些范围内保存的数据越多,需要的内存就越多。在设计 Web 应用程序时请记住这一点。
关于java - 如何在同一页面的每次提交操作中从数据库检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20172449/