Reputation: 11
I'm trying to get it to print out my Linked List from the user input. This code adds the object to the linked list as I can see it being printed to the console but they are not being printed to the page. Thanks.
Controller:
@Controller
@RequestMapping("/addr")
public class AddressController {
public Collection<Address> addresses = (Collection<Address>) Collections.synchronizedCollection(new LinkedList<Address>());
/* @RequestMapping("/new")
public String getAddressForm() {
System.out.println("Test");
return "addressProject/addressBook";
}
*/
@RequestMapping("/new")
public ModelAndView submitForm(String name, String email, String group, String phoneNumber, String address){
Address addr = new Address(name, email, group, phoneNumber, address);
addresses.add(addr);
ModelAndView modelandview = new ModelAndView("addressProject/addressBook");
modelandview.addObject("addresses", addr);
System.out.println("Address: name=" + addr.getName() + ", email=" + addr.getEmail() + ", group=" + addr.getGroup() + ", phoneNumber=" + addr.getPhoneNumber() + ", address=" + addr.getAddress());
return modelandview;
}
}
JSP:
<body>
<div class="container">
<form>
Name: <input type="text" name="name"><br>
<br/>
Email: <input type="text" name="email"><br>
<br/>
Group: <input type="text" name="group"><br>
<br/>
Phone Number: <input type="text" name="phoneNumber"><br>
<br/>
Address: <input type="text" name="address"><br>
<br/>
<input type="submit" />
</form>
</div>
<h2>Addresses v5</h2>
<c:forEach items="${addresses}" var="address">
<tr>
<td>${address}</td>
</tr>
Upvotes: 0
Views: 513
Reputation: 23226
You add the address to the model and not the list:
modelandview.addObject("addresses", addr);
should be:
modelandview.addObject("addresses", addresses);
jsp.
<table>
<c:forEach items="${addresses}" var="address">
<tr>
<td>${address}</td>
</tr>
</c:forEach>
<table>
And ensure you have declared the taglib directive at the top of your JSP file:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Upvotes: 1