Getting a list of the repeated last form data when trying to fetch the list of objects from database

Akash Raveendran

My first jsp is email.jsp and from there it is directed to template.jsp but when I try to get the list of EmailMessages object, I get repeated entries of the last form submitted data when I must be getting all the objects the database. Can anyone please help me through this?

@RequestMapping(value = "/email", method = RequestMethod.GET)
public String addEmail() {
    return "email";
}

@RequestMapping(value = "/template", method = RequestMethod.POST)
public String getTemplateEmail(@ModelAttribute EmailMessage emailMessage, ModelMap model) {
    emailService.insertTemplate(emailMessage);
    List<EmailMessage> emailMessageList = new ArrayList<EmailMessage>();
    emailMessageList = emailService.fetchTemplate();
    model.addAttribute("emailMessageList", emailMessageList);
    return "template";
}

email.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Email body</title>
</head>
<body>
<form action="/TemplateEmail/template" method="POST">
<table>
    <tr>
        <td><b>Category </b>: </td>
        <td><select name="category">
            <option value=""> </option>
            <option value="normal">Normal</option>
            <option value="urgent">Urgent</option>
        </select></td>
        <td>
        <button type="button" style="width: 25px;height: 25px;border: solid 1px #000;border-radius: 25px" onclick="addCategory()">+</button>
        <script>
            function addCategory() {
                var category = prompt("Add new category", "");
            }
        </script>
        </td>
    </tr>
    <tr>
        <td><b>Subject </b>: </td>
        <td><input type="text" name="subject"/></td>
    </tr>
    <tr>
        <td><b>Body </b>: </td>
        <td><textarea name="body" rows="10" columns="50"></textarea>     </td>
    </tr>
    <tr>
        <td colspan="2" align="center">
        <input type="submit" value=" SAVE "/>
        </td>
    </tr>
</table>
</form>
</body>
</html>

EmailMessage.java

package com.qburst.templateemail.entity;

public class EmailMessage {

private String category;
private String subject;
private String body;
private Long id;

public String getCategory() {
    return category;
}

public void setCategory(String category) {
    this.category = category;
}

public String getSubject() {
    return subject;
}

public void setSubject(String subject) {
    this.subject = subject;
}

public String getBody() {
    return body;
}

public void setBody(String body) {
    this.body = body;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

}

template.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=UTF-8">
<title>Template Webpage</title>
</head>
<body>
<center>
    <table border="1">
      <tr>
        <th>select</th>
        <th>Category</th>
        <th>Subject</th>
        <th>Body</th>
      </tr>
      <c:forEach var="emailMessage" items="${emailMessageList}">
          <tr>
            <td><label for="${emailMessage.id}"></label><input type="radio" name="emailMsg" id="${emailMessage.id}"/></td>
            <td>${emailMessage.category}</td>
            <td>${emailMessage.subject}</td>
            <td>${emailMessage.body}</td>
          </tr>
      </c:forEach>
    </table><br>
    <input type="button" onclick="location.href='/TemplateEmail/email'" value="Add More" />
    <br><br><br>
    <input type="button" onclick="location.href='/TemplateEmail/send_email'" value="Send To" />
    </center>
  </body>
</html>

EmailDao.java This is the code for fetching the list from database.

public List<EmailMessage> fetchTemplate() {
    String query = "select * from email_message";
    Connection connection = null;
    List<EmailMessage> emailMessageList = new ArrayList<EmailMessage>();
    try {
        EmailMessage emailMessage = new EmailMessage();
        connection = dataSource.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(query);
        ResultSet resultSet = preparedStatement.executeQuery(query);
        while (resultSet.next()) {
            emailMessage.setCategory(resultSet.getString("category"));
            emailMessage.setSubject(resultSet.getString("subject"));
            emailMessage.setBody(resultSet.getString("body"));
            emailMessageList.add(emailMessage);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    return emailMessageList;
}
Alexey Nikitenko

Ok, try to create always new object at the loop:

EmailMessage emailMessage;
while (resultSet.next()) {
       emailMessage = new EmailMessage();
       emailMessage.setCategory(resultSet.getString("category"));
       emailMessage.setSubject(resultSet.getString("subject"));
       emailMessage.setBody(resultSet.getString("body"));
       emailMessageList.add(emailMessage);
}

You are added repeated link at object to list. You always change values at the all members of list, because it link to one object.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Trying to display data from database but getting repeated variable name instead

From Dev

Getting an UnknownHostException when trying to fetch data from flickr?

From Dev

Trying to fetch last month data from mysql

From Dev

I am trying to get a data from a list of objects

From Dev

I am trying to get a data from a list of objects

From Dev

Getting value from a list of objects

From Dev

Trying to fetch data from Nested MongoDB Database?

From Dev

Not getting the employee List from database

From Dev

Database Helper null object reference when trying to fetch the data from SQLite database , no such table: Offices (code 1)

From Dev

Creating list of Objects from Database

From Dev

Creating list of Objects from Database

From Dev

How to get data from a database into a list of objects and display it in a listview?

From Dev

How to remove repeated data from a list in angularjs?

From Dev

insert data to mysql database from multiple select list (html form)

From Dev

Complement method for .last when working with List objects?

From Dev

Why i'm getting exception when trying to get all images form hard disk to a List?

From Dev

Getting only last value from List<T>

From Java

How to return a list with the last item of the list repeated?

From Dev

R: Getting data from list

From Dev

R: Getting data from list

From Dev

Fetch data from mysql database using drop down list and then use search function to filter data from selected drop down list option

From Dev

How to get the repeated ip address from the list of objects using javascript?

From Dev

Getting empty result when trying to fetch the output of AVAssetExportSession from PHAsset

From Dev

Laravel trying to get navbar list from database

From Dev

Getting to the subclass from superclass objects list

From Dev

creating a drop down list when pulling data from a database

From Java

Getting the last element of a list

From Dev

Null Object Reference when trying to call a method from a list of objects extracted from sqlite

From Dev

Python gives me an error when trying to get anything other than first or last two elements from list

Related Related

  1. 1

    Trying to display data from database but getting repeated variable name instead

  2. 2

    Getting an UnknownHostException when trying to fetch data from flickr?

  3. 3

    Trying to fetch last month data from mysql

  4. 4

    I am trying to get a data from a list of objects

  5. 5

    I am trying to get a data from a list of objects

  6. 6

    Getting value from a list of objects

  7. 7

    Trying to fetch data from Nested MongoDB Database?

  8. 8

    Not getting the employee List from database

  9. 9

    Database Helper null object reference when trying to fetch the data from SQLite database , no such table: Offices (code 1)

  10. 10

    Creating list of Objects from Database

  11. 11

    Creating list of Objects from Database

  12. 12

    How to get data from a database into a list of objects and display it in a listview?

  13. 13

    How to remove repeated data from a list in angularjs?

  14. 14

    insert data to mysql database from multiple select list (html form)

  15. 15

    Complement method for .last when working with List objects?

  16. 16

    Why i'm getting exception when trying to get all images form hard disk to a List?

  17. 17

    Getting only last value from List<T>

  18. 18

    How to return a list with the last item of the list repeated?

  19. 19

    R: Getting data from list

  20. 20

    R: Getting data from list

  21. 21

    Fetch data from mysql database using drop down list and then use search function to filter data from selected drop down list option

  22. 22

    How to get the repeated ip address from the list of objects using javascript?

  23. 23

    Getting empty result when trying to fetch the output of AVAssetExportSession from PHAsset

  24. 24

    Laravel trying to get navbar list from database

  25. 25

    Getting to the subclass from superclass objects list

  26. 26

    creating a drop down list when pulling data from a database

  27. 27

    Getting the last element of a list

  28. 28

    Null Object Reference when trying to call a method from a list of objects extracted from sqlite

  29. 29

    Python gives me an error when trying to get anything other than first or last two elements from list

HotTag

Archive