Fixing more bugs in Java Spring Boot project!
Tags: opensource, java, springboot, spring
Published: 2023-11-29 04:42:25

---
series: Contributing to Java Spring Boot project
---

Following my last [blog post](https://dev.to/katiel/contributing-to-a-larger-open-source-project-2618), I am continuing my open source contributions on [E-commerce-project-springBoot](https://github.com/jaygajera17/E-commerce-project-springBoot). This project is an e-commerce webapp which uses Java [Spring Boot](https://spring.io/projects/spring-boot) and [MySQL](https://www.mysql.com/).

This week I have raised a new issue and created a PR with the fix. I discovered this issue over a week ago, however I was not able to find a fix right away. This week I managed to figure out the cause of the issue. Thankfully, I have worked enough with this project to understand the project structure and Spring Boot objects and functions, because I do not think I would have been able to debug this if it were my first look into this project. I'm finding it very beneficial to contribute to a project continuously rather than jumping from project to project with every single pull request.

## The Issue: [Update button on products page does not work](https://github.com/jaygajera17/E-commerce-project-springBoot/issues/48)

![Step1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/850c3edls6s90fmoi442.png)

![Step2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lddnxwln056ot71z7taa.png)

## Cause of the Issue:

- when the update button in products page is clicked, it triggers `updateproduct()` in [`AdminController.java`](https://github.com/jaygajera17/E-commerce-project-springBoot/blob/master2/JtProject/src/main/java/com/jtspringproject/JtSpringProject/controller/AdminController.java)
  - updateproduct() creates a new "productsUpdate" ModelAndView and **adds a product object** and list of category objects to the ModelAndView
  - the resulting ModelAndView is displayed to the user

![updateproduct](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ixlvwvxrzeo2dq6kdv2.png)

- the ModelAndView can be found in [`productsUpdate.jsp`](https://github.com/jaygajera17/E-commerce-project-springBoot/blob/master2/JtProject/src/main/webapp/views/productsUpdate.jsp)
  - the code `<c:forEach var="product" items="products">` is causing the error to be thrown since it should be items="${products}" and not a string

![forEach code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jsdy94s5qwnpu2ry8gw6.png)

![error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t5x9j0pj1i3ax3rg4vq8.png)

  - there is no need for the forEach loop since we never add a list of products to the ModelAndView, only **one product object**
    - keeping the forEach loop results in blank page, since we never pass in a list of products

![blank page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1xa4si3julc7apr5as8b.png)

## Fix:

- in [`productsUpdate.jsp`](https://github.com/jaygajera17/E-commerce-project-springBoot/pull/49/files#diff-609a341bed98cb13e21bf76f500a54a2920d522cf0545cf7ad3d58a9012f42af), removed the forEach loop completely, since we add one product to the view and not a list of products

## Fix Tested:

![fix1](https://github.com/jaygajera17/E-commerce-project-springBoot/assets/98062538/c3a0358f-7ad7-434f-8c02-b276f5a74f4c)

![fix2](https://github.com/jaygajera17/E-commerce-project-springBoot/assets/98062538/85f9df9b-3852-4cad-bef4-5c544bbd247e)

## PR: [Fixed Update button in Products page](https://github.com/jaygajera17/E-commerce-project-springBoot/pull/49)

---

# What I Learned

- How to debug Java programs using [IntelliJ](https://www.jetbrains.com/idea/) and breakpoints
  - Used this to determine that the exception was thrown while rendering the ModelAndView `productsUpdate.jsp` and not the `updateproduct()` function
- How to read Java server logs to troubleshoot exceptions
  - Used this to figure out that in `productsUpdate.jsp` product was a string even though we were expecting a list
- Gained more in-depth knowledge about [Java Spring Framework](https://spring.io/projects/spring-framework) by reading official documentation
   - The [ModelAndView](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/ModelAndView.html) class holds both Model and View in the web MVC framework.
  - Using the [addObject](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/ModelAndView.html#addObject(java.lang.Object)) we can add an attribute to the model 

Even though the update button now displayed the correct page with the correct data to be updated, there is still a bug when trying to finish the information update by clicking Update Details in that page. I plan to work on this in a future PR.