How to loop over a list of object in JSTL - java

enter image description hereI have the following list which is coming from a class via toString method .
I am passing the list in modelAndView object to jsp page.
Now I want to loop over the list and make a table in jsp Page.
Please guide.
List<LocationStats> allStates = [LocationStats{state='Fujian', country='Afghanistan', latestTotalCases=51526}, LocationStats{state='Guangdong', country='Albania', latestTotalCases=59438}] ;
////////////////////////// LocationStats.JAVA ///////////////////////////////////////
public class LocationStats {
private String state;
private String country;
private int latestTotalCases;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public int getLatestTotalCases() {
return latestTotalCases;
}
public void setLatestTotalCases(int latestTotalCases) {
this.latestTotalCases = latestTotalCases;
}
#Override
public String toString() {
return "LocationStats{" +
"state='" + state + '\'' +
", country='" + country + '\'' +
", latestTotalCases=" + latestTotalCases +
'}';
}
}
///////////////////////////// HomeController.java ////////////////////////
#RequestMapping("/")
public ModelAndView home() {
ModelAndView mv = new ModelAndView();
mv.addObject("location", coronaVirusDataService.getAllStates());
mv.setViewName("home.jsp");
return (mv);
}
/////////////////// home.jsp //////////////////////
<table>
<tr>
<th>state</th>
<th>country</th>
<th>latestTotalCases</th>
</tr>
<tr th:each="elements : ${location}">
<td th:text="${elements.state}"></td>
<td th:text="${elements.country}"></td>
<td th:text="${elements.latestTotalCases}">0</td>
</tr>
</table>

You should add taglib at the beginning of jsp file.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Than you can write it in this way:
<table>
<tr>
<th>state</th>
<th>country</th>
<th>latestTotalCases</th>
</tr>
<c:forEach items="${location}" var="elements">
<tr>
<td>${elements.state}</td>
<td>${elements.country}</td>
<td>${elements.latestTotalCases}</td>
</tr>
</c:forEach>
</table>
You can also check if this 'location' is not empty, for example by printing it out:
<%=location%>

<table>
<tr>
<th>state</th>
<th>country</th>
<th>latestTotalCases</th>
</tr>
<c:forEach items="${location}" var="element">
<tr>
<td>${element.state}</td>
<td>${element.country}</td>
<td>${element.latestTotalCases}</td>
</tr>
</c:forEach>
</table>

Related

EL1008E: Property or field 'user_id' cannot be found on object of type 'java.util.ArrayList' - maybe not public or not valid?

I cant solve this problem, i need some help
this is the .html code
<tr data-th-each="user : ${Users}">
<td data-th-text="${Users.user_id}"></td>
<td data-th-text="${Users.user_name}"></td>
<td data-th-text="${Users.user_email}"></td>
<td data-th-text="${Users.user_telephone}"></td>
<td data-th-text="${Users.user_type}"></td>
<td data-th-text="${Users.user_available}"></td>
</tr>
</tbody>
</table>
</section>
<br>
<a class="btn btn-info" href="/newUser">Crear Usuario</a>
<a class="btn btn-info" href="/">Volver</a>
</body>
</html>
and this is the .java of user
public Users() {
super();
}
public Users(String user_id, String user_name, String user_email, String user_password, int user_telephone, String user_type, int user_available) {
this.user_id = user_id;
this.user_name = user_name;
this.user_email = user_email;
this.user_password = user_password;
this.user_telephone = user_telephone;
this.user_type = user_type;
this.user_available = user_available;
}
//Getters
public String getUserId() {
return user_id;
}
public String getUserName() {
return user_name;
}
public String getUserEmail() {
return user_email;
}
public String getUserPassword() {
return user_password;
}
public int getUserTelephone() {
return user_telephone;
}
public String getUserType() {
return user_type;
}
public int getUserAvailable() {
return user_available;
}
}
ok this is all the code of user.java
what should i do to solve this error
its for a sprint project
but why i cant send this, lol
Hi please check this example
<table>
<th:block th:each="user : ${users}">
<tr>
<td th:text="${user.login}"></td>
<td th:text="${user.name}"></td>
</tr>
</th:block>
</table>

Expression language can't access getter methods

I have the following class users.java:
package org.XY;
public class user {
private String mail;
private String name;
private String org;
private String pw;
private String admin;
public user (String mail,String name,String org, String pw, String admin){
this.mail=mail;
this.name=name;
this.org=org;
this.pw = pw;
this.admin = admin;
}
public String getMail() {
return this.mail;
}
public String getName() {
return this.name;
}
public String getOrg() {
return this.org;
}
public String getPw() {
return this.pw;
}
public String getAdmin() {
return this.admin;
}
public String getUser() {
String u = this.mail + " | " + this.name + " | " + this.org + " | " + this.pw + " | Is admin:" + this.admin;
return u;
}
}
And the following jsp page:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# page import="org.XY" %>
<%# page import="java.util.List" %>
<%#page isELIgnored="false"%>
<html>
<head>
<title>myservlet</title>
</head>
<body>
<%--<c:forEach items="${users}" var="user">
<tr>
<td><c:out value="${user.getMail()}" /></td>
<td><c:out value="${user.getName()}" /></td>
<td><c:out value="${user.getOrg()}" /></td>
<td><c:out value="${user.getPw()}" /></td>
<td><c:out value="${user.getAdmin()}" /></td>
</tr>
</c:forEach>--%>
<%
List<user> users = (List<user>) request.getAttribute("users");
for (user user : users) {
%>
${user.mail}
<%
}
%>
<%
for (user user : users) {
out.println("<li>" + user.getPw());
}
%>
...
I want to create a table which contains all the data from the users.
As you can see, I tried using the ${user.mail} method with el.
Both give me an javax.el.PropertyNotFoundException: No public static field named [mail] was found on class [org.isse.sopro.user] error and an java.lang.NoSuchFieldException: mail error.
I already tried renaming the getter methods but it did not solv the issue.
Could someone tell me what I am diong wrong?
Regards
Andi
UPDATE:
Obviously it wasn't about the program itself, it was about maven. After a complete reload from Intellij, maven reloaded the project and everything worked fine.
You can not use ${user.mail} because of did not implements Serializable
You need change user class implements Serializable
public class user implements Serializable{
}
And you can use ${user.mail} instead of ${user.getMail()}
<c:forEach items="${users}" var="user">
<tr>
<td><c:out value="${user.mail}" /></td>
</tr>
</c:forEach>
Try this
<%--<c:forEach items="${users}" var="user">
<tr>
<td><c:out value="${user.mail}" /></td>
<td><c:out value="${user.name}" /></td>
<td><c:out value="${user.org}" /></td>
<td><c:out value="${user.pw}" /></td>
<td><c:out value="${user.admin}" /></td>
</tr>
</c:forEach>--%>

Trying to create and save list of objects in spring mvc

This is my model class
public class DynamicRow {
private String id;
private String name;
private String email;
public DynamicRow(String id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
public DynamicRow() {
// TODO Auto-generated constructor stub
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return "DynamicRow [id=" + id + ", name=" + name + ", email=" + email
+ "]";
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
This is the form class
public class DynamicRowForm {
private List<DynamicRow> dynamicRow = LazyList.decorate(new ArrayList<DynamicRow>(), FactoryUtils.instantiateFactory(DynamicRow.class));
public DynamicRowForm() {
}
public List<DynamicRow> getDynamicRow() {
return dynamicRow;
}
public void setDynamicRow(List<DynamicRow> dynamicRow) {
this.dynamicRow = dynamicRow;
}
}
Below is my controllers
#RequestMapping(value="/createDetails")
public String list(Model model){
DynamicRowForm dynamicRowForm = new DynamicRowForm();
model.addAttribute("DynamicRowForm",dynamicRowForm);
return "test2";
}
#RequestMapping(value="/save")
public String showList(Model model,#ModelAttribute("DynamicRowForm") DynamicRowForm dynamicRowForm) {
System.out.println(dynamicRowForm.getDynamicRow());
return "success";
}
And this is my jsp page named as test2
<%#taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
var rowCount = 1;
function addMoreRows(form) {
rowCount ++;
var recRow = '<p id="rowCount'+rowCount+'"><tr><td><input name="" type="text" size="17%" maxlength="120" /></td><td><input name="" type="text" maxlength="120" style="margin: 4px 5px 0 5px;"/></td><td><input name="" type="text" maxlength="120" style="margin: 4px 10px 0 0px;"/></td></tr> Delete</p>';
$('#addedRows').append(recRow);
}
function removeRow(removeNum) {
$('#rowCount'+removeNum).remove();
}
</script>
</head>
<body>
<form:form action="save" method="GET" modelAttribute="DynamicRowForm">
<input type="button" onclick="addMoreRows(this.form);" value="AddRow">
<table rules="all" style="background:#fff;">
<tr>
<td style="font-size:14px;" >Name</td>
<td style="font-size:14px;">Email</td>
<td style="font-size:14px;">Mobile</td>
<!-- <td><span style="font:normal 12px agency, arial; color:blue; text-decoration:underline; cursor:pointer;" onclick="addMoreRows(this.form);">
Add More
</span>
</td> -->
</tr>
<tr id="rowId">
<td><form:input path="${dynamicRow.id}" type="" size="17%"/></td>
<td><form:input path="${dynamicRow.name}" type="text" /></td>
<td><form:input path="${dynamicRow.email}" type="text" /></td>
</table>
<div id="addedRows"></div>
<input type="submit" value="Save">
</form:form>
</body>
I know these questions have been asked before, but as am very new to spring mvc and learning things so am trying to get some comfort in this technology..
Anyway, what here I am trying to do is to save multiple row/list of object... but its not getting saved in the list... Please help me out on what is the mistake am doing, and correct me.
Edit
And one more thing I would like to clarify, in my first controller am creating a dynamicRowForm object and adding into model so that my jsp page can access it...and in my second controller am receiving DynamicRowForm object using #ModelAttribute with same name..but here am getting different object.why?
Your inputs need to look like this:
<form:form action="save" method="POST" modelAttribute="DynamicRowForm">
...
<td><form:input path="dynamicRow[0].id" type="text" /></td>
<td><form:input path="dynamicRow[0].name" type="text" /></td>
<td><form:input path="dynamicRow[0].email" type="text" /></td>
...
</form:form>
Which essentially states: on form submission bind the id field to the DynamicRow at index 0 in the form backing Object - DynamicRowForm.
If you do this, then the following should print the values input:
#RequestMapping(value="/save")
public String showList(#ModelAttribute DynamicRowForm dynamicRowForm) {
System.out.println(dynamicRowForm.getDynamicRow().get(0).getId());
System.out.println(dynamicRowForm.getDynamicRow().get(0).getName());
System.out.println(dynamicRowForm.getDynamicRow().get(0).getEmail());
return "success";
}
And one more thing I would like to clarify, in my first controller am
creating a dynamicRowForm object and adding into model so that my jsp
page can access it...and in my second controller am receiving
DynamicRowForm object using #ModelAttribute with same name..but here
am getting different object.why?
Because it is stateless. If you want to work with the same instance you will need to store it in the session between requests. See the following useful discussion.
http://www.intertech.com/Blog/understanding-spring-mvc-model-and-session-attributes/
You are missing indexing here as you are using collection. Try...
<form:input path="${DynamicRowForm.dynamicRow[0].id}" type="" size="17%"/>
and also in your addRow(), you have to use the dynamic index with row count. If it is not working with form:input, try simple html input type.
var count=0;
function addMoreRows(form) {
rowCount ++;
count =++count;
var recRow = '<p id="rowCount'+rowCount+'"><tr><td><input path="users['+count+'].id" id="users'+count+'.id" name="users['+count+'].id" type="" size="17%" maxlength="120" /></td><td><input path="users['+count+'].name" id="users'+count+'.name" name="users['+count+'].name" type="text" maxlength="120" style="margin: 4px 5px 0 5px;"/></td><td><input path="users['+count+'].email" id="users'+count+'.email" name="users['+count+'].email" type="text" maxlength="120" style="margin: 4px 10px 0 0px;"/></td></tr> Delete</p>';
$('#addedRows').append(recRow);
}

Servelet set attribute doesn't print in jsp

I have set a List of book and set to the request.setAttribute("booksa", allbooks); and in jsp i try to print the List in table but no values printed only the empty table.
This is my Servelet
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userPath = request.getServletPath();
if (userPath.equals("/index")) {
// TODO: Implement category request
userPath = "/index";
}
else if (userPath.equals("/books"))
{
List<Book> allbooks = bookFacade.findAll();
userPath = "/books";
request.setAttribute("booksa", allbooks);
//System.out.print(allbooks);
}
else
{
}
String url = userPath + ".jsp";
try {
request.getRequestDispatcher(url).forward(request, response);
} catch (Exception ex) {
ex.printStackTrace();
}
}
This is my Book.jsp page
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<table border="1">
<!-- column headers -->
<tr>
<td>ISBN</td>
<td>TITLE</td>
<td>PRICE</td>
<td>YEARS</td>
<td>LANGUAGE</td>
</tr>
<!-- column data -->
<c:forEach var="vehicle" items="${booksa}">
<tr>
<td><c:out value="${vehicle.isbn}" /></td>
<td><c:out value="${vehicle.title}" /></td>
<td><c:out value="${vehicle.price}" /></td>
<td><c:out value="${vehicle.years}" /></td>
<td><c:out value="${vehicle.languages}" /></td>
</tr>
</c:forEach>
</table>
</body>
But when i redirect to this page the view page source show like this
<!-- column data -->
<c:forEach var="vehicle" items="[ejb.Book[ isbn=SR001 ]]">
<tr>
<td><c:out value="" /></td>
<td><c:out value="" /></td>
<td><c:out value="" /></td>
<td><c:out value="" /></td>
<td><c:out value="" /></td>
</tr>
</c:forEach>
My Book class
public Book() {
}
public Book(String isbn) {
this.isbn = isbn;
}
public Book(String isbn, String title, double price, int years, String languages) {
this.isbn = isbn;
this.title = title;
this.price = price;
this.years = years;
this.languages = languages;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getYears() {
return years;
}
public void setYears(int years) {
this.years = years;
}
public String getLanguages() {
return languages;
}
public void setLanguages(String languages) {
this.languages = languages;
}
#XmlTransient
public Collection<Author> getAuthorCollection() {
return authorCollection;
}
public void setAuthorCollection(Collection<Author> authorCollection) {
this.authorCollection = authorCollection;
}
#Override
public int hashCode() {
int hash = 0;
hash += (isbn != null ? isbn.hashCode() : 0);
return hash;
}
#Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Book)) {
return false;
}
Book other = (Book) object;
if ((this.isbn == null && other.isbn != null) || (this.isbn != null && !this.isbn.equals(other.isbn))) {
return false;
}
return true;
}
#Override
public String toString() {
return "ejb.Book[ isbn=" + isbn + " ]";
}
}
values are coming to page but i don't know how to print please give me a help
Include the following line in your JSP, in the beginning. That should solve your issue
<%# taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
If you not added the jstl to your build path , just add from the references here ,
1.Adding jstl support
2.Getting JSTL to run within Tomcat and Eclipse
hope this helps!!

I want to add "Edit" functionality. When I click Edit, it adds a duplicate record instead of editing

This is my Operations.java, products.Java class and Products.jsp file:
In which I implemented the Add and Edit functionality. The add button is working fine but the edit button is adding a duplicate record instead of editing the record. Please tell me how do I implement edit function in my program ?
package metier;
import java.util.ArrayList;
public class Operation {
private ArrayList<Produit> produits = new ArrayList<Produit>();
public ArrayList<Produit> getProduits() {
return produits;
}
public void setProduits(ArrayList<Produit> produits) {
this.produits = produits;
}
public void add(Produit p){
produits.add(p);
}
public void remove(Long id){
for(Produit p:produits){
if(p.getId()==id){ //equals
produits.remove(p);
break;
}
}
}
public void edit(Long id){
for(Produit p:produits){
if(p.getId()==id){
p.getId();
p.getName();
p.getContactno();
p.getSPS();
produits.add(p);
break;
}
}
}
public ArrayList getAll(){
return produits;
}
}
package metier;
public class Produit {
private Long id;
private String name, address, contactNo, SparePartsService;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getContactno() {
return contactNo;
}
public void setContactno(String contactNo) {
this.contactNo = contactNo;
}
public String getSPS() {
return SparePartsService;
}
public void setSPS(String SparePartsService) {
this.SparePartsService = SparePartsService;
}
public Produit() {
super();
// TODO Auto-generated constructor stub
}
public Produit(String name, String address, String contactNo, String SparePartsService) {
super();
this.name = name;
this.address = address;
this.contactNo = contactNo;
this.SparePartsService = SparePartsService;
}
public Produit(Long id, String name, String address, String contactNo, String SparePartsService) {
super();
this.id = id;
this.name = name;
this.address = address;
this.contactNo = contactNo;
this.SparePartsService = SparePartsService;
}
#Override
public String toString() {
return id + " - " + name + " - " + address + " - " + contactNo + " - " + SparePartsService + " .";
}
public void Show(){
System.out.println(toString());
}
}
<%# page import="web.ProduitBeans"%>
<%# page import="metier.Produit"%>
<%# page import="java.util.Iterator"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Getiteasy.Net</title>
</head>
<body>
<%
ProduitBeans produits;
if(request.getAttribute("modele") != null){
produits = (ProduitBeans) request.getAttribute("modele");
}else{
produits = new ProduitBeans();
}
%>
<h3>Tutorial MVC(Model, View, Controller)</h3>
<h5>Ajouter un nouveau produit</h5>
<form action="prodserv" method="post">
<table border="1" width="45%">
<tr>
<td>Name</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" name="address"></td>
</tr>
<tr>
<td>Contact No.</td>
<td><input type="text" name="contactNo"></td>
</tr>
<tr>
<td>Spare Parts Service(Yes/No)</td>
<td><input type="text" name="SparePartsService"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Valider"></td>
</tr>
</table>
</form>
<table border="1" width="60%">
<tr>
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Contact No.</th>
<th>Spare Parts Service</th>
<th>Option</th>
</tr>
<%
Iterator<Produit> list = produits.getListe().iterator();
while(list.hasNext()){
Produit p = list.next();
%>
<tr>
<td><%=p.getId() %></td>
<td><%=p.getName() %></td>
<td><%=p.getAddress() %></td>
<td><%=p.getContactno() %></td>
<td><%=p.getSPS() %></td>
<td>
<form action="prodserv" method="post">
<input type="hidden" name="id" value="<%=p.getId() %>" >
<input type="hidden" name="action" value="supprimer" >
<input type="submit" value="supprimer"/>
</form>
<form action="prodserv" method="post">
<input type="hidden" name="id" value="<%=p.getId() %>" >
<input type="hidden" name="editaction" value="Edit" >
<input type="submit" value="Edit"/>
</form>
</td>
</tr>
<%
}
%>
</table>
What do you want your edit method to do? Look at your edit method. If you find a product with the ID you are just calling the getter methods and you are doing nothing with the data. After that you are just adding the same object again to your list. You need to define what data you want to edit and then overwrite the data with a setter method. And don't add the object again to your list.
Working with an ArrayList for this purpose is very unefficiant, you will be looping your list many many times which will take longer and longer as your list is growing. Better make use of a Map, it "maps" a key to a value so you can call that value directly by the predefined key and don't have to loop the whole list.
In your Operation class:
change the ArrayList<Produit> produits ... to Map<Long,Produit> = new HashMap<>()
(Also I would recommand you making it static, to ensure the you keep the same list across the board)
Add a private static Long idCounter=0L
Then when you Add a new Produit, do:
public void add(Produit p){
idCounter++;
p.setId(idCounter);
produits.put(idCounter,p);
}
Remove:
public void remove(Produit p){
produits.remove(p.getId());
}
Note.
No idcounter--; here because you don't know if it is the last one in your map, So baicly you will be skipping some id's if you removed alot of elements.
The id of the Produit that you want to remove has to be set on the p parameter.
Edit:
public void edit(Produit p){
produits.put(p.getId(),p);
}
Note.
The ID of the Produit that you want to remove has to be set on the p parameter.
getAll:
public Collection<Produit> getAll(){
produits.values();
}
In your JSP page:
You have to somehow tell your controller what action it has to do. You can do this by using an Action parameter.
You have done this for one case (Delete (=supprimer FR)).
<input type="hidden" name="action" value="supprimer" >
To edit you must do it again, so change:
<input type="hidden" name="editaction" value="Edit" >
to
<input type="hidden" name="action" value="Edit" >
Note. "name"-tag is the name of the parameter like number is the name of the variable Int number.
In your Controller (servlet):
your:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getParameter("action") != null) {
op.remove(Long.parseLong(req.getParameter("id")));
}
else { // Recuprer les information
String name = req.getParameter("name");
String address = req.getParameter("address");
String contactNo = req.getParameter("contactNo");
String SparePartsService = req.getParameter("SparePartsService");
op.add(new Produit(1L, name, address, contactNo,SparePartsService));
}
In the "else"-clause of you if-satement that checks if action-parameter is null, you only have the functionality to add a Produit.
you must add:
if(req.getParameter("action").equals("Add")){
String name = req.getParameter("name");
String address = req.getParameter("address");
String contactNo = req.getParameter("contactNo");
String SparePartsService = req.getParameter("SparePartsService");
op.add(new Produit(1L, name, address, contactNo,SparePartsService));
}else if (req.getParameter("action").equals("Edit")){
String name = req.getParameter("name");
String address = req.getParameter("address");
String contactNo = req.getParameter("contactNo");
String SparePartsService = req.getParameter("SparePartsService");
Long id = Long.ParseLong(req.getParameter("id"));
op.edit(new Produit(id, name, address, contactNo,SparePartsService));
}
Result:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getParameter("action") != null) {
op.remove(Long.parseLong(req.getParameter("id")));
}
// Recuprer les information
else if(req.getParameter("action").equals("Add")){
String name = req.getParameter("name");
String address = req.getParameter("address");
String contactNo = req.getParameter("contactNo");
String SparePartsService = req.getParameter("SparePartsService");
op.add(new Produit(1L, name, address, contactNo,SparePartsService));
}else if (req.getParameter("action").equals("Edit")){
String name = req.getParameter("name");
String address = req.getParameter("address");
String contactNo = req.getParameter("contactNo");
String SparePartsService = req.getParameter("SparePartsService");
Long id = Long.ParseLong(req.getParameter("id"));
op.edit(new Produit(id, name, address, contactNo,SparePartsService));
}
}
Note. You must add a new "else if"-clause for every possible action-parameter value that you want to implement.
I advise you to put the functionality that is within these if-clauses in a seperate private function, if not your doPost(...) function will get very long.
Or even better take a look at the Command-patern.

Categories

Resources