I want to send data from form to PostgreSQL. When I send data by form, hibernate save (by save() method) blank record .. I did it manually (for test) without using form and then everything is ok.
Spitter.class (entity for user)
#Entity
#Table(name="spitter")
public class Spitter implements Serializable {
private static final long serialVersionUID = 829803238866007413L;
#Id
//#SequenceGenerator(name = "hibernate_sequence")
#GeneratedValue(strategy=GenerationType.AUTO) #Column(name="spitter_id")
private Long id;
#Column(unique=true) #Size(min=3, max=20) #Pattern(regexp = "^[a-zA-Z0-9]+$", message="Nie poprawna nazwa uzytkownika.")
private String username;
#Size(min=5, max=15, message="Haslo musi miec minimum 5 znakow.")
private String password;
#Size(min=3, max=25, message="Blad w imieniu i nazwisku.")
private String fullName;
#OneToMany(mappedBy="spitter")
private List<Spittle> spittles;
#Email(message="Nie poprawny adres email.")
private String email;
private boolean updateByEmail;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public List<Spittle> getSpittles() {
return spittles;
}
public void setSpittles(List<Spittle> spittles) {
this.spittles = spittles;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void setUpdateByEmail(boolean updateByEmail) {
this.updateByEmail = updateByEmail;
}
public boolean isUpdateByEmail() {
return updateByEmail;
}
#Override
public boolean equals(Object obj) {
Spitter other = (Spitter) obj;
return other.fullName.equals(fullName) && other.username.equals(username) && other.password.equals(password);
}
#Override
public int hashCode() {
// TODO Auto-generated method stub
return super.hashCode();
}
}
SpitterController.class
createSpitterProfile - shows form (edit.jsp) and sends model object (spitter) to form
addSpitterFromForm - receives binding data from form and save it to database and redirects to simply user profile
showSpitterProfile - there is of course null model object exception
#Controller
#RequestMapping("/spitters")
public class SpitterController {
private final SpitterService spitterService;
#Inject //#Autowired
public SpitterController(SpitterService spitterService) {
this.spitterService = spitterService;
}
//...
#RequestMapping(method = RequestMethod.GET, params = "new")
public String createSpitterProfile(Model model) {
model.addAttribute("spitter", new Spitter());
return "spitters/edit";
}
#RequestMapping(method = RequestMethod.POST)
public String addSpitterFromForm(#Valid Spitter spitter, BindingResult bindingResult) {
if(bindingResult.hasErrors())
return "spitters/edit";
spitterService.saveSpitter(spitter);
return "redirect:/spitters/" + spitter.getUsername();
}
#RequestMapping(value="/{username}", method = RequestMethod.GET)
public String showSpitterProfile(#PathVariable String username, Model model) {
model.addAttribute(spitterService.getSpitter(username));
return "spitters/view";
}
edit.jsp (registration form for new user (Spitter))
<%# taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>
<%# taglib prefix="s" uri="http://www.springframework.org/tags"%>
<div>
<h2>New account test</h2>
<sf:form method="POST" modelAttribute="spitter"
enctype="multipart/form-data">
<fieldset>
<table>
<tr>
<th><sf:label path="fullName">Full name:</sf:label></th>
<td><sf:input path="fullName" size="15" /><br/>
<sf:errors path="fullName" cssClass="error" />
</td>
</tr>
<tr>
<th><sf:label path="username">Username:</sf:label></th>
<td><sf:input path="username" size="15" maxlength="15" />
<small id="username_msg">No spaces, please.</small><br/>
<sf:errors path="username" cssClass="error" />
</td>
</tr>
<tr>
<th><sf:label path="password">Password:</sf:label></th>
<td><sf:password path="password" size="30"
showPassword="true"/>
<small>6 characters or more (be tricky!)</small><br/>
<sf:errors path="password" cssClass="error" />
</td>
</tr>
<tr>
<th><sf:label path="email">Email Address:</sf:label></th>
<td><sf:input path="email" size="30"/>
<small>In case you forget something</small><br/>
<sf:errors path="email" cssClass="error" />
</td>
</tr>
<tr>
<th></th>
<td>
<sf:checkbox path="updateByEmail"/>
<sf:label path="updateByEmail">Send me email updates!</sf:label>
</td>
</tr>
<tr>
<th></th>
<td>
<input name="commit" type="submit"
value="I accept. Create my account." />
</td>
</tr>
</table>
</fieldset>
</sf:form>
</div>
and blank saved record to Postgres..
Try adding #modelattribute in this method .It fill fetch the required model object.
#RequestMapping(method = RequestMethod.POST)
public String addSpitterFromForm(**#ModelAttribute("spitter")** #Valid Spitter spitter, BindingResult bindingResult) {
if(bindingResult.hasErrors())
return "spitters/edit";
spitterService.saveSpitter(spitter);
return "redirect:/spitters/" + spitter.getUsername();
}
and just to check if it is getting the values from form,syso some values like syso(spitter.getUserName) to check if values are coming.
ALso, I believe that you are making a constructor and passing service to it ,so there is no need of #Inject
#Inject //#Autowired///Why are you injecting it if it is a constructor?
public SpitterController(SpitterService spitterService) {
this.spitterService = spitterService;
}
You have enctype="multipart/form-data in your FORM,
Check that you have something like that in your App-servlet.xml:
<bean id="multipartResolver" class=
"org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:maxUploadSize="500000" />
Related
Basically, Spring crashes with this error "value [null]; codes [NotNull.user.email,NotNull.email,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.email,email]; arguments []; default message [email]]; default message [must not be null], Field error in object 'user' on field 'firstName': rejected value [null]; codes" everytime I try to submit the completeProfile form. So, naturally, I looked at what data was saved after clicking submit on form. It turns out that the User object saved was having all the submitted property from the form as null. I checked inside the controller what was passed inside, all submitted properties from the form were null. Why does this happen?
User.java
#Entity
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#NotNull
#Size(min = 2, max = 80)
private String firstName;
#NotNull
#Size(min= 2, max = 80)
private String lastName;
#NotNull
#Email
private String email;
private Boolean enabled = false;
private String password = "";
private String role = "AUTHOR";
private String location = "";
private String topics = "";
private String job = "";
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getTopics() {
return topics;
}
public void setTopics(String topics) {
this.topics = topics;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
}
completeProfile.html
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>Complete your profile</p>
<form action="#" th:action="#{/completed-profile}" th:object="${user}" method="post">
<p>Email: <p th:text="${user.email}"></p>
<table>
<tr>
<td>Location:</td>
<td><input type="text" th:field="*{location}" /></td>
<td th:if="${#fields.hasErrors('location')}" th:errors="*{location}"></td>
</tr>
<tr>
<td>Job</td>
<td><input type="text" th:field="*{job}" /></td>
<td th:if="${#fields.hasErrors('job')}" th:errors="*{job}"></td>
</tr>
<tr>
<td>Topics of interest</td>
<td><input type="text" th:field="*{topics}" /></td>
<td th:if="${#fields.hasErrors('topics')}" th:errors="*{topics}"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" th:field="*{password}" /></td>
<td th:if="${#fields.hasErrors('password')}" th:errors="*{password}"></td>
<td th:text="${password_error}"></td>
</tr>
<tr>
<td><button type="submit">Submit</button></td>
</tr>
</table>
</form>
</body>
</html>
ProfileController.java
#Controller
public class ProfileController {
UserRepository userRepository;
public ProfileController(UserRepository userRepository) {
this.userRepository = userRepository;
}
#RequestMapping(value = {"/profile", "/profile.html"}, method = RequestMethod.GET)
public String profileForm(){
return "auth/profile";
}
#RequestMapping(value = "/complete-profile", method = RequestMethod.GET)
public String completeProfileForm(){
return "auth/completeProfile";
}
#RequestMapping(value = "/completed-profile", method = RequestMethod.POST)
public String submitProfileForm(#Valid User user, BindingResult bindingResult, Model model){
System.err.println(user.getEmail());
if (user.getPassword() == null || user.getPassword().equals("")){
// model.addAttribute("password_error", "password cannot be null");
model.addAttribute("user", user);
return "auth/completeProfile";
}
if(bindingResult.hasErrors()){
System.out.println(bindingResult.getAllErrors());
return "auth/completeProfile";
}
userRepository.save(user);
return "auth/login";
}
}
//This is my loginController.java
import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
#Controller
public class LoginController
{
#RequestMapping(value="/login.htm", method = RequestMethod.POST)
public String login(#RequestParam(value="userid", required=true) String userid,
#RequestParam(value="password", required=true) String password,
#RequestParam(value="confirmpassword", required=true) String confirmpassword,
#RequestParam(value="role", required=true) String role,
Map<String, Object> model)
{
if(userid.matches("^[a-zA-Z0-9]{5,24}$") && password.matches("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%^&+=])(?=\\S+$).{5,15}$")
&& confirmpassword.matches("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%^&+=])(?=\\S+$).{6,20}$")
&& (role.equals(new String("OPS(Operational)"))||role.equals(new String("Helpdesk"))))
{
model.put("userid", userid);
model.put("password", password);
model.put("confirmpassword", confirmpassword);
model.put("role", role);
System.out.println("successful!");
return "page2";
}
else
{
return "login";
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
{
String userid = request.getParameter("userid");
String password = request.getParameter("password");
String confirmpassword = request.getParameter("confirmpassword");
String role = request.getParameter("role");
try
{
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
catch (ServletException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String userid = request.getParameter("userid");
String password = request.getParameter("password");
String confirmpassword = request.getParameter("confirmpassword");
String role = request.getParameter("role");
request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
}
}
//This is my login.jsp file
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# include file="include.jsp" %>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div align="center" id='formlogin' class="container">
<form method="post" id="loginForm" name="loginForm" action="login.htm">
<table class="tableprop" border="0" width="90%" cellspacing="5" cellpadding="5">
<h3> Add a new user </h3>
<tr>
<td align="center">User ID:</td>
<td><input tabindex="5" size="20" type="text" name="userid" id="userid" value="<%=request.getParameter("userid")!=null?request.getParameter("userid"):""%>"/></td>
</tr>
<tr>
<td align="center">Password:</td>
<td><input tabindex="5" size="20" type="password" name="password" id="password" value="<%=request.getParameter("password")!=null?request.getParameter("password"):""%>"/></td>
</tr>
<tr>
<td align="center">Confirm Password:</td>
<td><input tabindex="5" size="20" type="password" name="confirmpassword" id="confirmpassword" value="<%=request.getParameter("confirmpassword")!=null?request.getParameter("confirmpassword"):""%>"/></td>
</tr>
<tr>
<td align="center">Role:</td>
<td><select name="role" id="role" title="Please select role" tabindex="5" value="<%=request.getParameter("role")!=null?request.getParameter("role"):""%>"/>
<option value="">Select a specific role</option>
<option value="OPS(Operational)">OPS(Operational)</option>
<option value="Helpdesk">Helpdesk</option>
</select></td>
</tr>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<tr>
<td align="center" colspan="4"><input tabindex="7" type="submit" value="Submit" id="submit" class="submit"/></td>
</tr>
<!-- <div id="dialog" title="Dialog Title">I'm in a dialog</div> -->
</table>
</form>
</div>
<script>
// just for the demos, avoids form submit
jQuery.validator.setDefaults({
debug: true,
success: "valid"
});
</script>
</body>
</html>
I have added here 2 files.
first one is loginController.java
and other one is login.jsp
i have done client side validation in jquery.
now i want to display error message on server side validation in loginController.java file which has code for server side validation. and also i want it to be check once whether loginController.java is written correct or not.
You can use Spring Validator interface to build your own custom validator and use spring form tags.
User.java
package com.expertwebindia.beans;
public class User {
private String name;
private String email;
private String address;
private String country;
private String state;
private String city;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
UserValidator.java
package com.expertwebindia.validators;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.expertwebindia.beans.User;
#Component
public class UserValidator implements Validator
{
public boolean supports(Class clazz) {
return User.class.equals(clazz);
}
public void validate(java.lang.Object arg0, Errors arg1) {
ValidationUtils.rejectIfEmptyOrWhitespace(arg1, "name", "name.required", "Name is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(arg1, "email", "Name.required", "Email is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(arg1, "address", "name.required", "Address is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(arg1, "country", "country.required", "Country is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(arg1, "state", "state.required", "State is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(arg1, "city", "city.required", "City is required.");
}
}
In controller you need to the following code to validate your bean.
#RequestMapping(value = "/login", method = RequestMethod.POST)
public String doLogin(#ModelAttribute("userForm") User userForm,
BindingResult result, Map<String, Object> model) {
validator.validate(userForm, result);
System.out.println("Email:"+userForm.getEmail());
if (result.hasErrors()) {
return "register";
}else{
return "success";
}
}
Please find more details about this in link below.
http://www.expertwebindia.com/spring-3-mvc-custom-validator-example/
I am displaying checkboxes on a page and want to only update values in the db for which the values were changed. Also I would like to display an error if nothing was changed and form was submitted.
This is what i have so far. Any suggestion?
View
public class PersonView {
private List<Person> personList;
public List<Person> getPersonList() {
return personList;
}
public void setPersonList(List<Person> personList) {
this.personList = personList;
}
}
Domain
public class Person {
private String fullName;
private Boolean isSupervisor;
private Boolean isManager;
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public Boolean isSupervisor() {
return isSupervisor;
}
public void setSupervisor(Boolean isSupervisor) {
this.isSupervisor = isSupervisor;
}
public Boolean isManager() {
return isManager;
}
public void setManager(Boolean isManager) {
this.isManager = isManager;
}
}
Controller
#Controller
#RequestMapping("/person.html")
public class PersonsController {
#Autowired
private PersonService personService;
#RequestMapping(method = RequestMethod.GET)
public String initForm() {
return "member";
}
#RequestMapping(method = RequestMethod.POST)
public String submitForm(#ModelAttribute PersonView personView) {
model.addAttribute("persons", personView);
return "successMember";
}
#ModelAttribute
public PersonView getPersonView(){
List<Person> persons= personService.getPersonList();
PersonView pv = new PersonView();
pv.setPersonList(persons);
return pv;
}
}
JSP
<html>
<title>Persons Information</title>
</head>
<body>
<form:form method="POST" modelAttribute="personView">
<table>
<tr>
<th>Full Name</th>
<th>Supervisor</th>
<th>Manager</th>
</tr>
<c:forEach var="person" items="${personView.personList}"
varStatus="row">
<tr>
<td>{person.fullName}</td>
<td><form:checkbox path="personList[row.index].supervisor" value="true" />
</td>
<td><form:checkbox path="personList[row.index].manager" value="true" />
</td>
</tr>
</c:forEach>
<tr>
<td><input type="submit" name="submit" value="Submit">
</td>
</tr>
<tr>
</table>
</form:form>
</body>
</html>
I am using Spring MVC and have a form that looks like this : https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-ash3/1526193_10152219199443013_1553859401_n.jpg
On the form there is a button for uploading pictures and when I try to get the file into my controller using an argument I get HTTP-400 status when i press the ok button.
What is the problem and how to fix this issue with files
My jsp code :
<form:form method="post" commandName="editPersonBean" enctype="multipart/form-data">
<form:hidden path="id" />
<table class="myTable">
<c:if test="${editPersonBean.id > 0}">
<tr>
<th>ID</th>
<td>${editPersonBean.id}</td>
</tr>
</c:if>
<tr>
<td>Förnamn</td>
<td><form:input path="firstName" /></td>
</tr>
<tr>
<td>Efternamn</td>
<td><form:input path="lastName" /></td>
</tr>
<tr>
<td>Telefon nummer</td>
<td><form:input path="phoneNumber" /></td>
</tr>
<tr>
<td>E-Mail</td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td>Övrigt</td>
<td><form:input path="otherInfo" /></td>
</tr>
<tr>
<td>Bild</td>
<td><form:label path ="image" />
<input type="file" name="file" />
</td>
</tr>
<br>
<br>
<tr>
<td><c:set var="submitText">
OK
</c:set> <input type="submit" size="20" value="${submitText}" /> </td>
<td></td>
</tr>
</table>
</form:form>
When I am passing in this argument into the controller to catch up the uploaded file its then I get the problem.
#RequestParam("file") MultipartFile file
And the controller I am having problem with looks like the following :
#Controller
#RequestMapping("/editPerson/{id}.html")
public class EditPersonContoller {
#Autowired
PersonService service;
#RequestMapping(method = RequestMethod.GET)
public ModelAndView index(#PathVariable int id) {
EditPersonBean bean = new EditPersonBean();
if (id > 0) {
Person person = service.getPerson(id);
bean.copyValuesToBean(person);
}
ModelAndView mav = new ModelAndView("editPerson");
mav.addObject("editPersonBean", bean);
return mav;
}
#RequestMapping(method = RequestMethod.POST)
public ModelAndView handleSubmit(EditPersonBean bean, BindingResult errors, #RequestParam("file") MultipartFile file) {
if (errors.hasErrors()) {
ModelAndView mav = new ModelAndView("editPerson");
mav.addObject("editPersonBean", bean);
return mav;
}
if (bean.getId() > 0) {
Person person = service.getPerson((int) bean.getId());
bean.copyBeanValuesToPerson(person);
saveImage(person, file);
service.updatePerson(person);
} else {
Person person = new Person();
bean.copyBeanValuesToPerson(person);
saveImage(person, file);
service.createPerson(person);
}
return new ModelAndView("redirect:/person.html");
}
public void saveImage(Person person, #RequestParam("file") MultipartFile file) {
try {
Blob blob = Hibernate.createBlob(file.getInputStream());
person.setImage(blob);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Also I am trying to save the picture using a bean :
public class EditPersonBean {
private int id;
private String firstName;
private String lastName;
private String email;
private String phoneNumber;
private String otherInfo;
private Blob image;
public void copyValuesToBean(Person person){
setId((int) person.getId());
setFirstName(person.getFirstName());
setLastName(person.getLastName());
setEmail(person.getEmail());
setPhoneNumber(person.getPhoneNumber());
setOtherInfo(person.getOtherInfo());
setImage(person.getImage());
}
public void copyBeanValuesToPerson(Person person){
person.setId((int) getId());
person.setFirstName(getFirstName());
person.setLastName(getLastName());
person.setEmail(getEmail());
person.setPhoneNumber(getPhoneNumber());
person.setOtherInfo(getOtherInfo());
person.setImage(image);
}
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getOtherInfo() {
return otherInfo;
}
public void setOtherInfo(String otherInfo) {
this.otherInfo = otherInfo;
}
public Blob getImage() {
return image;
}
public void setImage(Blob image) {
this.image = image;
}
}
And I am trying to save it into the database through a service that just calls the repository :
#Override
public void createPerson(Person person) {
session.getCurrentSession().save(person);
}
And finally it should go down to the model and persist it into the database:
#Column(name = "image")
#Lob
private Blob image;
But no image is being saved into the database and instead the page return an http : 400- Status.
Whats the problem here?
The Spring MVC documentation explains its support for multipart file uploading.
Basically, you need to provide a MultipartResolver in the ApplicationContext that will be available to the DispatcherServlet. You have a few options. A common one is CommonsMultipartResolver, for which you will need Apache's commons-fileupload.jar. The documentation provides the following example
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>
The DispatcherServlet will detect this bean in your ApplicationContext and register it so that it can process requests that have a Content-Type of multipart/.... On each such request, it will wrap the HttpServletRequest in a MultipartHttpServletRequest.
Spring uses a RequestParamMethodArgumentResolver to resolve arguments for parameters annotated with #RequestParam in your #RequestMapping annotated handler methods.
If the parameter is also of type MultipartFile, Spring will cast the HttpServletRequest to a MultipartHttpServletRequest created earlier and use it to retrieve the parts, one of which is your file.
If this configuration is not done, the whole process fails and you likely get a 400 Bad Request.
how you are getting file in the controller which is uploaded by the JSP, put the below method in your controller and debug your code
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
FileUpload file = (FileUpload)command;
MultipartFile multipartFile = file.getFile();
String fileName="";
if(multipartFile!=null){
fileName = multipartFile.getOriginalFilename();
//do whatever you want
}
System.out.println("filename :" + fileName);
return new ModelAndView("FileUploadSuccess","fileName",fileName);
}
I have a table for which I am passing list of student objects from my spring controller method, On page load 3 rows are populated. I want the user to be able to add more rows delete existing rows on button click. Can anyone tell me how to achieve this. See below my controller and jsp code. On clicking add I want to add 3 more rows selecting check box and clicking delete row should delete the row. i want the the added columns to be binded
I am very new to jQuery is this possible without jQuery. If not please tell me in detail how to achieve this using jQuery
Student Entity
#Entity
#Table(name="STUDENT_REGISTRATION")
public class Student {
private int studentId;
private String firstName;
private String lastName;
private Date dob;
private String sex;
private String status;
private Date doj;
private int deptId;
private String deptName;
private int batchId;
private String batchName;
private int roleId;
private String roleName;
private String regNo;
public Student(){
}
public Student(int studentId, String firstName, String lastName, Date dob,
String sex, String status, Date doj, int deptId,
String deptName, int batchId, String batchName, int roleId,
String roleName, String regNo) {
super();
this.studentId = studentId;
this.firstName = firstName;
this.lastName = lastName;
this.dob = dob;
this.sex = sex;
this.status = status;
this.doj = doj;
this.deptId = deptId;
this.deptName = deptName;
this.batchId = batchId;
this.batchName = batchName;
this.roleId = roleId;
this.roleName = roleName;
this.regNo = regNo;
}
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="STUDENT_ID")
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
#Column(name="STUDENT_FIRST_NAME")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
#Column(name="STUDENT_LAST_NAME")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
#Column(name="DOB")
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
#Column(name="SEX")
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
#Column(name="STATUS")
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
#Column(name="DOJ")
public Date getDoj() {
return doj;
}
public void setDoj(Date doj) {
this.doj = doj;
}
#Column(name="DEPT_ID")
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
#Column(name="DEPT_NAME")
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
#Column(name="BATCH_ID")
public int getBatchId() {
return batchId;
}
public void setBatchId(int batchId) {
this.batchId = batchId;
}
#Column(name="BATCH_NAME")
public String getBatchName() {
return batchName;
}
public void setBatchName(String batchName) {
this.batchName = batchName;
}
#Column(name="ROLE_ID")
public int getRoleId() {
return roleId;
}
public void setRoleId(int roleId) {
this.roleId = roleId;
}
#Column(name="ROLE_NAME")
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
#Column(name="REG_NO")
public String getRegNo() {
return regNo;
}
public void setRegNo(String regNo) {
this.regNo = regNo;
}
}
Student DTO
public class StudentDTO {
private int studentId;
private String firstName;
private String lastName;
private Date dob;
private String sex;
private String status;
private Date doj;
private int deptId;
private String deptName;
private int batchId;
private String batchName;
private int roleId;
private String roleName;
boolean select;
private String regNo;
public StudentDTO(){
}
public StudentDTO(int studentId, String firstName, String lastName,
Date dob, String sex, String status, Date doj, int deptId,
String deptName, int batchId, String batchName, int roleId,
String roleName, boolean select, String regNo) {
super();
this.studentId = studentId;
this.firstName = firstName;
this.lastName = lastName;
this.dob = dob;
this.sex = sex;
this.status = status;
this.doj = doj;
this.deptId = deptId;
this.deptName = deptName;
this.batchId = batchId;
this.batchName = batchName;
this.roleId = roleId;
this.roleName = roleName;
this.select = select;
this.regNo = regNo;
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Date getDoj() {
return doj;
}
public void setDoj(Date doj) {
this.doj = doj;
}
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public int getBatchId() {
return batchId;
}
public void setBatchId(int batchId) {
this.batchId = batchId;
}
public String getBatchName() {
return batchName;
}
public void setBatchName(String batchName) {
this.batchName = batchName;
}
public int getRoleId() {
return roleId;
}
public void setRoleId(int roleId) {
this.roleId = roleId;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public boolean isSelect() {
return select;
}
public void setSelect(boolean select) {
this.select = select;
}
public String getRegNo() {
return regNo;
}
public void setRegNo(String regNo) {
this.regNo = regNo;
}
}
Here I am adding 3 Student Objects
public List<StudentDTO> addStudentToList(){
List<StudentDTO> studentList = new ArrayList<StudentDTO>();
StudentDTO stud = new StudentDTO();
for(int i=0; i<3; i++){
studentList.add(stud);
}
return studentList;
}
Student Controller class
#RequestMapping(value="/addStudent", method=RequestMethod.GET)
public ModelAndView getStudentForm(ModelMap model)
{ List<StudentDTO> studentList = studentService.getStudentAttributesList();
//List<Integer> userIdForDropDown = userDAO.getAllUserIdForDropDown();
//model.addAttribute("userIdDropDown",userIdForDropDown);
List<String> deptList = configDAO.getDeptListForDropDown();
model.addAttribute("deptlist",deptList);
List<String> batchList = configDAO.getAllBatchForDropDrown();
model.addAttribute("batchList", batchList);
ModelAndView mav = new ModelAndView("add_student");
Student stu = new Student();
mav.getModelMap().put("add_student", stu);
StudentForm studentForm = new StudentForm();
studentForm.setStudentList(studentList);
model.addAttribute("studentForm",studentForm);
return mav;
}
#RequestMapping(value="/addStudent", method=RequestMethod.POST)
public String saveStudent(#ModelAttribute("add_student") StudentForm studenfForm, BindingResult result, SessionStatus status, ModelMap model) throws ParseException{
/*if(result.hasErrors()){
return "add_student";
}*/
List<StudentDTO> newList = (List<StudentDTO>) studenfForm.getStudentList();
List<Student> newList1 = new ArrayList<Student>();
for(StudentDTO stud:studenfForm.getStudentList()){
Student student = new Student();
student.setBatchId(stud.getBatchId());
student.setBatchName(stud.getBatchName());
student.setDeptId(stud.getDeptId());
student.setDeptName(stud.getDeptName());
SimpleDateFormat sdf = new SimpleDateFormat("DD/MM/YYYY");
Date dateWithoutTime = sdf.parse(sdf.format(new Date()));
student.setDob(stud.getDob());
student.setDoj(stud.getDoj());
student.setFirstName(stud.getFirstName());
student.setLastName(stud.getLastName());
student.setRegNo(stud.getRegNo());
student.setRoleId(stud.getRoleId());
student.setRoleName(stud.getRoleName());
student.setStatus(stud.getStatus());
student.setSex(stud.getSex());
student.setStudentId(stud.getStudentId());
newList1.add(student);
}
Integer saveStatus = studentDAO.saveStudentInfo(newList1);
//Integer res = roleDAO.saveRole(role);
if(saveStatus!=null){
status.setComplete();
model.addAttribute("savedMsg", "Student record saved Successfully.");
}
return "redirect:addStudent";
}
See my jsp page
<table bgcolor="white" width="1200" height="300" align="center" style="border-collapse: collapse;" border="1" bordercolor="#006699" >
<form:form action="addStudent" method="post" commandName="add_student" modelAttribute="studentForm">
<tr>
<td align="center" style="background-color: lightblue"><h3>Add Student</h3></td>
</tr>
<tr align="left">
<td align="left">
<input type="button" id="addrows" name="addrows" class="addperson" value="Add Rows">
<input type="button" id="removerows" class="removerows" value="Delete Rows" />
<input type="submit" value="Save" />
</td>
</tr>
<tr valign="middle" align="center">
<td align="center" valign="middle">
<table width="1200" height="200" style="border-collapse: collapse;" border="0" bordercolor="#006699" cellspacing="0" cellpadding="0">
<thead>
<tr height="1" bgcolor="lightblue">
<th colspan="1">
No
</th>
<th width="5">
Select
</th>
<th>
Reg No
</th>
<th>
First Name
</th>
<th>
Last Name
</th>
<th>
Sex
</th>
<th>
DOB
</th>
<th>
DOJ
</th>
<th>
Dept Name
</th>
<th>
Role Name
</th>
<th>
Batch Name
</th>
<th>
Status
</th>
</tr>
</thead>
<tbody>
<c:forEach var="rows" items="${studentForm.studentList}" varStatus="status">
<tr class="${status.index % 2 == 0 ? 'even' : 'odd'}" >
<td width="15">
<b>${status.count}</b>
</td>
<td width="10">
<form:checkbox path="studentList[${status.index}].select"/>
</td>
<td><form:input path="studentList[${status.index}].regNo"/></td>
<td><form:input path="studentList[${status.index}].firstName"/></td>
<td><form:input path="studentList[${status.index}].lastName"/></td>
<td><form:select path="studentList[${status.index}].sex">
<form:option value="NONE" label="--- Select ---"/>
<form:option value="M" label="Male"/>
<form:option value="F" label="Female"/>
</form:select></td>
<td><form:input path="studentList[${status.index}].dob"/></td>
<td><form:input path="studentList[${status.index}].doj"/></td>
<td><form:select path="studentList[${status.index}].deptName">
<form:option value="NONE" label="--- Select ---"/>
<form:options items="${deptlist}" />
</form:select></td>
<td><form:select path="studentList[${status.index}].roleName">
<form:option value="NONE" label="--- Select ---"/>
<form:option value="ROLE_STUDENT" label="Student"/>
<form:option value="ROLE_BATCHREP" label="Batch Rep"/>
</form:select></td>
<td><form:select path="studentList[${status.index}].batchName">
<form:option value="NONE" label="--- Select ---"/>
<form:options items="${batchList}" />
</form:select>
</td>
<td><form:select path="studentList[${status.index}].status">
<form:option value="NONE" label="--- Select ---"/>
<form:option value="E" label="Enable"/>
<form:option value="D" label="Disable"/>
</form:select></td>
</tr>
</c:forEach>
</tbody>
</table>
</td>
</tr>
<tr align="center">
<td width="100" align="center"><B>
${savedMsg}
</B>
</td>
</tr>
</form:form>
</table>
Even though this thread is older, For the benefit of others who is in need of this.
Let me explain with a minimal code and User example with firstName, email, userName and gender fields so that people won't get confused with bigger code.
Consider you are sending 3 empty users in usersList from controller this will creates 3 empty rows. If you inspect/view page source you will see
Rows(<input> tags) with different id's like list0.firstName
list1.firstName
Rows(<input> tags) with different names like list[0].firstName
list[1].firstName
Whenever form is submitted id's are not considered by server(added for only for helping client side validations), but name attribute will be interpreted as request parameter and are used to construct your modelAttribute, hence attribute names are very important while inserting rows.
Adding row
So, How to construct/append new rows?
If i submit 6 users from UI, controller should receive 6 user object from usersList. Steps to achieve the same is given below
1. Right click -> view page source. You will see rows like this(you can see *[0].* in first row and *[1].* in second row)
<tr>
<td><input id="list0.firstName" name="list[0].firstName" type="text" value=""/></td>
<td><input id="list0.email" name="list[0].email" type="text" value=""/></td>
<td><input id="list0.userName" name="list[0].userName" type="text" value=""/></td>
<td>
<span>
<input id="list0.gender1" name="list[0].gender" type="radio" value="MALE" checked="checked"/>Male
</span>
<span>
<input id="list0.gender2" name="list[0].gender" type="radio" value="FEMALE"/>Female
</span>
</td>
</tr>
<tr>
<td><input id="list1.firstName" name="list[1].firstName" type="text" value=""/></td>
<td><input id="list1.email" name="list[1].email" type="text" value=""/></td>
<td><input id="list1.userName" name="list[1].userName" type="text" value=""/></td>
<td>
<span>
<input id="list1.gender1" name="list[1].gender" type="radio" value="MALE" checked="checked"/>Male
</span>
<span>
<input id="list1.gender2" name="list[1].gender" type="radio" value="FEMALE"/>Female
</span>
</td>
</tr>
Copy first row and construct a javascript string and replace '0' with variable name index. As given in below sample
'<tr>'+
'<td><input id="list'+ index +'.firstName" name="list['+ index +'].firstName" type="text" value=""/></td>'+
'<td><input id="list'+ index +'.email" name="list['+ index +'].email" type="text" value=""/></td>'+
...
'</tr>';
Append the constructed row to the <tbody>. Rows get added in UI also on submission of form newly added rows will be received in controller.
Deleting row
Deleting row is little bit complicated, i will try to explain in easiest way
Suppose you added row0, row1, row2, row3, row4, row5
Deleted row2, row3. Do not just hide the row but remove it from the
DOM by catching event.
Now row0,row1,row4,row5 will get submitted but in the controller your
userList will have 6 user object but user[2].firstName will be null
and user[3].firstName will be null.
So in your controller iterate and check for null and remove the
user.(Use iterator don't use foreach to remove user object)
Posting code to benefit beginners.
// In Controller
#RequestMapping(value = "/app/admin/add-users", method = RequestMethod.GET)
public String addUsers(Model model, HttpServletRequest request)
{
List<DbUserDetails> usersList = new ArrayList<>();
ListWrapper userListWrapper = new ListWrapper();
userListWrapper.setList(usersList);
DbUserDetails user;
for(int i=0; i<3;i++)
{
user = new DbUserDetails();
user.setGender("MALE"); //Initialization of Radio button/ Checkboxes/ Dropdowns
usersList.add(user);
}
model.addAttribute("userListWrapper", userListWrapper);
model.addAttribute("roleList", roleList);
return "add-users";
}
#RequestMapping(value = "/app/admin/add-users", method = RequestMethod.POST)
public String saveUsers(#ModelAttribute("userListWrapper") ListWrapper userListWrapper, Model model, HttpServletRequest request)
{
List<DbUserDetails> usersList = userListWrapper.getList();
Iterator<DbUserDetails> itr = usersList.iterator();
while(itr.hasNext())
{
if(itr.next().getFirstName() == null)
{
itr.remove();
}
}
userListWrapper.getList().forEach(user -> {
System.out.println(user.getFirstName());
});
return "add-users";
}
//POJO
#Entity
#Table(name = "userdetails")
#XmlRootElement(name = "user")
public class DbUserDetails implements Serializable
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String firstName;
private String userName;
private String email;
private String gender;
//setters and getters
}
//list wrapper
public class ListWrapper
{
private List<DbUserDetails> list;
//setters and getters
}
In JSP
<form:form method="post" action="${pageContext.request.contextPath}/app/admin/add-users" modelAttribute="userListWrapper">
<table class="table table-bordered">
<thead>
<tr>
<th><spring:message code="app.userform.firstname.label"/></th>
<th><spring:message code="app.userform.email.label"/></th>
<th><spring:message code="app.userform.username.label"/></th>
<th><spring:message code="app.userform.gender.label"/></th>
</tr>
</thead>
<tbody id="tbodyContainer">
<c:forEach items="${userListWrapper.list}" var="user" varStatus="loop">
<tr>
<td><form:input path="list[${loop.index}].firstName" /></td>
<td><form:input path="list[${loop.index}].email" /></td>
<td><form:input path="list[${loop.index}].userName" /></td>
<td>
<span>
<form:radiobutton path="list[${loop.index}].gender" value="MALE" /><spring:message code="app.userform.gender.male.label"/>
</span>
<span>
<form:radiobutton path="list[${loop.index}].gender" value="FEMALE" /><spring:message code="app.userform.gender.female.label"/>
</span>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<div class="offset-11 col-md-1">
<button type="submit" class="btn btn-primary">SAVE ALL</button>
</div>
</form:form>
Javascript needs to be included in JSP
var currentIndex = 3; //equals to initialRow (Rows shown on page load)
function addRow()
{
var rowConstructed = constructRow(currentIndex++);
$("#tbodyContainer").append(rowConstructed);
}
function constructRow(index)
{
return '<tr>'+
'<td><input id="list'+ index +'.firstName" name="list['+ index +'].firstName" type="text" value=""/></td>'+
'<td><input id="list'+ index +'.email" name="list['+ index +'].email" type="text" value=""/></td>'+
'<td><input id="list'+ index +'.userName" name="list['+ index +'].userName" type="text" value=""/></td>'+
'<td>'+
'<span>'+
'<input id="list'+ index +'.gender1" name="list['+ index +'].gender" type="radio" value="MALE" checked="checked"/>Male'+
'</span>'+
'<span>'+
'<input id="list'+ index +'.gender'+ index +'" name="list['+ index +'].gender" type="radio" value="FEMALE"/>Female'+
'</span>'+
'</td>'+
'</tr>';
}