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

Categories

HOME
gitlab
debugging
appx
relayjs
kalman-filter
cvs
cmd
sqlite3
opengl-es-2.0
cross-validation
add
synchronization
facebook-messenger-bot
dacpac
google-translate
kibana-4
metatrader4
imacros
rascal
quickbooks
nstableview
etl
fortumo
clojurescript
tostring
normalizr
openedx
jtextfield
swiftlint
kvc
sparse-matrix
facebook-access-token
intel-pin
bootstrap-material-design
minitab
assistant
elasticsearch-net
librato
unboundid
catch-all
atl
amazon-kinesis-kpl
ios5
vxworks
main
impersonation
document.write
leading-zero
pango
errordocument
promela
pnotify
hls.js
np-complete
redundancy
gnome-shell-extensions
setuptools
android-fonts
elements
measures
django-debug-toolbar
nsviewcontroller
python-stackless
fadeout
oauth2client
pyke
ford-fulkerson
intellij-14
qdialog
quicklisp
tableau-online
interrupted-exception
unity5.2.3
sonarqube5.1.2
pretty-print
jscript.net
cdt
rdtsc
nsbutton
terminfo
project-planning
has-many-through
p4java
wp-query
java.nio.file
jmapviewer
dbconnection
flashvars
dsn
punbb
nsobject
external-accessory
blackberry-playbook
cinema-4d
javax.script
datareader
h.323
jquery-ui-droppable
audio-capture
javap
project-hosting

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile