I want to know how to get dynamically generated table row value (eg. empp_id) when clicking submit button that is generated for each row dynamically.
Below is my code. I used to generate each row and submit button for each row. I don't know how to use submit button to get and pass the value of 'id' column to another action.
Struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<package name="default" namespace="/" extends="struts-default">
<action name="edit" class="com.ojt.database.EditUserAction" method="execute">
<result name="success">edituser.jsp</result>
<result name="error"></result>
</action>
</struts>
EditUserAction.java
package com.ojt.database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class EditUserAction extends ActionSupport {
List<User> liUser=null;
public List<User> getLiUser() {
return liUser;
}
public void setLiUser(List<User> liUser) {
this.liUser = liUser;
}
@Override
public String execute() throws Exception {
Connection conn;
String ret = "error";
try {
String url = "jdbc:mysql://localhost:3306/test";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, "root", "root");
String sql = "SELECT * FROM user";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
liUser=new ArrayList<User>();
while (rs.next()) {
User user = new User();
user.setId(rs.getInt(1));
user.setName(rs.getString(2));
user.setPassword(rs.getString(3));
liUser.add(user);
}
conn.close();
ret = "success";
System.out.println(ret);
} catch (Exception e) {
e.printStackTrace();
ret = "error";
System.out.println(ret);
}
return ret;
}
}
edituser.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="display" uri="http://displaytag.sf.net/el"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Edit</title>
</head>
<body>
<table cellpadding="20" align="center">
<tr>
<th>Id</th>
<th>Name</th>
<th>Password</th>
</tr>
<s:iterator value="liUser">
<tr>
<td><s:property value="id" /></td>
<td><s:property value="name" /></td>
<td><s:property value="password" /></td>
<td><s:submit value="Edit" theme="simple" action="review" method="POST"/></td>
</tr>
</s:iterator>
</table>
</body>
</html>
I need your help please. I can't go any further, i am stuck here.
When you use <s:property/>
, you are not creating a form tag, but just simple text.
To send something printed with <s:property/>
, use an <s:hidden/>
in conjunction with it.
To send a list of object from JSP, you need to use the IteratorStatus object to specify an index.
You are not even using a form. You need a form to POST something in the standard, non-AJAX way.
You need two action's methods, or two actions: one to display the data, the other to edit it.
In your execute()
method you are initializing your list every time: liUser=new ArrayList<User>();
,
so the one coming from the page will always be lost.
You are in deep water. I suggest you to stop for a moment and take a closer look at HTML, HTTP, Struts2 and its tags, otherwise you will get frustrated quite soon.