动态表单,带或不带多部分/表单数据

发布于 2024-11-03 20:53:00 字数 1994 浏览 1 评论 0原文

我正在用java设计一个简单的CRUD框架,在我的HTML页面中,我有一个动态表单(2个带有用于创建和更新文件上传的多部分,1个没有文件上传和用于删除的多部分)。服务器端,请求调制器使用 request.getParameterMap(); 检查所有参数,并从此隐藏类型输入进行检查 无论是创建、更新还是删除操作。基于此,它将调用必要的处理程序。

注意:我的表单 enctype 和编码设置为 multipart/form-data 注意:我的 paramMap.size() 在这里返回 0 并且 returnType 正在获取 null,因此我收到了空指针异常。

如果我根本不使用 enctype 和编码,它运行得很好,但是我的文件上传再次给了我一个例外,编码类型应该是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)
        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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清风挽心 2024-11-10 20:53:00

根据另一个答案的评论:

我可以以任何方式将 request.getParameterMap(); 与多部分一起使用吗?

如果这是您的唯一要求,那么只需创建一个 Filter 即可解析工作并使用所有已解析的多部分项准备请求参数映射,以便您可以继续使用 getParameter()getParameterMap() 并在 JSP/ 中按照常规方式进行组合小服务程序。您可以在此处找到此类过滤器的完整示例。

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.

我不是你的备胎 2024-11-10 20:53:00

也许你应该看看 Commons IO FileUpload

为了区分提交的表单类型,您可以使用隐藏的输入字段

 <input type="hidden" name="formAction" value="uploadSomething">

然后您可以在 Servlet 中使用它来根据您的表单执行操作

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);
}
}

就是这样。
希望这有帮助。

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.

安人多梦 2024-11-10 20:53:00

嗨,我设法解决了。
我正在使用 2 个请求和一个会话变量。

第一个请求提交不带多部分的表单并将请求类型存储在会话变量中。

发送第一个请求后,请发送相同形式的第二个请求,但这次使用多部分并检查会话变量值并执行适当的处​​理程序。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文