diff --git a/spring-mvc-java/src/main/java/org/baeldung/model/Message.java b/spring-mvc-java/src/main/java/org/baeldung/model/Message.java index 9a1350996f..051fa0d880 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/model/Message.java +++ b/spring-mvc-java/src/main/java/org/baeldung/model/Message.java @@ -2,9 +2,14 @@ package org.baeldung.model; public class Message { + private String from; private String text; public String getText() { return text; } + + public String getFrom() { + return from; + } } diff --git a/spring-mvc-java/src/main/java/org/baeldung/model/OutputMessage.java b/spring-mvc-java/src/main/java/org/baeldung/model/OutputMessage.java index 4bee5e9e4c..53a7081f2c 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/model/OutputMessage.java +++ b/spring-mvc-java/src/main/java/org/baeldung/model/OutputMessage.java @@ -2,10 +2,13 @@ package org.baeldung.model; public class OutputMessage { + private String from; private String text; private String time; - public OutputMessage(final String text, final String time) { + public OutputMessage(final String from, final String text, final String time) { + + this.from = from; this.text = text; this.time = time; } @@ -17,4 +20,8 @@ public class OutputMessage { public String getTime() { return time; } + + public String getFrom() { + return from; + } } diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/controller/ChatController.java b/spring-mvc-java/src/main/java/org/baeldung/web/controller/ChatController.java index 5dcbe77cd5..cc39695dc8 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/web/controller/ChatController.java +++ b/spring-mvc-java/src/main/java/org/baeldung/web/controller/ChatController.java @@ -17,7 +17,7 @@ public class ChatController { public OutputMessage send(final Message message) throws Exception { final String time = new SimpleDateFormat("HH:mm").format(new Date()); - return new OutputMessage(message.getText(), time); + return new OutputMessage(message.getFrom(), message.getText(), time); } } diff --git a/spring-mvc-java/src/main/webapp/resources/chat.html b/spring-mvc-java/src/main/webapp/resources/chat.html index 5704ac7b58..92a54534fd 100644 --- a/spring-mvc-java/src/main/webapp/resources/chat.html +++ b/spring-mvc-java/src/main/webapp/resources/chat.html @@ -45,8 +45,9 @@ function sendMessage() { + var from = document.getElementById('from').value; var text = document.getElementById('text').value; - stompClient.send("/app/chat", {}, JSON.stringify({ 'text': text })); + stompClient.send("/app/chat", {}, JSON.stringify({'from':from, 'text':text})); } function showMessageOutput(messageOutput) { @@ -54,7 +55,7 @@ var response = document.getElementById('response'); var p = document.createElement('p'); p.style.wordWrap = 'break-word'; - p.appendChild(document.createTextNode(messageOutput.time + ": " + messageOutput.text)); + p.appendChild(document.createTextNode(messageOutput.from + ": " + messageOutput.text + " (" + messageOutput.time + ")")); response.appendChild(p); } @@ -65,6 +66,12 @@