我正在检索数据库值并将它们放入数组列表(al)中。此数组列表以表格形式显示在JSP页面中。我想修改JSP页面中的值并使用Struts2更新数据库中的新值。我怎么做?从jsp中检索更新的数组列表struts2中的动作类
主要动作类
public class HelloWorldAction extends ActionSupport implements SessionAware{
ProjectDb pd;
public ProjectDb getPd() {
return pd;
}
public void setPd(ProjectDb pd) {
this.pd = pd;
}
ArrayList<ProjectDb> al=new ArrayList<ProjectDb>();
public ArrayList<ProjectDb> getAl() {
return al;
}
public String status() throws Exception{
boolean flag=false;
try{
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection conn = DriverManager
.getConnection("jdbc:ucanaccess://D:\\Db1.mdb");
username=(String) map.get("user");
PreparedStatement ps=conn.prepareStatement(
"SELECT * FROM StaleInGers WHERE (((StaleInGers.mailId)=(Select email from DBA where username='"+username+"' and email='"+email+"')))");
//ps.setString(1,username);
//ps.setString(2,password);
ResultSet rs=ps.executeQuery();
al=new ArrayList<ProjectDb>();
while(rs.next()){
pd =new ProjectDb();
pd.setProject(rs.getString("project"));
pd.setStatus(rs.getString("status"));
pd.setComments(rs.getString("comments"));
al.add(pd);
flag=true;
}
} catch (Exception e) {
System.out.println("Exception : " +e);
}
if(flag==true){
return "success";
}
else{
return "error";
}
}
}
这是POJO类
public class ProjectDb {
private String project,status,comments,email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getProject() {
return project;
}
public void setProject(String project) {
this.project = project;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
}
这是JSP页面,在这里我显示数组列表
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4
/loose.dtd">
<html>
<head>
<style type="text/css">
table,td,th {
border: 1px solid green;
width:100%;
}
th {
background-color: green;
color: white;
}
</style>
</head>
<body>
<form action="update">
<table >
<tr>
<th>Project</th>
<th>Status</th>
<th>Comments</th>
</tr>
<s:iterator value="al" id="array" status="alStatus">
<tr>
<td><s:property value="%{project}"/></td>
<td><s:textfield name="array[%{#alStatus.index}].status" value="%{status}" theme="simple"
/></td>
<td><s:textfield name="array[%{#alStatus.index}].comments" value="%{comments}" theme="simple"
/></td>
</tr>
</s:iterator>
</table><br><br>
<input style="opacity: 0.7; border-radius: 5px; border: 0; width: 250px;
height:35px;
font-family: Goudy Old Style; font-size: 22px; background: #00CC80;"
type="submit" value="Submit">
</form>
</body>
</html>
你可以用很多方式做到这一点,你有一个具体的问题吗? – 2014-11-25 10:42:46