Spring, Java, HTML: How can I populate an HTML form with values?












0















I want to write a part of a website that lets the user alter the data of a pre existing book. For that, I am trying to use a form which works fine. I just can't figure out how to get the form to display data in the editing fields so the user doesn't have to enter everything again but can simply change some details. My HTML code looks like this:



<form method="post" role="form" class="ui form" id="bookForm" th:action="@{/editBook}" th:object="${bookForm}">
<div class="field">
<label for="name">Book</label>
<input id="name" name="name" th:field="*{name}" th:errorclass="fieldError" type="text" required="required"/><br/>
</div>


I include some more code about errors and other things but this is basically where I want the form not only to pass values to my java file but also to take values about the book and display them in the editing fields.
I think I need to pass the book that the user wants to edit into this form but I'm not sure how. I have tried:



<input type="hidden" id="currentBook" name="currentBook" th:value="${currentBook}"/>


right before the "div" statement and then passing the "currentBook" into HTML with



model.addAttribute("currentBook", currentBook); 


in my @GetMapping method of that website. I then changed the "input" statement in my field as well to



<input id="name" name="name" th:field="*{name}" th:value="${currentBook.name}" th:errorclass="fieldError" type="text" required="required"/><br/>


currentBook.name will give me the name of that book just not within this context. Does anyone know what I'm doing wrong and how it will work?



Thank you in advance!










share|improve this question























  • You'll have to create Spring MVC controller method with Model injected as param and target view name return value. Then you can edit it in model.addAttribute("currentBook", currentBook) way and access the attribute later inside the Thymeleaf view. Spring MVC

    – WildDev
    Nov 24 '18 at 9:46













  • Sorry, I don't quite get what you mean. I have a controller in which I'm using this statement. It looks like this: @GetMapping("/books") String edits(Model model, BookForm bookForm) { ... Is that what you mean?

    – Corni
    Nov 24 '18 at 10:11













  • Yes, but param order matters. It should to be .edits(BookForm form, Model model). For binding the form @ModelAttribute annotation may be also required

    – WildDev
    Nov 24 '18 at 10:14













  • I tried that, also with the @ModelAttribute but it still doesn't work

    – Corni
    Nov 24 '18 at 10:43
















0















I want to write a part of a website that lets the user alter the data of a pre existing book. For that, I am trying to use a form which works fine. I just can't figure out how to get the form to display data in the editing fields so the user doesn't have to enter everything again but can simply change some details. My HTML code looks like this:



<form method="post" role="form" class="ui form" id="bookForm" th:action="@{/editBook}" th:object="${bookForm}">
<div class="field">
<label for="name">Book</label>
<input id="name" name="name" th:field="*{name}" th:errorclass="fieldError" type="text" required="required"/><br/>
</div>


I include some more code about errors and other things but this is basically where I want the form not only to pass values to my java file but also to take values about the book and display them in the editing fields.
I think I need to pass the book that the user wants to edit into this form but I'm not sure how. I have tried:



<input type="hidden" id="currentBook" name="currentBook" th:value="${currentBook}"/>


right before the "div" statement and then passing the "currentBook" into HTML with



model.addAttribute("currentBook", currentBook); 


in my @GetMapping method of that website. I then changed the "input" statement in my field as well to



<input id="name" name="name" th:field="*{name}" th:value="${currentBook.name}" th:errorclass="fieldError" type="text" required="required"/><br/>


currentBook.name will give me the name of that book just not within this context. Does anyone know what I'm doing wrong and how it will work?



Thank you in advance!










share|improve this question























  • You'll have to create Spring MVC controller method with Model injected as param and target view name return value. Then you can edit it in model.addAttribute("currentBook", currentBook) way and access the attribute later inside the Thymeleaf view. Spring MVC

    – WildDev
    Nov 24 '18 at 9:46













  • Sorry, I don't quite get what you mean. I have a controller in which I'm using this statement. It looks like this: @GetMapping("/books") String edits(Model model, BookForm bookForm) { ... Is that what you mean?

    – Corni
    Nov 24 '18 at 10:11













  • Yes, but param order matters. It should to be .edits(BookForm form, Model model). For binding the form @ModelAttribute annotation may be also required

    – WildDev
    Nov 24 '18 at 10:14













  • I tried that, also with the @ModelAttribute but it still doesn't work

    – Corni
    Nov 24 '18 at 10:43














0












0








0








I want to write a part of a website that lets the user alter the data of a pre existing book. For that, I am trying to use a form which works fine. I just can't figure out how to get the form to display data in the editing fields so the user doesn't have to enter everything again but can simply change some details. My HTML code looks like this:



<form method="post" role="form" class="ui form" id="bookForm" th:action="@{/editBook}" th:object="${bookForm}">
<div class="field">
<label for="name">Book</label>
<input id="name" name="name" th:field="*{name}" th:errorclass="fieldError" type="text" required="required"/><br/>
</div>


I include some more code about errors and other things but this is basically where I want the form not only to pass values to my java file but also to take values about the book and display them in the editing fields.
I think I need to pass the book that the user wants to edit into this form but I'm not sure how. I have tried:



<input type="hidden" id="currentBook" name="currentBook" th:value="${currentBook}"/>


right before the "div" statement and then passing the "currentBook" into HTML with



model.addAttribute("currentBook", currentBook); 


in my @GetMapping method of that website. I then changed the "input" statement in my field as well to



<input id="name" name="name" th:field="*{name}" th:value="${currentBook.name}" th:errorclass="fieldError" type="text" required="required"/><br/>


currentBook.name will give me the name of that book just not within this context. Does anyone know what I'm doing wrong and how it will work?



Thank you in advance!










share|improve this question














I want to write a part of a website that lets the user alter the data of a pre existing book. For that, I am trying to use a form which works fine. I just can't figure out how to get the form to display data in the editing fields so the user doesn't have to enter everything again but can simply change some details. My HTML code looks like this:



<form method="post" role="form" class="ui form" id="bookForm" th:action="@{/editBook}" th:object="${bookForm}">
<div class="field">
<label for="name">Book</label>
<input id="name" name="name" th:field="*{name}" th:errorclass="fieldError" type="text" required="required"/><br/>
</div>


I include some more code about errors and other things but this is basically where I want the form not only to pass values to my java file but also to take values about the book and display them in the editing fields.
I think I need to pass the book that the user wants to edit into this form but I'm not sure how. I have tried:



<input type="hidden" id="currentBook" name="currentBook" th:value="${currentBook}"/>


right before the "div" statement and then passing the "currentBook" into HTML with



model.addAttribute("currentBook", currentBook); 


in my @GetMapping method of that website. I then changed the "input" statement in my field as well to



<input id="name" name="name" th:field="*{name}" th:value="${currentBook.name}" th:errorclass="fieldError" type="text" required="required"/><br/>


currentBook.name will give me the name of that book just not within this context. Does anyone know what I'm doing wrong and how it will work?



Thank you in advance!







java html spring forms






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 8:40









CorniCorni

11




11













  • You'll have to create Spring MVC controller method with Model injected as param and target view name return value. Then you can edit it in model.addAttribute("currentBook", currentBook) way and access the attribute later inside the Thymeleaf view. Spring MVC

    – WildDev
    Nov 24 '18 at 9:46













  • Sorry, I don't quite get what you mean. I have a controller in which I'm using this statement. It looks like this: @GetMapping("/books") String edits(Model model, BookForm bookForm) { ... Is that what you mean?

    – Corni
    Nov 24 '18 at 10:11













  • Yes, but param order matters. It should to be .edits(BookForm form, Model model). For binding the form @ModelAttribute annotation may be also required

    – WildDev
    Nov 24 '18 at 10:14













  • I tried that, also with the @ModelAttribute but it still doesn't work

    – Corni
    Nov 24 '18 at 10:43



















  • You'll have to create Spring MVC controller method with Model injected as param and target view name return value. Then you can edit it in model.addAttribute("currentBook", currentBook) way and access the attribute later inside the Thymeleaf view. Spring MVC

    – WildDev
    Nov 24 '18 at 9:46













  • Sorry, I don't quite get what you mean. I have a controller in which I'm using this statement. It looks like this: @GetMapping("/books") String edits(Model model, BookForm bookForm) { ... Is that what you mean?

    – Corni
    Nov 24 '18 at 10:11













  • Yes, but param order matters. It should to be .edits(BookForm form, Model model). For binding the form @ModelAttribute annotation may be also required

    – WildDev
    Nov 24 '18 at 10:14













  • I tried that, also with the @ModelAttribute but it still doesn't work

    – Corni
    Nov 24 '18 at 10:43

















You'll have to create Spring MVC controller method with Model injected as param and target view name return value. Then you can edit it in model.addAttribute("currentBook", currentBook) way and access the attribute later inside the Thymeleaf view. Spring MVC

– WildDev
Nov 24 '18 at 9:46







You'll have to create Spring MVC controller method with Model injected as param and target view name return value. Then you can edit it in model.addAttribute("currentBook", currentBook) way and access the attribute later inside the Thymeleaf view. Spring MVC

– WildDev
Nov 24 '18 at 9:46















Sorry, I don't quite get what you mean. I have a controller in which I'm using this statement. It looks like this: @GetMapping("/books") String edits(Model model, BookForm bookForm) { ... Is that what you mean?

– Corni
Nov 24 '18 at 10:11







Sorry, I don't quite get what you mean. I have a controller in which I'm using this statement. It looks like this: @GetMapping("/books") String edits(Model model, BookForm bookForm) { ... Is that what you mean?

– Corni
Nov 24 '18 at 10:11















Yes, but param order matters. It should to be .edits(BookForm form, Model model). For binding the form @ModelAttribute annotation may be also required

– WildDev
Nov 24 '18 at 10:14







Yes, but param order matters. It should to be .edits(BookForm form, Model model). For binding the form @ModelAttribute annotation may be also required

– WildDev
Nov 24 '18 at 10:14















I tried that, also with the @ModelAttribute but it still doesn't work

– Corni
Nov 24 '18 at 10:43





I tried that, also with the @ModelAttribute but it still doesn't work

– Corni
Nov 24 '18 at 10:43












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456553%2fspring-java-html-how-can-i-populate-an-html-form-with-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456553%2fspring-java-html-how-can-i-populate-an-html-form-with-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python