在 Struts 2 中使用 JSP 自定义调试拦截器

问题描述

我使用的是 Struts 2。

我想创建一个自定义拦截器调试类,以便在用户单击保存按钮时在拦截器调试模式浏览器中显示所有属性字段值。

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>

拦截器产品:

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

在我的jsp页面中使用拦截器类和调试模式浏览器点击保存按钮时,如何拦截我的类产品的所有归档值?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)