Skip to content
Advertisement

How can I get the id of the table element by clicking on the button with struts2 action

I have a product table whose data is taken from mysql. The button is associated with an action that redirects to a jsp that shows that the product has been purchased. I need this action to create a table where it inserts the id of the customer and the product. My problem is being able to retrieve the id for that product.

my action

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;
    }


}

my 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" /> &euro;</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>

Advertisement

Answer

You need to send the id of the selected item to the action, otherwise it won’T know what was selected. You could do this in at least 2 ways:

  1. Submit a form instead of using a link and put the id into a hidden form field

Something like this (I didn’t use Struts2 in a while so there may be errors):

<s:form action="BuyProduct.action">
  <s:hidden name="selectedProduct" value="%{productId}"/>
  <s:submit/>
</s:form>
  1. append the id as a query parameter to the action url, e.g. via the <s:url> tag

Example (same disclaimer as above):

<s:url value="BuyProduct.action" var="buyUrl">
   <s:param name="selectedProduct" value="%{productId}" />
</s:url>
<a href="${buyUrl}"> ... </a>
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement