【问题标题】:retrieve updated array list from jsp to action class in struts2从jsp到struts2中的动作类检索更新的数组列表
【发布时间】:2015-01-23 06:06:57
【问题描述】:

我正在检索数据库值并将它们放入数组列表(al)中。这个数组列表以表格的形式显示在 JSP 页面中。我想修改 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>

【问题讨论】:

  • 您可以通过多种方式做到这一点,您有具体的问题吗?

标签: java arrays jsp struts2


【解决方案1】:

首先想到的是将表格字段设置为输入类型,并将来自 ArrayList 的值放在每个输入值参数上。在此之后创建一个更新方法(如 satus() 之一),并使用您的 pojo 的设置器更新输入值,从而触发表单操作上的更新方法。

它应该看起来像这样:

HelloWorldAction 中的更新方法:

public void update(ProjectDB prjDb) throws Exception{
boolean flag=false;
PreparedStatement ps = null;


try{
    Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");

    Connection conn = DriverManager
            .getConnection("jdbc:ucanaccess://D:\\Db1.mdb");

    conn.setAutoCommit(false);
    username=(String) map.get("user");
    /*Selecting which field would be updated */
    if(!"".equals(prjDb.getEmail().trim())){
     ps=conn.prepareStatement(  
            "UPDATE StaleInGers SET email=?  WHERE (((StaleInGers.mailId)=(Select email from DBA where username='"+username+"' and email='"+email+"')))");  
     ps.setString(1,prjDb.getEmail());
    }
    //else if(...){...} --> Treat all the cases, if email is not empty, and another field is not empty, if only one field is not empty, etc..


    int i = ps.executeUpdate();
    conn.commit();  




        if(i>0){ flag=true; }
    }


} catch (Exception e) {
    System.out.println("Exception : " +e);
}  
if(flag==true){
    return "success";
}
else{
    return "error";
}       
}
//Don't forget to close the connection and prepared statement

字段必须从数组中填充,以使您进入此页面(您可以在此处进行更新),就像您在 JSP 中所做的那样。这是更新方法的 Demo 示例,但它应该可以工作,我让你自己编写其余代码。

【讨论】:

  • 您能详细说明一下吗?我是 Java 新手,但是,我确实尝试使用 getter 和 setter 检索值,但我什么也没得到..
  • 查看本教程了解更多详情:java4s.com/struts-tutorials/…
【解决方案2】:
  1. 在迭代器中使用var 而不是id(已弃用);在你的情况下,它甚至不需要顺便说一句。

  2. 确保在您的目标操作 (update) 中有一个名为 arrayList&lt;ProjectDb&gt;(使用接口列表,而不是实现 ArrayList),带有 getter/setter;

  3. 使用&lt;input type="hidden" /&gt; 发送您使用&lt;s:property /&gt; 显示的值:

    <td>
        <s:hidden name="array[%{#alStatus.index}].project" value="%{project}" />
        <s:property value="%{project}" />
    </td> 
    

【讨论】:

  • 你能详细说明我应该在行动课上做什么吗?我确实尝试了你的建议,但我没有从 jsp 中得到任何值..
  • name 属性用于映射目标操作中的目标属性。如果您写name="foo[0].project",则需要在update 操作(由您的表单定位)private List&lt;ProjectDb&gt; foo;。如果你写name="foo.project",你需要private ProjectDb foo;。如果你写array[%{#alStatus.index}].project,你需要private List&lt;ProjectDb&gt; array;。不要忘记二传手;)
  • 漂亮的头像,卡尔文和霍布斯 FTW 8)
【解决方案3】:

以前我需要将 javascript 数组传递给 struts 2 的动作类,我尝试使用 json,var arr1=["1", "2", "3"], in

$.getJSON('ajaxSaveDetails', {
          javaArray : JSON.stringify(arr1)
          }.fail(function(){
              $(".error-txt").text("Error occured");
    });

在Action类中,我写了private String javaArray ;和方法

jsonArray = (JSONArray)new JSONParser().parse(javaArray);

我得到了正确的数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 2012-01-14
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多