动态表单,有或没有多部分/表单数据 [英] Dynamic form, with or without multipart/form-data

查看:91
本文介绍了动态表单,有或没有多部分/表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用java设计一个简单的CRUD框架,在我的HTML页面中,我有一个动态表单(2与多部分创建和更新与文件上传和1没有文件上传和多部分删除)。在服务器端,请求调节器使用 request.getParameterMap(); 检查所有参数,并从此隐藏类型输入< input type =隐藏name =returntypevalue =创建> 是否创建,更新或删除操作。注意:我的表单enctype和编码设置为 multipart / form-data / strong>
注意:我的paramMap.size()在这里返回0,returnType变为null,所以我得到空指针异常。

如果我根本不使用enctype和encoding,它运行良好,但是我的文件上传又给我一个例外,即编码类型应该是 multipart / form-data 。任何人都可以帮助我,我可以创建一个可以创建CRUD的动态表单吗?或为什么我无法使用 request.getParameterMap(); multipart / form-data 感谢:)

以下是请求调制器的代码

  public String identifyNow()throws ServletException,java。 io.IOException 
{
UploadXmlAgent uploadAgent;
paramMap = request.getParameterMap();
if(paramMap == null)
抛出新的ServletException(
getParameterMap返回null:+ 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];



//基于returnType识别,实例化相应的Handler

if(returnType.equals(Create))
{
uploadAgent = new UploadXmlAgent(realPath,request,paramMap);
uploadAgent.retrieveXml();
//更多操作
return uploadAgent.uploadXml();
}
else if(returnType.equals(Update))
{
System.out.println(Update);
uploadAgent = new UploadXmlAgent(realPath,request,paramMap);
uploadAgent.retrieveXml();
//更多操作
return uploadAgent.uploadXml();
}
else if(returnType.equals(Delete))
{
//一些操作
}
return returnType;
}


解决方案

其他答案:


可以使用 request.getParameterMap(); 以任何方式与multipart?


如果这是您的唯一要求,那么只需创建 Filter ,它执行解析工作,并准备好请求参数映射以及所有已解析的多部分项目,以便您可以继续使用 getParameter() getParameterMap()并配合JSP / Servlet中的常用方式。您可以在此处找到此类过滤器的完整示例。


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.

这篇关于动态表单,有或没有多部分/表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆