Can not insert data into two tables spring mvc - java

Im using Spring mvc and facing the problem about inserting data into two tables, my target is inserting data into 2 tables from 1 jsp (1 form)
I have two 2 tables:
1.Cars
Id
Name
CityId(FK)
2.City
Id
Name
this is my controller:
#RequestMapping(value="/CreateCar", method = RequestMethod.GET)
public ModelAndView getCreateCarPage(ModelMap model) throws ServletException, IOException {
try {
Car myCar = new Car();
City city = new City();
myCar.setCity(city);
model.addAttribute("city", city);
model.addAttribute("createCar", myCar);
}
catch(Exception e) {
e.printStackTrace();
}
return new ModelAndView("testForm");
}
#RequestMapping(value="/CreateCar", method = RequestMethod.POST)
public ModelAndView setCreateCarPage(ModelMap model,
#ModelAttribute("createCar") Car createCar) throws SQLException, Exception {
try {
carService.createCar(createCar);
}
catch(Exception e) {
e.printStackTrace();
}
return new ModelAndView("successProcess");
}
}
JSP:
<form:form modelAttribute="createCar" method="POST" commandName="createCar" action="/CreateCar" enctype="multipart/form-data">
<fieldset style="width:300px">
<table cellspacing="0" cellpadding="0" align="center">
<tr>
<td>
<label>Name of car</label>
<form:input path="name" id="name"/>
</td>
<td>
<label>Name of city</label>
<form:input path="city.name" />
</td>
</tr>
<tr>
<td><input class="btn btn-danger" type="submit" value="Inser New" style="width:100px;" /></td>
</tr>
</table>
</fieldset>
With above source, it just insert name of car success but can not insert name of city
Log massages:
SqlExceptionHelper - Column 'city_id' cannot be null
How can I fix this problem ? thank so much !

Related

How do I get the selected value from a drop-down menu with Thymeleaf?

My user is adding an exam object which is then added to the subject object. Subject and exam have a one to many relationship.
The user is selecting the subject in the drop down menu.This menu contains string not actual subject objects.
In this form, how do I send an exam object and the selected item (String) to the controller?
My HTML file
<form action="#" th:action="#{/addExam}" th:object="${exam}"
method="post">
<div th:object="${subject}">
<select th:field="*{option}" class="form-control" id="subjectOrder"
name= "subjectOrder">
<option value="">Select subject</option>
<option
th:each="Subject : ${subjects}"
th:value="${Subject}"
th:text="${Subject}"></option>
</div>
<div>
<table>
<tr>
Exam Title
<td><input type="text" th:field="*{examTitle}" /></td>
</tr>
<tr>
<td> Exam grade worth </td>
<td><input th:field="*{examGradeWorth}" /></td>
</tr>
<tr>
<td><button type="submit">Submit post</button></td>
</tr>
</table>
</div>
</form>
The controller, I want to set subject Name to equal the subject the user selected in the drop down box.
#GetMapping("/addexam")
public String showExamForm(Model model) {
Authentication loggedInUser =
SecurityContextHolder.getContext().getAuthentication();
String email = loggedInUser.getName();
User user = userRepository.findByEmailAddress(email);
ArrayList<String> subjects = new ArrayList<String>();
for(Subject sub:user.getSubject())
{
subjects.add(sub.getSubjectName());
}
model.addAttribute("subjects", subjects);
return "addExam";
}
#PostMapping("/addexam")
public String addNewExam(#ModelAttribute("exam") #Valid #RequestBody Exam
exam,UserRegistrationDto userDto, BindingResult result, Model model) {
examRepository.save(exam);
model.addAttribute("examTitle", exam.getExamTitle());
model.addAttribute("examGradeWorth", exam.getExamGradeWorth());
String subjectName = ();
//I want to set subjectName to equal the selected option.
Subject subject = subjectRepository.findBySubjectName(subjectName);
subject.addExam(exam);
subjectRepository.save(subject);
return "userProfile1";
}
I managed to find the selected value.
See my code below.
Exam Controller:
#Controller
public class AddExamController {
#Autowired
private ExamRepository examRepository;
#Autowired
private SubjectRepository subjectRepository;
#Autowired
private UserRepository userRepository;
#ModelAttribute("exam")
public Exam exam() {
return new Exam();
}
#GetMapping("/addexam")
public String showExamForm(Model model) {
Authentication loggedInUser = SecurityContextHolder.getContext().getAuthentication();
String email = loggedInUser.getName();
User user = userRepository.findByEmailAddress(email);
ArrayList<String> subjects = new ArrayList<String>();
for(Subject sub:user.getSubject())
{
subjects.add(sub.getSubjectName());
}
model.addAttribute("subjects", subjects);
return "addExam";
}
#PostMapping("/addExam") //This was causing one problem i was getting. I had it as /addexam and it should have been addExam
public String addNewExam(#ModelAttribute("exam") #Valid #RequestBody Exam exam,UserRegistrationDto userDto, BindingResult result, Model model) {
examRepository.save(exam);
model.addAttribute("examTitle", exam.getExamTitle());
model.addAttribute("examGradeWorth", exam.getExamGradeWorth());
model.addAttribute("subject", "");
//String subjectName = ("subject", exam.getSubject());
// Subject subject = subjectRepository.findBySubjectName(subjectName);
//subject.addExam(exam);
/// subjectRepository.save(subject);
return "userProfile1";
}
}
HTML
<form action="#" th:action="#{/addExam}" th:object="${exam}" method="post">
<select th:field="*{subject}" class="form-control" id="subject" name="subject">
<option value="">Select subject</option>
<option
th:each="Subject : ${subjects}"
th:value="${Subject}"
th:text="${Subject}"></option>
<table>
<tr>
<td> Exam Title:</td>
<td><input type="text" th:field="*{examTitle}" /></td>
</tr>
<tr>
<td> Exam grade worth </td>
<td><input th:field="*{examGradeWorth}" /></td>
</tr>
<tr>
<td><button type="submit">Submit post</button></td>
</tr>
</table>
</div>
</form>

Display values retrieved from database in a table

I am quite new to hibernate frameworks and I am working on a Hibernate framework project in the netbeans IDE. I have a javadb that is connected to the project and I need to get the task list for each employee and display it as a bullet list in a <td> against each employee name. The following is the table displaying the employees' names and roles that are being fetched using the getEmployeeDetails method in the EmployeeHelper class.
Issue - The task list is always empty though I call the resultTaskList parameter that is passed from the Controller. - I know that I have to do an if conditional check for task within the nested forloop that will list the tasks to group them based on the employee names. But am not sure how to do an if within a jsp page.
Any suggestions on how to display the task list of each employee within the same table will be highly appreciated.
In this table, I intend to have the tasks pertaining to each employee as a bulletlist within each corresponding <td>.
Employee.jsp
...
<div class="content">
<!--Display table of roles with an edit button against each role-->
<form method="get" action="<%= request.getContextPath() %>/RoleController">
<br>
<table border ="2">
<thead class="thead-inverse">
<tr>
<th></th>
<th>Employee Name</th>
<th>Role</th>
<th>Tasks</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<c:forEach items="${result}" var="res">
<tr>
<th scope="row">
</th>
<td>
<c:out value="${res[0]}"></c:out>
</td>
<td>
<c:out value="${res[1]}"></c:out>
</td>
<td>
<c:out value="${resultTaskList[1]}"></c:out>
</td>
<td>
<input type="button" class="btn btn-info" value="Edit Role">
</td>
</tr>
</c:forEach>
</table>
</form>
</div>
...
EmployeeController.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
EmployeeHelper helper = new EmployeeHelper();
List<Employee> resultTaskList = helper.getEmployeeTasks();
request.setAttribute("resultTaskList", resultTaskList);
List<Employee> result = helper.getEmployeeDetails();
request.setAttribute("result", result);
request.getRequestDispatcher("Employee.jsp").forward(request, response);
}
EmployeeHelper.java
public class EmployeeHelper {
Session session = null;
public EmployeeHelper(){
this.session = HibernateUtil.getSessionFactory().openSession();
}
public List getEmployeeDetails(){
Transaction tx = this.session.beginTransaction();
List<Employee> employeeList = null;
try{
Query query = session.createQuery("select e.name, r.title from Employee as e, Role as r where e.employeeid=r.employeeid");
employeeList = (List<Employee>)query.list();
tx.commit();
}
catch(HibernateException ex){
ex.printStackTrace();
tx.rollback();
}
return employeeList;
}
public List getEmployeeTasks(){
Transaction tx = this.session.beginTransaction();
List<Employee> employeeTaskList = null;
try{
Query query = session.createQuery("select e.name, t.description from Employee as e, Role as r, Task as t, EmployeeTask as et where e.employeeid=r.employeeid and t.taskid=et.taskid and e.employeeid=et.employeeid");
employeeTaskList = (List<Employee>)query.list();
tx.commit();
}
catch(HibernateException ex){
ex.printStackTrace();
tx.rollback();
}
return employeeTaskList;
}
}
SQL Query executed within the getEmployeeTasks() method
instead of List employeeTaskList use List and iterate loop and set values in employee list try following code .
public List getEmployeeTasks(){
Transaction tx = this.session.beginTransaction();
List<Employee> employeeTaskList = null;
try{
session.beginTransaction();
Query query = session.createQuery("select e.name, t.description from Employee as e, Role as r, Task as t, EmployeeTask as et where e.employeeid=r.employeeid and t.taskid=et.taskid and e.employeeid=et.employeeid");
List<Object[]> result = (List<Object[]>) query.list();
if (null != result && result.size() != 0) {
for (Object[] obj : result) {
//Set values over here
employeeTaskList.set.....
}
tx.commit();
}
}
catch(HibernateException ex){
ex.printStackTrace();
tx.rollback();
}
return employeeTaskList;
}
These two lists are non-related as suggested by JB Nizet. Still You can do something like this.
<c:forEach items="${result}" var="res">
<tr>
<th scope="row">
</th>
<td>
<c:out value="${res[0]}"></c:out>
</td>
<td>
<c:out value="${res[1]}"></c:out>
</td>
<td>
<c:forEach items="${resultTaskList}" var="resultTaskList">
<c:if test = "${res[0] == resultTaskList[0]}">
<c:out value="${resultTaskList[1]}"></c:out>
</c:if>
</c:forEach>
</td>
<td>
<input type="button" class="btn btn-info" value="Edit Role">
</td>
</tr>
</c:forEach>
Hope this helps.

text field in file upload servlet, Java

In my java application i am uploading multiple files using java servlet.
All things works fine until i added extra text field in my form.
I am getting null document when i add text field.
Here is my code:-
JSP Form:-
<form action="upload/servlet" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>Upload File: </td>
<td><input type="file" name="file" multiple/>
</td>
<td style="color: red; font-style: italic;"><form:errors
path="file" />
</td>
</tr>
<tr>
<td>Generate Key</td><td> </td>
<td><input type="button" value="Change Key"/>
</td>
<td>${key}</td>
</tr>
<tr>
<td>Zip Code</td><td> </td>
<td><input type="text" value="100001" name="zipcode"/>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Upload" />
</td>
<td> </td>
</tr>
</table>
</form>
Here is my servlet:-
#Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (!ServletFileUpload.isMultipartContent(req)) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST,"Multipart content expected!");
}
ModelMap model = new ModelMap();
try {
#SuppressWarnings("unchecked")
List<FileItem> files = this.upload.parseRequest(req);
String userName=req.getSession().getAttribute("username").toString();
String fileName;
String contentType;
byte[] content;
System.out.print("Zipcode is "+req.getParameter("zipcode"));
for(FileItem item : files) {
if(item.isFormField()){
fileName = item.getName();
contentType = item.getContentType();
content = item.get();
String id=this.indexDocument(fileName, contentType, content,userName,req);
model.put(id, fileName);
System.out.println("Done for "+fileName+ " id "+id);
}
}
} catch (FileUploadException e) {
System.out.println("Error FileUploadException: "+e.getMessage());
throw new ServletException(e);
}
catch (Exception e) {
e.printStackTrace();
System.out.println("Error "+e.getMessage());
}
req.setAttribute("message", model);
req.getSession().setAttribute("tmpRetMessage", model);
// RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/success.do");
//dispatcher.forward(req, resp);
resp.sendRedirect("../success.do");
}
If i add two text fields then getting null document error two times. If i add three times then getting error three times.
Here form enctype type multipart (enctype="multipart/form-data"). So request.getParameter() value will be null. So you need to process file field and regular fields means other than file like text, radio, etc separately.
see for more how to get request parameters

Getting IllegalStateException when creating dynamic Fields using Thymeleaf

In my program, I am generating dynamic form names based on the number of feedbacks I get. I am then taking the satisfaction, comment and feedbackId inputs. They are different for each iteration. This is giving me an IllegalStateException error.
My HTML form is:
<form action="#" th:action="#{/heart2heart/token/__${tokenId}__/}" method="post">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover table-condensed">
<tr>
<th style="display:none;"></th>
<th th:text="#{service.desc}">Service</th>
<th th:text="#{feedback.description}">Feedback</th>
<th th:text="#{visit.date}">Date of Visit</th>
<th th:text="#{repr.name}">FU Repr</th>
<th th:text="#{resolution.response}">Response</th>
<th th:text="#{resolution.satisfactionLevel}">Satisfaction</th>
<th th:text="#{resolution.comment}">Comment</th>
</tr>
<tr th:each="feedback, feedbackStat : *{feedbacks}">
<td style="display:none;"><input type="hidden" th:field="*{feedbacks[__${feedbackStat.index}__].feedbackId}" th:value="${feedback.id}" /></td>
<td th:text="${feedback.service.description}">Steel</td>
<td th:text="${feedback.description}">Problem</td>
<td th:text="${feedback.visits[0].date}">12/08/2015</td>
<td th:text="${feedback.visits[0].repr.fullName}">XYZ</td>
<td th:text="${feedback.receipt.resolutions[0].response}">response</td>
<td>
<div class="radio">
<label><input type="radio" th:field="*{feedbacks[__${feedbackStat.index}__].satisfaction}" th:text="#{global.yes}" value="SATISFIED">Yes</input></label>
</div>
<div class="radio">
<label><input type="radio" th:field="*{feedbacks[__${feedbackStat.index}__].satisfaction}" th:text="#{global.no}" value="NOT SATISFIED">No</input></label>
</div>
</td>
<td><textarea th:field="*{feedbacks[__${feedbackStat.index}__].comment}" class="form-control" rows="2"></textarea></td>
</tr>
</table>
<div class="form-group">
<button type="submit" name="addRow" th:text="#{button.submit}"
class="btn btn-primary btn-md">Submit</button>
</div>
</div>
</form>
My controller is:
#RequestMapping(value = "/{tokenId}/", method = RequestMethod.POST)
public String addSatisfaction(#PathVariable int tokenId, #Valid ReceiptForm receiptForm, BindingResult result, Model model) {
try {
for (SatisfactionForm satisfactionForm : receiptForm.getFeedbacks()) {
Feedback feedback = new Feedback();
feedback.setId(satisfactionForm.getFeedbackId());
Feedback feedback1 = heart2heartService.getFeedbackById(feedback);
Resolution resolution = new Resolution();
resolution.setId(feedback1.getReceipt().getResolutions().get(0).getId());
resolution.setSatisfactionLevel(satisfactionForm.getSatisfaction().name());
resolution.setComment(satisfactionForm.getComment());
heart2heartService.addSatisfaction(resolution);
model.addAttribute("success", "Satisfaction added for tokenId " + tokenId);
}
} catch (Exception e) {
logger.error("Exception :: ", e);
}
return "success2";
}
#RequestMapping(value = "/{tokenId}/", method = RequestMethod.GET)
public String getSatisfaction(#PathVariable int tokenId, Model model) {
Token token = new Token();
token.setId(tokenId);
try {
model.addAttribute("feedbacks", heart2heartService.getFeedbacksByToken(token));
} catch (Exception e) {
logger.error("Exception :: ", e);
}
return "heart2heart/closeFeedback";
}
My forms are:
public class ReceiptForm {
private List<SatisfactionForm> feedbacks = new ArrayList<SatisfactionForm>();
public List<SatisfactionForm> getFeedbacks() {
return feedbacks;
}
public void setFeedbacks(List<SatisfactionForm> feedbacks) {
this.feedbacks = feedbacks;
}
}
and
public class SatisfactionForm {
public static enum Satisfaction {
NOT_SATISFIED, SATISFIED
}
private String comment;
private int feedbackId;
private Satisfaction satisfaction;
public String getComment() {
return comment;
}
public int getFeedbackId() {
return feedbackId;
}
public Satisfaction getSatisfaction() {
return satisfaction;
}
public void setComment(String comment) {
this.comment = comment;
}
public void setFeedbackId(int feedbackId) {
this.feedbackId = feedbackId;
}
public void setSatisfaction(Satisfaction satisfaction) {
this.satisfaction = satisfaction;
}
}
I am getting the following error:
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'feedbacks[0]' available as request attribute
How do I fix this?
You did not specify the form-backing bean in your Thymeleaf template.
You are using the *{} operator to access fields of the bean, but Thymeleaf does not know
which bean you want to use. To resolve the error, add a th:object to your form.
Assuming that heart2heartService.getFeedbacksByToken(token) returns a ReceiptForm your form would look like this:
<form action="#" th:action="#{/heart2heart/token/__${tokenId}__/}" method="post" th:object="${feedbacks}">

Neither BindingResult nor plain target object for bean name 'index' available as request attribute

I have a problem when trying to access the controller.
Here is my controller:
#Controller
#RequestMapping("/index.htm")
public class LoginController {
#Autowired
private UserService userService;
#RequestMapping(method = RequestMethod.GET)
public String showForm(Map model) {
model.put("index", new LoginForm());
return "index";
}
#RequestMapping(method = RequestMethod.POST)
public String processForm(LoginForm loginForm, BindingResult result,
Map model) {
if (result.hasErrors()) {
HashMap<String, String> errors = new HashMap<String, String>();
for (FieldError error : result.getFieldErrors()) {
errors.put(error.getField(), error.getDefaultMessage());
}
model.put("errors", errors);
return "index";
}
List<User> users = userService.getUsers();
loginForm = (LoginForm) model.get("loginForm");
for (User user : users) {
if (!loginForm.getEmail().equals(user.getEmail()) || !loginForm.getPassword().equals(user.getPassword())) {
return "index";
}
}
model.put("index", loginForm);
return "loginsuccess";
}
}
Here is my form:
<form:form action="index.htm" commandName="index">
<table border="0" cellspacing="12">
<tr>
<td>
<spring:message code="application.loginForm.email"/>
</td>
<td>
<form:input path="email"/>
</td>
<td class="error">
<form:errors path="email"/>
</td>
</tr>
<tr>
<td>
<spring:message code="application.loginForm.password"/>
</td>
<td>
<form:password path="password"/>
</td>
<td class="error">
<form:errors path="password"/>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
</form:form>
On form submit I am getting this exception:
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'index' available as request attribute.
What am I doing wrong here?
It's because you're only doing this bit down at the bottom
model.put("index", loginForm);
If you return from the validation error, or from a success, there is no backing object in the model map named "index" so your form tag that has commandName="index" pitches a fit.
A common solution is to simply do this
#ModelAttribute("index")
public LoginForm getLoginForm() {
return new LoginForm();
}
Then there will always be one present, and you can take adding it out of the GET method.

Categories