I am working on a JEE school project which is done with Spring and Hibernate. My tables are :
INFRACTION
id_infraction
description
DOSSIER
id_dossier
description
gravite
INFRACTIONDOSSIER
id_dossier
id_infraction
The application does work well for adding "infractions" and "dossiers" and retrieve them in jsp pages.
The app should be able to add multiple "infractions" to one "dossier", and that is where i'm blocked;
I'm learning One-to-many tags and implemented them in my "Dossier" Class, but as I want a form separated from the adding infraction and adding dossier ones, I don't know how to handle that.
Here is the code I added in my Dossier class :
#OneToMany
private Set<Infraction> infractions;
#OneToMany(cascade = CascadeType.ALL)
#JoinTable(
name = "DOSSIERINFRACTION",
joinColumns = #JoinColumn(name = "id_dossier"),
inverseJoinColumns = #JoinColumn(name = "id_infraction")
)
And my AppController class part handling adding Dossiers
/*
* Méthode d'ajout d'un dossier
*/
#RequestMapping(value = { "/new" }, method = RequestMethod.GET)
public String newDossier(ModelMap model) {
Dossier dossier = new Dossier();
model.addAttribute("dossier", dossier);
model.addAttribute("edit", false);
return "registration";
}
/*
* Gère l'envoi du POST pour l'ajout de dossier ainsi que la validation du
* formulaire
*
*/
#RequestMapping(value = { "/new" }, method = RequestMethod.POST)
public String saveDossier(#Valid Dossier dossier, BindingResult result, ModelMap model) {
if (result.hasErrors()) {
return "registration";
}
service.saveDossier(dossier);
model.addAttribute("success", "Dossier " + dossier.getNom() + " enregistré");
return "success";
}
JSP form to record a dossier
<form:form method="POST" modelAttribute="dossier">
<form:input type="hidden" path="id" id="id"/>
<table>
<tr>
<td><label for="nom">Nom: </label> </td>
<td><form:input path="nom" id="nom"/></td>
<td><form:errors path="nom" cssClass="error"/></td>
</tr>
<tr>
<td><label for="prenom">Prénom: </label> </td>
<td><form:input path="prenom" id="prenom"/></td>
<td><form:errors path="prenom" cssClass="error"/></td>
</tr>
<tr>
<td><label for="plaque">Plaque: </label> </td>
<td><form:input path="plaque" id="plaque"/></td>
<td><form:errors path="plaque" cssClass="error"/></td>
</tr>
<tr>
<td><label for="permis">Permis: </label> </td>
<td><form:input path="permis" id="permis"/></td>
<td><form:errors path="permis" cssClass="error"/></td>
</tr>
<tr>
<td colspan="3">
<c:choose>
<c:when test="${edit}">
<input type="submit" value="Mettre à jour"/>
</c:when>
<c:otherwise>
<input type="submit" value="Enregistrer"/>
</c:otherwise>
</c:choose>
</td>
</tr>
</table>
</form:form>
My question may seem a bit dumb, but how can I create a form which can add multiple infractions to dossier, and how to display the infractions from a dossier ?
Thank you,
Romain
Related
I tried all the solutions but I keep getting this error. Moreover if i don't use form:form in jsp file and use a simple HTML, I get the desired output.
Controller Class
#Controller
public class controller_class {
/*
* #RequestMapping(path = "/index", method = RequestMethod.GET) public
* ModelAndView mar() { return new ModelAndView("index","command",new marks());
* }
*/
#RequestMapping("/index")
public ModelAndView showComments() {
return new ModelAndView("marks","command",new marks());
}
#RequestMapping(value = "/addMarks", method = RequestMethod.POST)
public ModelAndView stud(#ModelAttribute("marks") marks m) {
ModelAndView mv = new ModelAndView("result");
int k = m.calculate();
mv.addObject("tot_marks", k);
return mv;
}
}
index.jsp
<form:form method = "POST" modelAttribute="marks" action = "/springmvc_qa3/addMarks">
<table>
<tr>
<td><form:label path = "sci_marks">Name</form:label></td>
<td><form:input path = "sci_marks" /></td>
</tr>
<tr>
<td><form:label path = "maths_marks">Age</form:label></td>
<td><form:input path = "maths_marks" /></td>
</tr>
<tr>
<td><form:label path = "eng_marks">id</form:label></td>
<td><form:input path = "eng_marks" /></td>
</tr>
<tr>
<td colspan = "2">
<input type = "submit" value = "Submit"/>
</td>
</tr>
</table>
</form:form>
</body>
Correct Output if I use this instead
<form method="POST" action="/springmvc_qa3/addMarks" >
<table>
<tr>
<td><label >Science Marks</label></td>
<td><input type="text" name="sci_marks" /></td>
</tr>
<tr>
<td><label >Mathematics Marks</label></td>
<td><input type="text" name="maths_marks" /></td>
</tr>
<tr>
<td><label >English Marks</label></td>
<td><input type="text" name="eng_marks" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit" />
</td>
</tr>
</table>
</form>
What is the reason that I can't get the right output using the first method?
add the following to your controller class:
#ModelAttribute("marks")
public Marks nameOfMethodDoesntMatter(){
return new Marks();
}
make sure your Marks class has getters, setters and default constructor.
consider calling your class MarksDTO or something similar to convey its meaning better (DTO = data transfer object).
i have following scenario :
#ModelAttribute("persons")
public void addAttributes(Model model) {
Person person = new Person()
;
person.setAge(26);
person.setFirstName("mars");
model.addAttribute("persons", person);
}
#RequestMapping(value="/process-person")
public ModelAndView processPerson(#ModelAttribute Person person,#ModelAttribute ("persons")Person persons,ModelAndView modelAndView ) {
//
//ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("person-result-page");
modelAndView.getModel().remove("personObj");
modelAndView.addObject("pers", person);
----> modelAndView.addObject("pers2", persons);// persons holds the myname1 provided in input box
modelAndView.addObject("personObj", person);
return modelAndView;
}
As shown in the --> i want this variable -persons to hold the value obtained from addAttributes() method but it is taking the same values that i input from the jsp page :
<form:form method="POST" commandName="person-entity" action="process-person.html">
<table>
<tr>
<td><form:label path="firstName">Name:</form:label></td>
<td><form:input path="firstName" /></td>
</tr>
<tr>
<td><form:label path="age">Age:</form:label></td>
<td><form:input path="age" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit"/>
</td>
<td></td>
<td></td>
</tr>
</table>
</form:form>
<br/>
i saw few similar questions but none of them resolved this problem . Here i want values from addAttribute method to placed inside "persons" object, but it is taking the same value as that of "person" being provided from the form.jsp.
please help me out
When I run this it will create a new book if valid however upon error it will not post back to the page and I get a 400:
This application has no explicit mapping for /error, so you are seeing this as a fallback.
There was an unexpected error (type=Bad Request, status=400).
Validation failed for object='book'. Error count: 4
I tested with system outs, it never gets to the submit if it has errors, I think maybe #Valid is wrong or ???
This is my controller... please tell me what's missing ?
#Controller
#RequestMapping("/")
public class BookController{
#RequestMapping(method=RequestMethod.GET)
public String home(Model model) {
refData = new ReferenceData();
model.addAttribute("refData", refData);
model.addAttribute("book", new Book());
List<Book> books = BookRepo.findAll();
model.addAttribute("books", books);
return "home";
}
#RequestMapping(method=RequestMethod.POST)
public String submit(#Valid Book book,Model model, BindingResult bindingResult) {
refData = new ReferenceData();
model.addAttribute("refData", refData);
if (bindingResult.hasErrors()) {
return "home";
}
BookRepo.save(book);
return "redirect:/";
}
My form looks like this...
<form name="addForm" action="#" th:action="#{/}" th:object="${book}" method="post">
<table>
<tr>
<td><label for="*{title}">Book:</label></td>
<td><input type="text" th:field="*{title}" /></td>
<td th:if="${#fields.hasErrors('title')}" th:errors="*{title}">Name
Error</td>
</tr>
<tr>
<td><label for="*{author}">Author:</label></td>
<td><input type="text" th:field="*{author}" /></td>
<td th:if="${#fields.hasErrors('author')}" th:errors="*{author}">Author
Error</td>
</tr>
<tr>
<td><label for="*{genre}">Genre:</label></td>
<td><input type="text" th:field="*{genre}" /></td>
<td th:if="${#fields.hasErrors('genre')}" th:errors="*{genre}">genre
Error</td>
</tr>
<tr>
<td><label for="*{pages}">Pages:</label></td>
<td><input type="text" th:field="*{pages}" /></td>
<td th:if="${#fields.hasErrors('pages')}" th:errors="*{pages}">pages
Error</td>
</tr>
<tr>
<td><label th:for="*{year}">Year:</label></td>
<td>
<select th:field="*{year}" ng-model="bookData.year">
<option th:each="selectItem: ${refData.years}"
th:value="${selectItem.value}" th:text="${selectItem.label}">year</option>
</select>
</td>
<td th:if="${#fields.hasErrors('year')}" th:errors="*{year}">year
Error</td>
</tr>
<tr>
<td><label th:for="*{rating}">Rating:</label></td>
<td><select th:field="*{rating}" ng-model="bookData.rating">
<option th:each="selectItem: ${refData.rating}"
th:value="${selectItem.value}" th:text="${selectItem.label}">rating</option>
</select></td>
<td th:if="${#fields.hasErrors('rating')}" th:errors="*{rating}">rating
Error</td>
</tr>
<tr>
<td><button type="submit">Submit</button></td>
</tr>
</table>
</form>
my entity...
#Entity
public final class Book
{
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
#NotNull
#Pattern(regexp="^[a-zA-Z0-9_]+$")
private String title;
#NotNull
#Pattern(regexp="^[a-zA-Z0-9_]+$")
private String author;
#NotNull
#Pattern(regexp="^[a-zA-Z0-9_]+$")
private String genre;
#NotNull
#Digits(message="message here", integer=32767, fraction = 0)
private Integer pages;
private String year;
private String rating;
I found I had to place the bindingResult directly after the #Valid Book entity thus...
#RequestMapping(method=RequestMethod.POST)
public String submit(#Valid Book book,**BindingResult bindingResult**, Model model ) {
refData = new ReferenceData();
model.addAttribute("refData", refData);
if (bindingResult.hasErrors()) {
return "home";
}
BookRepo.save(book);
return "redirect:/";
}
Wow what a minor=major mistake !!
Johnny O.
I'm currently having inconsistent of adding and deleting data which happen randomly without any errors showing. The page will just keep loading forever until I stop my tomcat server. I can't identify where is the problem which is causing this inconsistency.
Controller
#RequestMapping(value = "menu/{userId}", method = RequestMethod.GET)
public ModelAndView orderPizzaForm(#ModelAttribute(value = "pizzaform") PizzaTO pizzaTO,
#PathVariable String userId) {
ModelAndView model = new ModelAndView("pizzaform");
List pizzaList = pizzaService.getListByUserId(userId);
model.addObject("pizzaList", pizzaList);
return model;
}
#RequestMapping(value = "menu/add/{userId}", method = RequestMethod.POST)
public ModelAndView orderPizza(#ModelAttribute(value = "pizzaform") PizzaTO pizzaTO, #PathVariable String userId,
BindingResult result) {
ModelAndView model = new ModelAndView("pizzaform");
pizzaFormValidator.validate(pizzaTO, result);
if (result.hasErrors()) {
List pizzaList = pizzaService.getListByUserId(userId);
model.addObject("pizzaList", pizzaList);
return model;
} else {
pizzaTO.setUserId(userId);
String[] data = pizzaTO.getPizzaSize().split("\\|");
pizzaTO.setPizzaSize(data[0]);
pizzaTO.setPizzaPrice(Double.parseDouble(data[1]));
pizzaService.addPizza(pizzaTO);
List pizzaList = pizzaService.getListByUserId(userId);
model.addObject("pizzaList", pizzaList);
return model;
}
}
////////////////////////////// DELETE PIZZA //////////////////////////////
#RequestMapping(value = "menu/delete/{userId}/{pizzaId}", method = RequestMethod.GET)
public ModelAndView editEmployee(#ModelAttribute(value = "pizzaform") PizzaTO pizzaTO, #PathVariable String pizzaId,
#PathVariable("userId") String userId) {
pizzaService.deletePizza(pizzaId);
ModelAndView model = new ModelAndView("pizzaform");
List pizzaList = pizzaService.getListByUserId(userId);
model.addObject("pizzaList", pizzaList);
return model;
}
pizzaform.jsp
<body>
<div style="text-align: center">
<form:form commandName="pizzaform"
action="${pageContext.request.contextPath}/menu/add/${userId}" method="POST">
<form:hidden path="userId" value="${userId}" />
<table align="center">
<tr>
<td><label id="pizzaCrust">Choose a crust:</label></td>
<td><form:radiobutton path="pizzaCrust" value="Hand Tossed" />Hand
Tossed <form:radiobutton path="pizzaCrust" value="Thin Crust" />Thin
Crust<form:errors
path="pizzaCrust" class="error" /></td>
</tr>
<tr>
<td><label id="pizzaFlavour">Choose a flavour:</label></td>
<td><form:radiobutton path="pizzaFlavour" value="Pepperoni" />Pepperoni
<form:radiobutton path="pizzaFlavour" value="BBQ Chicken" />BBQ
Chicken <form:radiobutton path="pizzaFlavour"
value="Spicy Chicken" />Spicy Chicken<form:errors
path="pizzaFlavour" class="error" /></td>
</tr>
<tr>
<td><label id="pizzaSize">Choose a size:</label></td>
<td><form:radiobutton path="pizzaSize" value="10|23.80" />10 inch (Regular - 4 Pax)
<form:radiobutton path="pizzaSize" value="12|30.80" />12 inch (Medium - 6 Pax)
<form:radiobutton path="pizzaSize" value="14|37.80" />14 inch (Large - 8 Pax)<form:errors
path="pizzaSize" class="error" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Add" />
<button type="button" name="back"
onclick="window.location='${pageContext.request.contextPath}/main'">Back</button></td>
</tr>
</table>
</form:form>
<br> <br>
<table border="1" bgcolor="black" width="600px" align="center">
<tr
style="background-color: white; color: black; text-align: center;"
height="30px">
<td><b>Crust</b></td>
<td><b>Flavor</b></td>
<td><b>Size (Inch)</b></td>
<td><b>Price (S$)</b></td>
<td></td>
</tr>
<c:forEach items="${pizzaList}" var="pizza">
<tr
style="background-color: white; color: black; text-align: center;"
height="30px">
<td><c:out value="${pizza.pizzaCrust}" /></td>
<td><c:out value="${pizza.pizzaFlavour}" /></td>
<td><c:out value="${pizza.pizzaSize}" /></td>
<td><c:out value="${pizza.pizzaPrice}" /></td>
<td>Delete</td>
</tr>
</c:forEach>
</table>
</div>
Hi I am new to spring and I am trying to develop a simple portlet that accepts users first and last name and saves it to db using hibernate.
Basically I cannot figure out how the jsps and controllers communicate; I am missing some chunk here.
This is my first controller that needs to be called (where do I mention so?)
package codes.controller;
import javax.portlet.RenderResponse;
import codes.base.User;
import codes.service.UserService;
#Controller(value="SimpleUserController")
#RequestMapping(value = "VIEW")
public class SimpleUserController {
// -- auto-wiring of service dependency
#Autowired
#Qualifier("userService")
private UserService userService;
// --maps the incoming portlet request to this method
#RenderMapping
public String showUsers(RenderResponse response) {
return "home";
}
#ExceptionHandler({ Exception.class })
public String handleException() {
return "errorPage";
}
// -- #ModelAttribute here works as the referenceData method
#ModelAttribute(value="user")
public User getCommandObject() {
return new User();
}
}
Initially I am displaying a home.jsp that will display the form with two input boxes and a submit button.
<%#include file="include.jsp" %>
<portlet:actionURL var="addUserActionUrl">
<portlet:param name="myaction" value="addUser" />
</portlet:actionURL>
<form:form name="home" commandName="user" method="post"
action="${addUserActionUrl}">
<table>
<tr>
<td>First Name:<font style="color: #C11B17;">*</font></td>
<td><form:input path="firstname" /></td>
</tr>
<tr>
<td>Last Name:<font style="color: #C11B17;">*</font></td>
<td><form:input path="lastname" /></td>
</tr>
<table align="right">
<tr>
<td> </td>
<td><input type="submit" value="SUBMIT" /></td>
</tr>
</table>
</table>
</form:form>
This JSP should call the action method in the AddUserController.java:
package codes.controller;
import javax.portlet.ActionResponse;
import javax.portlet.RenderResponse;
import codes.base.User;
import codes.service.UserService;
#Controller(value = "AddUserController")
#RequestMapping(value = "VIEW")
public class AddUserController {
#Autowired
#Qualifier("userService")
private UserService userService;
#RenderMapping(params = "myaction=addUser")
public String showRegisterPage(Model model) {
model.addAttribute("user", new User());
model.addAttribute("users", getUsers());
return "addUser";
}
public List<User> getUsers() {
return userService.getAllUsers();
}
#ActionMapping(params = "myaction=addUser")
public void addBook(#ModelAttribute(value = "user") User user,
BindingResult bindingResult, ActionResponse response,
SessionStatus sessionStatus) {
if (!bindingResult.hasErrors()) {
userService.addUser(user);
response.setRenderParameter("myaction", "users");
sessionStatus.setComplete();
} else {
response.setRenderParameter("myaction", "addUser");
}
}
}
This time this firstname+last name should be saved in the db AND the screen should refresh to show a new form that will have a dropdown with the current users' names in the database and another first name and last name form fields. If you select a username from the dropdown the form fields are populated and you can edit these values and click on UPdate button to save the values in DB. Otherwise you can add a new user to the database using submit button.
addUser.jsp:
<%#include file="include.jsp" %>
<portlet:actionURL var="addUserActionUrl">
<portlet:param name="myaction" value="addUser" />
</portlet:actionURL>
<portlet:renderURL var="homeUrl">
<portlet:param name="myaction" value="Users" />
</portlet:renderURL>
<script type="text/javascript" src="js/userRelated.js"></script>
<form:form name="addUser" commandName="user" method="post"
action="${addUserActionUrl}">
<form:select path="model">
<form:option value="NONE" label="--- Select ---" id="userList" onchange="showHide()"/>
<form:options items="${users}" />
</form:select>
<table>
<tr>
<td>First Name:<font style="color: #C11B17;">*</font></td>
<td><form:input path="firstname" /></td>
</tr>
<tr>
<td>Last Name:<font style="color: #C11B17;">*</font></td>
<td><form:input path="lastname" /></td>
</tr>
<table align="right">
<tr>
<td> </td>
<td><input type="submit" id="submit" value="SUBMIT" />SUBMIT</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" id="update" value="SUBMIT" />UPDATE</td>
</tr>
</table>
</table>
</form:form>
I am hiding and unhiding the SUBMIT/UPDATE button using onchange of dropdown. How do I call different functions in the addUsercontroller depending on the button available?
by updating the action attribute of form element with javascript