I have a form which contains fields and file upload elements while submitting the form it is throwing a null pointer exception, when i logged the form object all fields are getting null and when i am removing the form enctype="multipart/form-data"1 then i get all fields but the file object is getting null.
Form Code :
<form:form method="post" id="form" name="frm" action="${action}" enctype="multipart/form-data">
<table>
<tr>
<td><form:label path="productName">Product Name: </form:label></td>
<td>
<form:input path="productName"/>
</td>
</tr>
<tr>
<td><form:label path="rfile">Receipt File</form:label></td>
<td><form:input path="rfile" id="receiptFile" type="file" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Upload Receipt" /></td>
</tr>
</table>
</form:form>
COntroller Code:
#RequestMapping(value="/test.do", method = RequestMethod.POST)
public ModelAndView testReceipt(#ModelAttribute("frm") ReceiptForm form, BindingResult result, HttpServletRequest request){
System.out.println("---"+form.getProductName());
System.out.println("---"+form.getRfile());
}
Please note file is a type of: CommonsMultipartFile
Use FileUpload library to parse the request from the client.
Related
I`m trying to send post request from JSP view to Controller by Button "onClick" method but i getting 404 Error that the RequestMapping is not sign, why is that?
HomeController:
#RequestMapping(value = "/showSelectedRequest/{id}", method = RequestMethod.POST)
public String loadRequestProducts(#PathVariable("id") int id, Model model) {
logger.debug("HomeController.RequestIdSelected() - Start");
logger.debug("HomeController.RequestIdSelected: id: " + id);
model.addAttribute("RequestIdSelected", id);
logger.debug("HomeController.RequestIdSelected() - Done");
return "/home";
}
Home.jsp:
<form action="${contextPath}/requestlist" method="post">
<table class="table table-sm">
<thead class="thead-inverse">
<tr>
<th>
Id
</th>
<th>
Name
</th>
<th>
Show request
</th>
</tr>
</thead>
<c:forEach items="${requestDTOList}" var="requestDTO">
<tr>
<td>
${requestDTO.getId()}
</td>
<td>
${requestDTO.getName()}
</td>
<td>
<button class="btn btn-info" onclick="post(/showSelectedRequest/${requestDTO.getId()})">Query</button>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</td>
</tr>
</c:forEach>
</table>
</form>
When you have a form, the action field is what will be executed when you will click on the input of type "submit".
As a solution you can modify your code as follow:
<form action="${contextPath}/showSelectedRequest/${requestDTO.getId()}" method="post">
// Form elements ...
<input type="submit" value="Query" />
</form>
I have a simple in my jsp with some input fields. It works perfectly fine in most browser and not so much in IE.
When the form is used in IE only the first submit will work. Every submit after that "fails" as some of the parameters from the form are missing. This is NOT an issue on Chrome or Firefox. If I reload the page between each submit it works fine on IE as well, but this is not an option.
form:
<form id="form1" method="post" action="receiver.htm">
<table>
<tr id="name">
<td>Name</td>
<td><input id="input_name" name="name"/></td>
</tr>
<tr>
<td>Amount</td>
<td><input id="amount" name="amount" type="text"/></td>
</tr>
<tr>
<td>Unit</td>
<td><input id="unit" name="unit" type="text"/></td>
</tr>
<tr>
<td colspan="2"><input id="submit_button" type="submit" value="submit"/></div>
</td>
</tr>
</table>
</form>
Controller:
#RequestMapping(value = "/receiver.htm", method = RequestMethod.POST)
#ResponseBody public String receive(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
String name = httpServletRequest.getParameter("name");
String amount = httpServletRequest.getParameter("amount");
UnitType unit = UnitType.getValue(httpServletRequest.getParameter("unit"));
}
UnitType is an enum I made. And it throws the following exception after 2nd submit with IE as some of the parameters return as null:
java.lang.NullPointerException: Name is null
I am trying to submit a spring form via ajax but the values within the form are always coming across as null (The actual form is not null).
This is how I send the form from the JSP:
$('#fSystemSave').click(function() {
$.post("/live-application/systemSave", {systemForm: $("#systemForm").serialize()}, displayModifiedSystemResults, "html");
});
This is the form:
<form:form id="systemForm"
method="post" action="/live-application/systemSave"
commandName="systemForm">
<tr>
<td colspan="1" align="left">
<strong>SYSTEM SETTINGS</strong>
</td>
</tr>
<tr>
<td valign="top">Name: </td>
<td valign="top"><form:input path="fName" size="20" /> </td>
</tr>
<tr>
<td valign="top" align="left">
<button type="button" id="fSystemSave">Save</button>
</td>
</tr>
</form:form>
And this is the Java controller side:
#RequestMapping(value = "/systemSave", method = RequestMethod.POST)
public void saveSystem(#ModelAttribute("systemForm") SystemForm systemForm, OutputStream outputStream) throws IOException {
logger.info("Save System1: " + systemForm);
logger.info("Save System2: " + systemForm.getfName());
}
So 'Save System1' returns an Object tostring and 'Save System2' returns null. Can anyone spot what I am doing wrong?
Thanks,
I don't know Spring but I think {systemForm: $("#systemForm").serialize()} should just be $("#systemForm").serialize().
the complete code is in github:
https://github.com/cuipengfei/MPJSP/tree/master/tmp
in the controller, there is a method that handles the submit:
#RequestMapping(value = "/home", method = RequestMethod.POST)
public void handleSubmit(Customer model, BindingResult result) {
System.out.println(model.getUserName());
result.rejectValue("userName", "required.userName", "user name invalid");
}
and in jsp, there is a form like this:
<form:form method="POST" action="home" modelAttribute="Customer">
<table>
<tr>
<td>Username :</td>
<td><form:input path="userName" /></td>
<td><form:errors path="userName" cssClass="error" /></td>
</tr>
<tr>
<td colspan="3"><input type="submit" /></td>
</tr>
</table>
</form:form>
the controllers just rejects value every time, but the error message is not displayed.
the complete code can be found here:
https://github.com/cuipengfei/MPJSP/tree/master/tmp
try to set commandName attribute in your form tag
<form:form method="POST" action="home" commandName="Customer">
I have a login page with form action of j_security_check. As of now this form just have two fields namely username
and password. I want to add a new dropdown to this form and collect the selected value in controller using
#RequestParam. For some reason I am not able to pass this dropdown value from JSP to my controller as its throwing the
exception: MissingServletRequestParameterException (Which occurs anytime a request param is missing).
In the code below I added the Visuals dropdown. Do I need to use Spring:Bind tag here?
Also on successful login, the control is directed to a controller with request mapping /controller1.html and this is where
I am trying to collect the dropdown value.
<form name="appLogin" action="j_security_check" method="POST">
<table width="100%">
<tr>
<td align="center">
<table>
<tr>
<td>Username: </td>
<td><input id="userName" name="j_username" value=""/></td>
</tr>
<tr>
<td>Password: </td>
<td><input name="j_password" type="password" value="" /></td>
</tr>
<tr>
<td>Visual: </td>
<td><Select name="visuals" id="visuals"/>
<option value="S1">S1</option>
<option value="S2">S2</option>
<option value="S3">S3</option>
<option value="S4">S4</option>
</Select>
</td>
</tr>
</table>
<table>
<tr>
<td>
<button type="submit" name="submit" value="Sign In">Sign In</button>
<input type="submit"/>
</td>
</tr>
</table>
</div>
</div>
</td>
</tr>
</table>
</form>
Controller Code:
#RequestMapping( value = " /controller1.html", method = RequestMethod.GET )
public String setupForm( #RequestParam(value = "visuals", required=false) String visuals,
ModelMap model )
{
List<String> studentNames = new ArrayList<String>();
List<String> teacherNames = new ArrayList<String>();
model.addAttribute("someData", teacherNames);
model.addAttribute("anotherData", studentNames);
model.addAttribute("visuals", visuals);
log.info("Role from Dropdown: " + visuals);
return "school/classTen";
}
You need to create yyour own Filter by extending AbstractAuthenticationProcessingFilter
I don't have the entire code in front of my eyes, but the following article could help you:
http://mark.koli.ch/2010/07/spring-3-and-spring-security-setting-your-own-custom-j-spring-security-check-filter-processes-url.html