多部分形式:如何在 JSP 中的隐藏输入中发送和获取参数

Multipart form: How to send and get parameters in an hidden input in JSP

我有一个包含几个字段的多部分表单,我需要将这些值发送到 jsp 文件。

这是多部分形式,这是我发送参数的方式(在非多部分中,它工作得很好)

<div class="createform">
        <form enctype="multipart/form-data" name="create" action="createcompetition.jsp" method="post">
            <ul>
                <li>
                    <label for="name">Competition Name</label>
                    <input type="text" id="name" name="name" placeholder="name" required>
                </li>
                <li>
                    <label for="img">Picture</label>
                    <input type="file" name="img" id ="img" multiple>
                </li>
                <li>
                    <label for="startdate">Start Date</label>
                    <input type="date" id="startdate" name="startdate">
                </li>
                <li>
                    <label for="deadline">Deadline</label>
                    <input type="date" id="deadline" name="deadline">
                </li>
                <li>
                    <label for="website">Website URL</label>
                    <input type="url" id="website" name="website">
                </li>
                <li>
                    <label for="termsnconds">Terms &amp; Conds</label>
                    <textarea id="termsnconds" name="termsnconds" cols="28" rows="5" ></textarea>
                </li>
                <li>
                    <input type="submit" value="Create">
                    <input type="hidden" name="competiname" id="competiname" value="document.getElementById('name').value;" />
                    <input type="hidden" name="competistartdate" id="competistartdate" value="document.getElementById('startdate').value;" />
                    <input type="hidden" name="competideadline" id="competideadline" value="document.getElementById('deadline').value;" />
                    <input type="hidden" name="competiwebsite" id="competiwebsite" value="document.getElementById('website').value;" />
                    <input type="hidden" name="competitermsnconds" id="competitermsnconds" value="document.getElementById('termsnconds').value;" />
                </li>
            </ul>


        </form>
    </div>

这是 JSP 页面,必须接收这些参数才能在第二个 INSERT INTO 中使用它们。

<%
        //Create a factory for disk-based file items
        DiskFileItemFactory factory = new DiskFileItemFactory();

        //Configure a repository (to ensure a secure temp location is used)
        ServletContext servletContext = this.getServletConfig().getServletContext();
        File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
        factory.setRepository(repository);

        //Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload(factory);

        //Parse the request
        List<FileItem> items = upload.parseRequest(request);
        Iterator<FileItem> iter = items.iterator();
        while (iter.hasNext()) {
            FileItem item = iter.next();

            if (item.isFormField()) {
                //rocessFormField(item);
            } else {
                item.write(new File("C:\indigo\eclipse\workspace\JSPyDB\WebContent\images\competitions\salida.png"));
            }
        }
        //File.createTempFile(arg0, arg1)
    %>

    <sql:transaction dataSource="jdbc/PuzzleDB">
        <sql:update>
            INSERT INTO picture_competition (name, url) VALUES (?, ?)
            <sql:param value="salida.png" />
            <sql:param value="/competitions/" />                
        </sql:update>
        <sql:query var="pictures">
            SELECT id FROM picture_competition WHERE name=? 
            <sql:param value="salida.png" />
        </sql:query>
        <c:if test="${!empty pictures.rows}">
            OK
            <c:forEach var="picture" items="${pictures.rows}">
                <sql:update>
                    INSERT INTO Competition (name, id_picture, id_sponsor, start_date, deadline, websiteURL, termsnconds) VALUES (?, ?, ?, ?, ?, ?, ?);
                    <sql:param value="${param.competiname}" />
                    <sql:param value="${picture.id}" /> 
                    <sql:param value="${sessionScope.userid}" /> 
                    <sql:param value="${param.competistartdate}" />
                    <sql:param value="${param.competideadline}" />
                    <sql:param value="${param.competiwebsite}" />
                    <sql:param value="${param.competitermsnconds}" />
                </sql:update>   
            </c:forEach>
        </c:if> 
    </sql:transaction>

但是参数 ${param.name}null,还有其他参数。

我做错了什么?

在 JSP

中使用 MultipartData 时,您可以执行以下操作 1) 声明 web.xml webapp 版本 3.0
2)使用@MultiPartConfig
配置你的控制器 3) 将 JSP 中的 Java 代码移动到控制器 servlet
4)如果你想在 servlet 中得到那个部分,做类似

的事情
Part mypart=request.getPart("Your Name Here")
mypart.getInputStream();

通过这种方式,您可以从 JSP 访问部分数据到 servlet。

如果您使用的不是 servlet 3.0 及更高版本,您可以使用 apache commonsfileupload

我尝试使用 getPart(),但没有用。我去了 oracle documentation,它说我应该对上传的文件使用 getPart(),但对其他输入字段使用 request.getParameter()。 有效! 我添加了这段代码:

final String name = request.getParameter("name");
        final String startdate = request.getParameter("startdate");
        final String deadline = request.getParameter("deadline");
        final String website = request.getParameter("website");
        final String termsnconds = request.getParameter("termsnconds");

并将此用于在 INSERT TO

中插入新比赛的查询
<c:forEach var="picture" items="${pictures.rows}">
                <c:set var="competiname" value="<%= name %>"></c:set>
                <c:set var="competistartdate" value="<%= startdate %>"></c:set>
                <c:set var="competideadline" value="<%= deadline %>"></c:set>
                <c:set var="competiwebsite" value="<%= website %>"></c:set>
                <c:set var="competitermsnconds" value="<%= termsnconds %>"></c:set>
                <sql:update>
                    INSERT INTO Competition (name, id_picture, id_sponsor, start_date, deadline, websiteURL, termsnconds) VALUES (?, ?, ?, ?, ?, ?, ?);
                    <sql:param value="${competiname}" />
                    <sql:param value="${picture.id}" /> 
                    <sql:param value="${sessionScope.userid}" /> 
                    <sql:param value="${competistartdate}" />
                    <sql:param value="${competideadline}" />
                    <sql:param value="${competiwebsite}" />
                    <sql:param value="${competitermsnconds}" />
                </sql:update>   
            </c:forEach>

但我遇到了一个新问题,type="date" return 的输入为空...我将检查一下,看看我是否能得到一些东西。

我找到了一种方法,现在完美运行了。

显然,当我执行 upload.parseRequest 时,multipart-form 的所有参数都在里面,但我只获取了上传的文件。

为了获取其余参数,我必须首先将它们插入到 Iterator 对象中,然后通过表单中先前给定的名称获取它们中的每一个。

<%
    String name = null ;
    String startdate = null;
    String deadline = null;
    String website = null;
    String termsnconds = null;
    //Create a factory for disk-based file items
    DiskFileItemFactory factory = new DiskFileItemFactory();
    //Configure a repository (to ensure a secure temp location is used)
    ServletContext servletContext = this.getServletConfig().getServletContext();
    File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
    factory.setRepository(repository);
    //Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);
    //Parse the request
    List<FileItem> items = upload.parseRequest(request);
    Iterator<FileItem> iter = items.iterator();
    while (iter.hasNext()) {
        FileItem item = iter.next();
        if (item.isFormField()) {
            InputStream input = item.getInputStream();
            if(item.getFieldName().equals("name")){
                byte[] str = new byte[input.available()];
                input.read(str);
                name = new String(str,"UTF8");
            }
            if(item.getFieldName().equals("startdate")){
                byte[] str = new byte[input.available()];
                input.read(str);
                startdate = new String(str,"UTF8");
            }
            if(item.getFieldName().equals("deadline")){
                byte[] str = new byte[input.available()];
                input.read(str);
                deadline = new String(str,"UTF8");
            }
            if(item.getFieldName().equals("website")){
                byte[] str = new byte[input.available()];
                input.read(str);
                website = new String(str,"UTF8");
            }
            if(item.getFieldName().equals("termsnconds")){
                byte[] str = new byte[input.available()];
                input.read(str);
                termsnconds = new String(str,"UTF8");
            }
        } else {
                item.write(new File("C:\indigo\eclipse\workspace\JSPyDB\WebContent\images\competitions\salida.png"));
        }
    }
        //File.createTempFile(arg0, arg1)


    %>
    <sql:transaction dataSource="jdbc/PuzzleDB">
        <sql:update>
            INSERT INTO picture_competition (name, url) VALUES (?, ?)
            <sql:param value="salida.png" />
            <sql:param value="/competitions/" />                
        </sql:update>
        <sql:query var="pictures">
            SELECT id FROM picture_competition WHERE name=? 
            <sql:param value="salida.png" />
        </sql:query>
        <c:if test="${!empty pictures.rows}">
            <c:forEach var="picture" items="${pictures.rows}">
                <c:set var="competiname" value="<%= name %>"></c:set>
                <c:set var="competistartdate" value="<%= startdate %>"></c:set>
                <c:set var="competideadline" value="<%= deadline %>"></c:set>
                <c:set var="competiwebsite" value="<%= website %>"></c:set>
                <c:set var="competitermsnconds" value="<%= termsnconds %>"></c:set>
                <sql:update>
                    INSERT INTO Competition (name, id_picture, id_sponsor, start_date, deadline, websiteURL, termsnconds) VALUES (?, ?, ?, ?, ?, ?, ?);
                    <sql:param value="${competiname}" />
                    <sql:param value="${picture.id}" /> 
                    <sql:param value="${sessionScope.userid}" /> 
                    <sql:param value="${competistartdate}" />
                    <sql:param value="${competideadline}" />
                    <sql:param value="${competiwebsite}" />
                    <sql:param value="${competitermsnconds}" />
                </sql:update>
            </c:forEach>
        </c:if> 
    </sql:transaction>