1

I'm using Struts 2.

I want to create a custom interceptor debug class to display in interceptor debugging mode browser all attributes field value when user click save button.

ProduitDto:

public class ProduitDto {
    private String reference;
    private String designation;
    private double prix;
    private int quantite;
    private boolean promo;
    
    public ProduitDto(String reference, String designation, double prix, int quantite, boolean promo) {
        this.reference = reference;
        this.designation = designation;
        this.prix = prix;
        this.quantite = quantite;
        this.promo = promo;
    }

    public ProduitDto() {
    }
  
    public String getReference() {
        return reference;
    }

    public void setReference(String reference) {
        this.reference = reference;
    }

    public String getDesignation() {
        return designation;
    }

    public void setDesignation(String designation) {
        this.designation = designation;
    }

    public double getPrix() {
        return prix;
    }

    public void setPrix(double prix) {
        this.prix = prix;
    }

    public int getQuantite() {
        return quantite;
    }

    public void setQuantite(int quantite) {
        this.quantite = quantite;
    }

    public boolean isPromo() {
        return promo;
    }

    public void setPromo(boolean promo) {
        this.promo = promo;
    }
}
    

ProduitAction:

import com.id.dto.ProduitDto;
import com.id.entites.Produit;
import com.id.service.ICatalogueService;
import com.id.service.SingletonService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class ProduitAction extends ActionSupport implements ModelDriven<Produit> {
    private ProduitDto produitDto = new ProduitDto();
    private Produit produit = new Produit();
    private List<Produit> produits;
    private String ref;
    private boolean editMode = false;
    private ICatalogueService service = SingletonService.getService();

    public String index( ) {
        produits = service.listProduit();
        return SUCCESS;
    }
    
    public String save() {
        if (!editMode) 
        service.ajouterProduit(produit);
        else
        service.miseAjourProduit(produit);

        produits = service.listProduit();
        return SUCCESS;
    }
    
    public String delete() {
        service.supprimerProduit(ref);
        produits = service.listProduit();
        return SUCCESS;
    }
    
    public String edit() {
        editMode = true;
        produit = service.recupererProduit(ref);
        service.miseAjourProduit(produit);
        produits = service.listProduit();
        return SUCCESS;
    }
    
    public Produit getProduit() {
        return produit;
    }
    
    public String getRef() {
        return ref;
    }

    public void setRef(String ref) {
        this.ref = ref;
    }

    public void setProduit(Produit produit) {
        this.produit = produit;
    }

    public List<Produit> getProduits() {
        return produits;
    }

    public void setProduits(List<Produit> produits) {
        this.produits = produits;
    }

    public ProduitDto getProduitDto() {
        return produitDto;
    }
    public void setProduitDto(ProduitDto produitDto) {
        this.produitDto = produitDto;
    }

    public boolean isEditMode() {
        return editMode;
    }

    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }

    @Override
    public Produit getModel() {
        return produit;
    }
}

JSP:

<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Produits</title>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
</head>
<body>
<div>
     <s:form action="save" method="post">
        <s:textfield label="REF" name="produit.reference"></s:textfield>
        <s:textfield label="Designation" name="produit.designation"></s:textfield>
        <s:textfield label="Prix" name="produit.prix"></s:textfield>
        <s:textfield label="Quantite" name="produit.quantite"></s:textfield>
        <s:checkbox label="Promo" name="produit.promo"></s:checkbox>
     <!--   <s:textfield name="editMode"></s:textfield> permet de voir une valeur du model  -->
        <s:hidden name="editMode"></s:hidden>
        <s:submit value="Save"></s:submit>
     </s:form>
</div>
<div>
    <table class="table1">
     <tr>
        <th>REF</th>
        <th>DES</th>
        <th>PRIX</th>
        <th>QUANTITE</th>
        <th>PROMO</th>
     </tr>
     <s:iterator value="produits">
         <tr>
            <td><s:property value="reference"/></td>
            <td><s:property value="designation"/></td>
            <td><s:property value="prix"/></td>
            <td><s:property value="quantite"/></td>
            <td><s:property value="promo"/></td>
            <s:url namespace="/" action="delete" var="lien1">
                <s:param name="ref">
                    <s:property value="reference"/>
                </s:param>
            </s:url>
            <s:url namespace="/" action="edit" var="lien2">
                <s:param name="ref">
                    <s:property value="reference"/>
                </s:param>
            </s:url>
            <td><s:a href="%{lien1}">Suppr</s:a></td>
            <td><s:a href="%{lien2}">Edit</s:a></td>
         </tr>
     </s:iterator>
    </table>
</div>
</body>
</html>

struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <package name="default" namespace="/" extends="struts-default">
        <default-action-ref name="index"></default-action-ref>
        <action name="index">
            <result>views/index.jsp</result>
        </action>
        <action name="produits" class="com.id.web.ProduitAction" method="index">
            <result name="success">views/Produits.jsp</result>
        </action>
        <action name="save" class="com.id.web.ProduitAction" method="save">
            <result name="success">views/Produits.jsp</result>
            <result name="input">views/Produits.jsp</result> 
        </action>
        <action name="delete" class="com.id.web.ProduitAction" method="delete">
            <result name="success">views/Produits.jsp</result>
        </action>
        <action name="edit" class="com.id.web.ProduitAction" method="edit">
            <result name="success">views/Produits.jsp</result>
        </action>
    </package>
</struts>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="struts_blank" version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <display-name>Struts Blank</display-name>

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
      org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
  </filter>

  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

Interceptor product :

public class ProductCustomInterceptor implements Interceptor
{
    private static final long serialVersionUID = 1L;
 
 
    @Override
    public String intercept(ActionInvocation invocation) throws Exception 
    {
        System.out.println("ProductCustomInterceptor intercept() is called...");
        System.out.println(invocation.getAction().getClass().getName());
        return invocation.invoke();
    }
}

How can I intercept all filed value of my class product when I click save button in my jsp page by using interceptor class with debugging mode browser?

Roman C
  • 49,761
  • 33
  • 66
  • 176
obela06
  • 307
  • 5
  • 15
  • I'm not sure I really understand what you're after but do you know how to create and use custom interceptors in the first place? If the answer is "no" then please have a look at the documentation and tutorials first. If "yes" then did you try checking for which action is being called and printing only when a product should be saved? – Thomas Mar 30 '21 at 12:21
  • Why use an interceptor for this? Interceptors are for general-purpose cross-cutting concerns. If you're trying to debug a single action put the logging code in the action. – Dave Newton Mar 30 '21 at 13:57
  • Hi @Thomas, I know how to create an interceptor. I must create my productInterceptorClass implements Interceptor, i must declare it in the struts.xml file and active devMode true in struts xml file. My problem is how to call my save action in my productAction.java and product.jsp page when user click button save. I want to see all parameter setted by user before saving. Can you help me to do this? – obela06 Mar 30 '21 at 15:30
  • Hi @DaveNewton i want to see all value(reference,designation,prix,quantite and promo) setted before saving ecause. Can you help me with this ? – obela06 Mar 30 '21 at 15:33
  • Well, you're alrady calling action "save" when submitting your form so that should be done already. You just need to put the interceptor into your stack and - dependending on whether you put it before or after the parameter interceptor - either query the form data directly from the request or get it from your action/form bean. The interceptor should be executed before the action method in any case (method execution is last in the chain). – Thomas Mar 30 '21 at 15:36
  • Hi @Thomas can you please add code in my interceptor product to explain me your solution or if you can please give an example of code because i doesn't understand – obela06 Mar 30 '21 at 15:56
  • @obela06 So log it before you save it--I still don't understand why an interceptor would be good for this. In any case, you're implementing `ModelDriven`, so you could still make it generic by checking for a `ModelDriven` action, getting the model, and either use a generic interface on all your models to log it, or use reflection to get the model fields and log them. – Dave Newton Mar 30 '21 at 16:28
  • 1
    @obela06 But again: using an interceptor for a single action is not the best approach. – Dave Newton Mar 30 '21 at 16:29
  • @DaveNewton please can you give me an example with code.Because right now, it's a bit of a blur to me. I am a struts2 beginner. Can you please propose me a code example like mine or if you can from my code, add or show me how to implement the way to retrieve the values entered by the user how I can log him. – obela06 Mar 30 '21 at 19:44
  • I agree with Dave but if you really want to do it with an interceptor: have a look at `ActionInvocation.getInvocationContext()` and `ActionContext.getParameter()`. To check for the correct action have a look at `ActionInvocation.getProxy()`, `ActionProxy.getAction()` (hint: use `instanceof`) and `ActionProxy.getMethod()`. – Thomas Mar 30 '21 at 19:45
  • What Dave means is that before calling the service in your `save()` method, why don't you just log `produit`? – Thomas Mar 30 '21 at 19:47
  • @Thomas Can you add this code to my ProductCustomInterceptor class at the top of the post so I can see it well. I use interceptors to see the values entered by the users to know in case of database saving problem which field is not filled or which field is empty. Can you detail the code by adding if necessary in my code on the post or write what I have to add, because as I say, I'm really beginner in struts2 – obela06 Mar 30 '21 at 20:17
  • Why don't you have a look at the methods I mentioned and some documentation to get a better understanding of Struts 2? This will help more in the long run. I also don't quite understand why you think you need interceptors. As Dave and I already pointed out: log `produit` _before_ calling the service - the values entered by the user will be mapped to that instance (look at your jsp, e.g. `name="produit.reference"`) - what's not in there hasn't been entered by the user if what you posted is your actual code (so nothing else changes `produit` between action initialization and the call to `save()` – Thomas Mar 30 '21 at 20:37
  • @Thomas, To make it simple, can you give me an example of a code (or a link to a tutorial) that via a jsp and a class struts action can make the backup in database and if possible log either by interceptor or other means to what the jsp sends to the action to then be saved in database – obela06 Mar 31 '21 at 11:41
  • Well, the simplest code in your case would be to add a `toString()` to `ProduitDto` that puts everything into the string and then call `System.out.println(produit)` at the start of `save()`. Start from there and work your way though loggers, getting the request parameters directly etc. – Thomas Mar 31 '21 at 11:48
  • Which browser are you using? – Roman C Apr 06 '21 at 18:16
  • I didn't get actually your question, what do you mean "debugging mode browser"? If you want to create a custom interceptor i.e. for logging then you can try [this](https://stackoverflow.com/a/24385658/573032) answer. If you are learning to create a custom interceptor then you can start [there](https://stackoverflow.com/a/16256030/573032). – Roman C Apr 06 '21 at 18:38

0 Answers0