Dynamic form, with or without multipart/form
I am designing a simple CRUD framework in java, where in my HTML page I have a dynamic form (2 with multipart for create and update with file upload and 1 without fileupload and multipart for delete). Server side, a request modulator checks for all the parameters using request.getParameterMap();
and checks from this hidden type input <input type="hidden" name="returntype" value="Create">
whether its a create, update or delete operation. Based on that it will call necessary handlers.
Note: My form enctype and encoding is set to multipart/form-data Note: My paramMap.size() returns 0 here and returnType is getting null and so I am getting null pointer exception.
If I do not use enctype and encoding at all it runs fine, but then again my file upload gives me an exception that encoding type shoud be multipart/form-data . Can anyone help me in a way that I can have a dynamic form with which I can create a CRUD? or why am I not able to use request.getParameterMap();
with multipart/form-data Thanks :)
Given below is the code of request modulator
public String identifyNow()throws ServletException, java.io.IOException
{
UploadXmlAgent uploadAgent;
paramMap=request.getParameterMap();
if (paramMap == null)
throw new ServletException(
"getParameterMap returned null in: " + getClass().getName());
iterator=paramMap.entrySet().iterator();
System.out.println(paramMap.size());
while(iterator.hasNext())
{
Map.Entry me=(Map.Entry)iterator.next();
if(me.getKey().equals("returntype"))
{
String[] arr=(String[])me.getValue();
returnType=arr[0];
}
}
//Identified based on returnType, instantiate appropriate Handler
if(returnType.equals("Create"))
{
uploadAgent=new UploadXmlAgent(realPath,request,paramMap);
uploadAgent.retrieveXml();
//SOME MORE OPERATIONS
return uploadAgent.uploadXml();
}
else if(returnType.equals("Update"))
{
System.out.println("Update");
uploadAgent=new UploadXmlAgent(realPath,request,paramMap);
uploadAgent.retrieveXml();
//SOME MORE OPERATIONS
return uploadAgent.uploadXml();
}
else if(returnType.equals("Delete"))
{
//SOME OPERATIONS
}
return returnType;
}
As per the comment on the other answer:
can I use request.getParameterMap();
in any way with multipart?
If that is your sole requirement, then just create a Filter
which does the parsing work and prepares the request parameter map with all those parsed multipart items so that you can continue using getParameter()
, getParameterMap()
and consorts the usual way in JSP/Servlet. You can find a complete example of such a filter here.
maybe you should take a look at Commons IO FileUpload.
To make a difference what kind of form is submitted, you can use a hidden input field
<input type="hidden" name="formAction" value="uploadSomething">
Then you can use this in your Servlet to do the action depending on your form
String act = request.getParameter("formAction");
if(act.equals("uploadSomething")
{
// EDIT
if(ServletFileUpload.isMultipartContent(request))
{
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
List /* FileItem */ items = upload.parseRequest(request);
}
}
That's it. Hope this helps.
Hi I managed to work it out. I am using 2 requests and a session variable.
1st request to submit the form without multipart and store the request type in the session variable.
once the 1st request is sent, go for 2nd request of the same form, but this time use multipart and check your session variable value and execute appropriate handler.
链接地址: http://www.djcxy.com/p/64186.html上一篇: 多数据类型表格中的数据