It looks like basical, but I am new with Struts2.
I want to send a new product created with name, price and category.
I work with struts2 version 2.5.26, Maven, tomcat 9.0, java jdk 1.8.0_241
It seams that the code I use to send the product is wrong, and Action
defined in struts.xml
can't get it.
My JSP file to send the product :
<%@ page pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Nouveau produit</title>
</head>
<body>
<h1>Ajouter un nouveau produit</h1><br><br>
<form action="newProduit.action" method="post">
<div >
<s:textfield name="produit.nom" label="Nom " />
</div><br>
<div>
<s:textfield name="produit.prix" class="formH len150" label="Prix " />
</div><br>
<div style="position:relative;">
<label for="cat" class="formH len150">Catégorie :</label>
<select id="cat" name="produit.categorie" class="formH len150">
<option value="">--Choisir--</option>
<s:iterator value="categories" var="c">
<option value=" ${c.id } " > ${c.nom } </option>
</s:iterator>
</select>
</div><br>
<div >
<s:submit class="btn btn-vert" value="Envoyer" />
</div><br>
<div >
<s:a href="listeProduits.action" class="btn" >Retour liste produits</s:a>
</div>
</form>
</body>
</html>
The struts.xml
file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="actionsDiverses" extends="struts-default,json-default" namespace="/actions" >
<!-- other actions -- >
<action name="newProduit" method="nvProduit" class="actions.ProduitsAction">
<result name="success" type="redirect">listeProduits</result>
</action>
</package>
</struts>
The ProduitsAction.java
file :
package actions;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import entities.Produit;
import services.IMetiersBoutiqueFactory;
import services.MetiersBoutiqueFactory;
public class ProduitsAction extends ActionSupport implements ModelDriven{
private static final long serialVersionUID = 1L;
private IMetiersBoutiqueFactory services = new MetiersBoutiqueFactory();
private Produit produit;
public String nvProduit() {
try {
services.ajouterProduit(produit);
}catch (Exception e) {
e.getStackTrace();
}
return SUCCESS;
}
// other methods, properties, getters and setters
public Produit getProduit() {
return produit;
}
public void setProduit(Produit produit) {
this.produit = produit;
}
@Override
public Produit getModel() {
return produit;
}
}
The error sent is :
com.opensymphony.xwork2.config.ConfigurationException: No result defined for action actions.ProduitsAction and result input
I tried some other solutions but the best I get is a product with nom=null, prix=null, categorie=null
.
I guess my problem is in these files. I may send you the other files or methods
The entity Produit
:
package entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "produit")
public class Produit {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String nom;
private Double prix;
@ManyToOne
@JoinColumn(name="id_categorie", nullable=true)
private Categorie categorie;
public Produit(Long id, String nom, Double prix, Categorie categorie) {
super();
this.id = id;
this.nom = nom;
this.prix = prix;
this.categorie = categorie;
}
public Produit() {}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public Double getPrix() {
return prix;
}
public void setPrix(Double prix) {
this.prix = prix;
}
public Categorie getCategorie() {
return categorie;
}
public void setCategorie(Categorie categorie) {
this.categorie = categorie;
}
}