java
When the argument is null as a parameter
The method as a controller in Java that I have written gets an File typed argument like below. #RequestMapping(value = "/user/insertUser") public String insertUser(#RequestParam("imageFile") CommonsMultipartFile file, HttpSession session) throws Exception { ......} What my problem is that when there is no File argument sent from a JSP , how I can handle it. I am not sure if my explain was understandable though.
You can set defaultValue to handle null case: #RequestMapping(value = "/user/insertUser") public String insertUser(#RequestParam(value = "imageFile", required = false, defaultValue = "/default/path/to/file") CommonsMultipartFile file, HttpSession session) throws Exception { ......}
I believe you are trying to make it as optional you can do #RequestParam(value = "imageFile", required = false)
You need to give required = false for imageFile #RequestMapping(value = "/user/insertUser") public String insertUser(#RequestParam("imageFile", required = false) CommonsMultipartFile file,HttpSession session) throws Exception { ......}
You can do it as follows: #RequestMapping(value = "/user/insertUser") public String insertUser(#RequestParam("imageFile") CommonsMultipartFile file, HttpSession session) throws Exception { if(file!=null && !file.isEmpty()) { //file is uploaded successfully } else { //file is not uploaded } }
If your question is whether you can provide an optional parameter as request param, you can do that by simply changing your #RequestParam to below. #RequestParam(value = "imageFile", required = false) CommonsMultipartFile file If you are using Java 8 you can also do the below. #RequestParam("imageFile") Optional<CommonsMultipartFile> file If your question is not this then please provide more details.
Here you can do this parameter as required = false so that you have option to sand this parameter or not. { #RequestMapping(value = "/user/insertUser") public String insertUser(#RequestParam(value="imageFile",required=false) CommonsMultipartFile file, HttpSession session) throws Exception { ......} } using this, if you send "imageFile" then you will get on server, else you have not send "imageFile" then you will get it null.
Related Links
JAXB cannot marshall and unmarshall Opensaml and Openws objects with non-arg constructors?
hibernate: Problem running queries / detached criterias on embedded object
How do I notify a MATLAB object of an event in a Java object?
how can one access window object of a webpage
Get content/sting from an URL
What's the best recourse when hibernate/HQL is too limited?
sorting 50 000 000 numbers
How can I get Eclipse work with Maven and SVN correctly?
JPA concurrent transactions
page refreshing regarding
Can a thread be interrupted in the middle of run()'s synchronized block?
Override log4j.properties
Android SQLite3 problem enforcing primary key
Unable to connect to my mail Server (java)
Deserialization of arrays with Jackson
Script tags in body do not get evaluated after previous JavaScript errors