如何通过单击具有 struts2 操作的按钮来获取 table 元素的 ID
How can I get the id of the table element by clicking on the button with struts2 action
我有一个产品 table,其数据来自 mysql。该按钮与重定向到 jsp 的操作相关联,该操作表明产品已被购买。我需要此操作来创建一个 table 并在其中插入客户 ID 和产品。我的问题是无法检索该产品的 ID。
我的行动
package it.pwm.wynd.action.customer;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
import it.pwm.wynd.pojo.customer.Customer;
import it.pwm.wynd.pojo.order.Order;
import it.pwm.wynd.pojo.order.OrderDAO;
import it.pwm.wynd.pojo.order.OrderDAOFactory;
import it.pwm.wynd.pojo.product.Product;
import it.pwm.wynd.pojo.product.ProductDAO;
import it.pwm.wynd.pojo.product.ProductDAOFactory;
public class BuyProduct extends ActionSupport implements SessionAware {
private static final long serialVersionUID = 1L;
private Customer customer;
private Product Product;
private Order order = new Order();
private Map<String,Object> session;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
public Map<String,Object> getSession() {
return session;
}
public void setSession(Map<String,Object> session) {
this.session = session;
}
public String execute() throws Exception {
Customer customer = (Customer) session.get("customer");
if(customer != null) {
System.out.println("customer " + customer.getUsername() + " in session");
}else {
return INPUT;
}
ProductDAO productDAO = ProductDAOFactory.getDAO();
OrderDAO orderDAO = OrderDAOFactory.getDAO();
Product product = productDAO.getProductById(productId); // i need this
order.setCustomer(customer);
order.setProduct(product);
orderDAO.save(order);
return SUCCESS;
}
}
我的jsp
<table class="innertube">
<thead>
<tr>
<th style="display: none;">Id</th>
<th>Name</th>
<th>Quantity</th>
<th>Category</th>
<th>Price</th>
<th></th>
</tr>
</thead>
<tbody>
<s:iterator value="list">
<tr>
<td style="display: none;"><s:property value="productId" /></td>
<td><s:property value="productName" /></td>
<td><s:property value="quantity" /></td>
<td><s:property value="category" /></td>
<td><s:property value="price" /> €</td>
<td><a
href="BuyProduct.action"><button
class="button">
<i class="fa fa-fw fa-cart-plus"></i>
</button></a></td>
</tr>
</s:iterator>
</table>
您需要将所选项目的ID发送给操作,否则它不会知道选择了什么。您至少可以通过两种方式做到这一点:
- 提交表单而不是使用 link 并将 id 放入隐藏的表单字段中
类似这样的东西(我有一段时间没有使用Struts2所以可能会有错误):
<s:form action="BuyProduct.action">
<s:hidden name="selectedProduct" value="%{productId}"/>
<s:submit/>
</s:form>
- 将 id 作为查询参数附加到操作 url,例如通过
<s:url>
标签
示例(与上述相同的免责声明):
<s:url value="BuyProduct.action" var="buyUrl">
<s:param name="selectedProduct" value="%{productId}" />
</s:url>
<a href="${buyUrl}"> ... </a>
我有一个产品 table,其数据来自 mysql。该按钮与重定向到 jsp 的操作相关联,该操作表明产品已被购买。我需要此操作来创建一个 table 并在其中插入客户 ID 和产品。我的问题是无法检索该产品的 ID。
我的行动
package it.pwm.wynd.action.customer;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
import it.pwm.wynd.pojo.customer.Customer;
import it.pwm.wynd.pojo.order.Order;
import it.pwm.wynd.pojo.order.OrderDAO;
import it.pwm.wynd.pojo.order.OrderDAOFactory;
import it.pwm.wynd.pojo.product.Product;
import it.pwm.wynd.pojo.product.ProductDAO;
import it.pwm.wynd.pojo.product.ProductDAOFactory;
public class BuyProduct extends ActionSupport implements SessionAware {
private static final long serialVersionUID = 1L;
private Customer customer;
private Product Product;
private Order order = new Order();
private Map<String,Object> session;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
public Map<String,Object> getSession() {
return session;
}
public void setSession(Map<String,Object> session) {
this.session = session;
}
public String execute() throws Exception {
Customer customer = (Customer) session.get("customer");
if(customer != null) {
System.out.println("customer " + customer.getUsername() + " in session");
}else {
return INPUT;
}
ProductDAO productDAO = ProductDAOFactory.getDAO();
OrderDAO orderDAO = OrderDAOFactory.getDAO();
Product product = productDAO.getProductById(productId); // i need this
order.setCustomer(customer);
order.setProduct(product);
orderDAO.save(order);
return SUCCESS;
}
}
我的jsp
<table class="innertube">
<thead>
<tr>
<th style="display: none;">Id</th>
<th>Name</th>
<th>Quantity</th>
<th>Category</th>
<th>Price</th>
<th></th>
</tr>
</thead>
<tbody>
<s:iterator value="list">
<tr>
<td style="display: none;"><s:property value="productId" /></td>
<td><s:property value="productName" /></td>
<td><s:property value="quantity" /></td>
<td><s:property value="category" /></td>
<td><s:property value="price" /> €</td>
<td><a
href="BuyProduct.action"><button
class="button">
<i class="fa fa-fw fa-cart-plus"></i>
</button></a></td>
</tr>
</s:iterator>
</table>
您需要将所选项目的ID发送给操作,否则它不会知道选择了什么。您至少可以通过两种方式做到这一点:
- 提交表单而不是使用 link 并将 id 放入隐藏的表单字段中
类似这样的东西(我有一段时间没有使用Struts2所以可能会有错误):
<s:form action="BuyProduct.action">
<s:hidden name="selectedProduct" value="%{productId}"/>
<s:submit/>
</s:form>
- 将 id 作为查询参数附加到操作 url,例如通过
<s:url>
标签
示例(与上述相同的免责声明):
<s:url value="BuyProduct.action" var="buyUrl">
<s:param name="selectedProduct" value="%{productId}" />
</s:url>
<a href="${buyUrl}"> ... </a>