After submitting a new Episode entity I need my user to be sent back to the dashboard - ie /dashboard
I have tried
return /dashboard
but that simply sent the template to the browser without any data.
My controller looks like this:
public class formController {
private EpisodeRepository episodeRepository;
#Autowired
public void setEpisodeRepository(EpisodeRepository episodeRepository) {
this.episodeRepository = episodeRepository;
}
#GetMapping("/newEpisode")
public String episodeForm(Model model) {
model.addAttribute("episode", new Episode());
return "episode";
}
#PostMapping("/newEpisode")
public String episodeSubmit(#ModelAttribute Episode episode) {
episodeRepository.save(episode);
return "dashboard";
}
#GetMapping("/dashboard")
public String dashboard(Model model) {
List<Episode>listEpisodes = new ArrayList<>();
episodeRepository.findAll().forEach((episode) -> {
listEpisodes.add(episode);
});
System.out.println(listEpisodes.size());
model.addAttribute("episodes", listEpisodes);
return "dashboard";
}
#GetMapping("/event/{eventno}")
public String findEvent(#PathVariable String eventno, Model model) {
Episode episode = new Episode();
episode = episodeRepository.findByeventno(eventno);
System.out.println("Printing the values held in episode for one get: " +eventno);
model.addAttribute("episode", episode);
return "episode";
}
}
And the relevant part of the html
<form id="form" class="form-horizontal" method="post" action="#" th:action="#{/newEpisode}" th:object="${episode}">
And the answer is:
#PostMapping("newEpisode")
public String episodeSubmit(#ModelAttribute Episode episode) {
episodeRepository.save(episode);
return "redirect:/dashboard";
}
Related
could someone help me figure out why it is passing null values instead of actual names? The form has a input textbox for last name and fistname but I would only get null values when I access "authorRequest.getLastName() or authorRequest.getFirstName().
Controller:
#Controller
public class AuthorController {
private BooktownService __authorService = BooktownServiceFactory.getInstance();
private Model mod;
private String msg = "";
first endpoint, return a collection of authors
#GetMapping("/booktown/list")
public List<Author> returnAuthors() {
return __authorService.getAuthors();
}
#GetMapping("/booktown/list")
public String returnAuthors(Model model) {
this.mod = model;
Author auth= new Author();
List<Author> auths = __authorService.getAuthors();
if(msg == ""){
msg = "Listing page for Authors";
}
mod.addAttribute("authors", auths);
mod.addAttribute("authorRequest", auth);
mod.addAttribute("msg",msg);
return "list_form";
}
// third endpoint, create an Author via POST
#PostMapping("/booktown/add")
public String createAuthor(#ModelAttribute("authorRequest") Author authorRequest) {
__authorService.createAuthor(authorRequest.getLastName(), authorRequest.getFirstName());
msg = "Added Author" + authorRequest.getFirstName() + " " + authorRequest.getLastName();
return "redirect:/booktown/list";
}
// Sixth endpoint: DELETE
#GetMapping("/booktown/delete")
public String deleteAuthor(#RequestParam Integer id) {
// returns a boolean
__authorService.deleteAuthor(id);
msg = "Deleted Author " + id;
System.out.println("deleted");
return "redirect:/booktown/list";
}
}
Html:
<form action="#" th:action="#{/booktown/add}" th:object="${authorRequest}" method="post">
<p>Last name: <input type="text" th:field="*{__lastName}" /></p>
<p>First name: <input type="text" th:field="*{__firstName}" /></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
Author.java:
package com.booktown.booktownservice;
public class Author {
public Author(){}
public Author(int id, String lname, String fname) {
__id = id;
__lastName = lname;
__firstName = fname;
}
public int getAuthorID() {
return __id;
}
public String getLastName() {
return __lastName;
}
public String getFirstName() {
return __firstName;
}
public int get__id() {
return __id;
}
public String get__lastName() {
return __lastName;
}
public String get__firstName() {
return __firstName;
}
private int __id;
private String __lastName;
private String __firstName;
}
TaskController
#GetMapping("/admin/addTask")
public String task(String email, Model model, HttpSession session){
session.setAttribute("email",email);
model.addAttribute("task",new Task());
return "task";
}
#PostMapping("/admin/addTask")
public String addTask(#Valid Task task, BindingResult bindingResult,HttpSession session){
if(bindingResult.hasErrors()){
return "task";
}
String emailik = (String) session.getAttribute("email");
taskService.addTask(task,userService.findOne(emailik));
return "redirect:/admin/users";
}
#GetMapping("/admin/usertask")
public String getEmail(User email, Model model,HttpSession session){
model.addAttribute("task",taskService.findUserTask(email));
model.addAttribute("emaail",session.getAttribute("email"));
return "usertasklist";
}
users list controller
#GetMapping("admin/users")
public String listUsers(Model model, #RequestParam(defaultValue = "") String name) {
model.addAttribute("users",userService.findByName(name));
return "list";
}
}
when i enter user in admin/users i get link like /admin/usertask?email=rafs#gmail.com where is users tasks
and i want this work with it
<div th:each="email:${emaail}">
<a th:href="#{/admin/addTask(email=${email})}" class="btn btn-dark"></a>
</div>
but with getattribute it is displayed just after adding task
is there any way to get this email?
If you want to get the email in usertaks endpoint /admin/usertask?email=rafs#gmail.com
You need to change your controller like this
#GetMapping("/admin/usertask")
public String getEmail(#RequestParam("email") String email, Model model,HttpSession session){
model.addAttribute("task",taskService.findUserTask(email));
model.addAttribute("emaail",session.getAttribute("email"));
return "usertasklist";
}
This question already has answers here:
Passing a list generated with Javascript to a Spring controller
(3 answers)
Closed 5 years ago.
I'm building an app with Spring Boot, the MVC pattern, and Thymeleaf as template engine. I have a form that generates a list with JavaScript, and a controller with a #ModelAttribute expecting a list, to be saved into the database.
At the JavaScript side I collect the items with this array:
groupList = [];
groupList.push(inputValue);
function getGroupList(){
return groupList;
}
In the html I'm trying to assign the array value to a hidden input field:
<input type="hidden" class="invisible" id="groupList" th:field="*{groupList}"/>
With this inline function:
<script th:inline="javascript" type="text/javascript">
$(function() {
var groupListCollected = getGroupList();
$("#groupList").val(groupListCollected);
});
</script>
My problem at this point, is that I don't find the way to collect the array, and pass it to the Controller as a list.
This is the value that the console shows, for the input field:
<input type="hidden" class="invisible" id="groupList" name="groupList"
value="[object HTMLInputElement]">
Any advice to face this issue, would be much appreciated. Thanks in advance.
You can proceed like this :
Create a Model, for exemple:
public class User {
private String username;
public User() {
}
public User(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
Create a FormModel:
public class FormUsers {
private List<User> listUsers;
public FormUsers(List<User> listUsers) {
this.listUsers = listUsers;
}
public FormUsers() {
}
public List<User> getListUsers() {
return listUsers;
}
public void setListUsers(List<User> listUsers) {
this.listUsers = listUsers;
}
#Override
public String toString() {
return "FormUsers{" + "listUsers=" + listUsers + '}';
}
}
in your controller (Get and Post) :
#RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(ModelMap model) {
FormUsers formUsers = new FormUsers();
List<User> list = new ArrayList<>();
list.add(new User("Wassim"));
formUsers.setListUsers(list);
logger.debug("--> " + formUsers.toString());
model.addAttribute("formUsers", formUsers);
return "hello";
}
#RequestMapping(value = "/hello", method = RequestMethod.POST)
public String helloPost(#ModelAttribute FormUsers formUsers, ModelMap model) {
logger.debug("Get List of new users --> " + formUsers.toString());
return "hello";
}
In my view (hello.html) : for listing all users :
<button id="addAction">ADD</button>
<form action="#" th:action="#{/hello}" th:object="${formUsers}" method="post">
<ul id="myUl">
<input type="text" th:field="*{listUsers[0].username}" />
</ul>
<input type="submit" value="Submit" />
</form>
Now if I want to add another user with javascript when I clic in the button 'ADD, I will append() a new 'li' element containing the new user :
<script type="text/javascript">
$(document).ready(function () {
console.log("ready!");
$("#addAction").click(function () {
console.log("FUNCTION");
$("#myUl").append('<li><input id="listUsers1.username" name="listUsers[1].username" value="Rafik" type="text"/></li>');
});
});
</script>
When I Submit, I will get a list with 2 User : 'Wassim' & 'Rafik'.
You can manage this exemple (also the index of the list must managed properly) to append your list with the needed DATA.
I'm trying to use a type converter in a Spring boot app and using Thymeleaf but I can't get it working. I've put some code on Github so you can see exactly what I'm trying to do. This is Spring 1.5.1 and Thymeleaf 3.0.3. https://github.com/matthewsommer/spring-thymeleaf-simple-converter
Basically this code is just trying to add a person to a comment object. The person object is null when it gets posted and I don't understand why.
Something that's odd is that the ID of the person isn't being added to the value attribute but it is if th:field="*{body}" is removed. I think it has to do with this: https://github.com/thymeleaf/thymeleaf/issues/495 but I'm currently trying to add BindingResult and it's not working...
My HTML is:
<body>
<div th:if="${personObject != null}" th:text="${personObject.name}"></div>
<form th:action="#{/}" th:object="${comment}" method="post">
<input type="hidden" th:if="${personObject != null}" th:value="${personObject.id}" th:field="*{person}" />
<textarea id="comment" placeholder="Comment..." th:field="*{body}"></textarea>
<button id="comment_submit" type="submit">Comment</button>
</form>
<div th:text="${comment.body}"></div>
</body>
My controller:
#Controller
public class HomeWebController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String getHome(final HttpServletRequest request, final Map<String, Object> model, #ModelAttribute(value = "comment") Comment comment) {
model.put("personObject", new Person(1, "John Smith"));
return "Home";
}
#RequestMapping(value = "/", method = RequestMethod.POST)
public String postHome(final HttpServletRequest request, final Map<String, Object> model, #ModelAttribute(value = "comment") Comment comment) {
model.put("commentBody", comment.getBody());
model.put("person", comment.getPerson());
return "Home";
}
}
And the converter:
#Component
public class StringToPersonConverter implements Converter<String, Person> {
#Autowired
public StringToPersonConverter() { }
#Override
public Person convert(String id) {
if(id == "1") {
Person person = new Person(1, "John Smith");
return person;
}
return null;
}
}
Hi finally I had to do some changes to make it work, but this is the result class by class.
ConvertorApplication:
#SpringBootApplication
#Configuration
#EnableWebMvc
public class ConvertorApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(ConvertorApplication.class, args);
}
//Add converter and configuration annotation
#Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new StringToPersonConverter());
}
}
StringToPersonConverter:
#Override
public Person convert(String id) {
//Never compare String with == use equals, the "==" compares memory space not the values
if(id.equals("1")) {
Person person = new Person(1, "John Smith");
return person;
}
return null;
}
HomeWebController
#Controller
public class HomeWebController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String getHome(final Map<String, Object> model, #ModelAttribute(value = "comment") Comment comment) {
//Initialize the comment with the person inside, no need of personObject object
model.put("comment", new Comment(new Person(1, "John Smith")));
return "Home";
}
#RequestMapping(value = "/", method = RequestMethod.POST)
public String postHome(final Map<String, Object> model,
#ModelAttribute(value = "comment") Comment comment,
#RequestParam(value = "person.id") Person person) {
//from the view retrieve the value person.id which will be used by the converter to build the Person entity
comment.setPerson(person);
model.put("comment", comment);
return "Home";
}
}
Comment (Add empty constructor)
public Comment(){}
Person (Add empty constructor)
public Person(){}
Home.jsp (Basically remove personObject, not need)
<!DOCTYPE html>
<html xmlns:th="//www.thymeleaf.org">
<body>
<div th:text="${comment.person.name}"></div>
<form th:action="#{/}" th:object="${comment}" method="post">
<input type="hidden" th:field="*{person.id}" />
<textarea id="comment" placeholder="Comment..." th:field="*{body}"></textarea>
<button id="comment_submit" type="submit">Comment</button>
</form>
<div th:text="${comment.body}"></div>
</body>
</html>
That's would be everything to make it work.
I want to do something like this, where Foo is a class with one String field name, and getter/setter:
<form:form id="frmFoo" modelAttribute="foos">
<c:forEach items="${foos}" var="foo">
<form:input path="${foo.name}" type="text"/>
And then submit the complete list of Foos with updated names?
My controller looks like this:
#RequestMapping(value = "/FOO", method = RequestMethod.POST)
public String getSendEmail(List<Foo> foos, Model model) {
// ...
}
Maybe this answersyour question:
CONTROLLER :
#Controller("/")
public class FooController{
//returns the ModelAttribute fooListWrapper with the view fooForm
#RequestMapping(value = "/FOO", method = RequestMethod.GET)
public String getFooForm(Model model) {
FooListWrapper fooListWrapper = new FooListWrapper();
fooListWrapper.add(new Foo());
fooListWrapper.add(new Foo());
//add as many FOO you need
model.addAttribute("fooListWrapper", fooListWrapper);
return "fooForm";
}
#RequestMapping(value = "/FOO", method = RequestMethod.POST)
public String postFooList(#ModelAttribute("fooListWrapper")FooListWrapper fooListWrapper, Model model) {
//...........
}
}
FOO LIST WRAPPER :
public class FooListWrapper {
private List<Foo> fooList;
public FooListWrapper() {
this.fooList = new ArrayList<Foo>();
}
public List<Foo> getFooList() {
return fooList;
}
public void setFooList(List<Foo> fooList) {
this.fooList = fooList;
}
public void add(Foo foo) {
this.fooList.add(foo);
}
}
FOO CLASS :
public class Foo {
private String name;
public Foo() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
JSP VIEW (name = fooForm):
<c:url var="fooUrl" value="/FOO"/>
<form:form id="frmFoo" action="${fooUrl}" method="POST" modelAttribute="fooListWrapper">
<c:forEach items="${fooListWrapper.fooList}" varStatus="i">
<form:input path="fooList[${i.index}].name" type="text"/>
</c:forEach>
<button>submit</button>
</form:form>
Although the above answer works, here's an alternate that does not require you to create a wrapper class/ form class.
Model And Controller
public class Foo {
private String name;
private List<Foo> fooList; //**must create this list, also getter and setter**
public Foo() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getFooList() {
return fooList;
}
public void setFooList(String fooList) {
this.fooList = fooList;
}
}
#Controller("/")
public class FooController{
//returns the ModelAttribute fooListWrapper with the view fooForm
#RequestMapping(value = "/FOO", method = RequestMethod.GET)
public String getFooList(Model model) {
List<Foo> fooList = service.getFooList();
model.addAttribute("fooList", fooList);
return "list_foo"; //name of the view
}
#RequestMapping(value = "/FOO", method = RequestMethod.POST)
public String postFooList(#ModelAttribute("foo")Foo foo, Model model) {
List<Foo> list = foo.getFooList(); // **This is your desired object.
//If you debug this code, you can easily find this is the list of
//all the foo objects that you wanted, provided you pass them properly.
//Check the jsp file to see one of the ways of passing such a list of objects**
//Rest of the code
}
}
JSP View
<form:form id="form" action="<paste-target-url-here>" method="POST" modelAttribute="fooList">
<c:forEach items="${fooList}" varStatus="i">
<form:input path="fooList[${i.index}].name" type="text"/>
<!-- Here you are setting the data in the appropriate index which will be caught in the controller -->
</c:forEach>
<button>submit</button>
</form:form>