【问题标题】:Not getting Text Area value with file upload JSP and Servet 2.5没有通过文件上传 JSP 和 Servlet 2.5 获取 Textarea 值
【发布时间】:2013-12-02 11:19:03
【问题描述】:

我正在尝试在 JSP 表单中提交包含文本字段、文本区域、文件字段等的表单。我正在为此表单使用公共文件上传。

这是我的 JSP 表单:

<form name="add_product_form" id="add_product_form" enctype="multipart/form-data" method="post" action="Product.Add">
    <div id="form-body">
        <div id="lebel">
            <font color="red">*</font>Product Name:
        </div>
        <div id="field">
            <input type="text" name="product_name" id="product_name" value="">
        </div>
        <div id="lebel">
             <font color="red">*</font>SKU No:
        </div>
        <div id="field">
            <input type="text" name="sku_no" id="sku_no" value="">
        </div>
        <div id="lebel">
             <font color="red">&nbsp;</font>In Date:
        </div>
        <div id="field">
            <input type="text" name="in_date" id="in_date" value="">
        </div>
        <div id="lebel">
            <font color="red">&nbsp;</font>Upload Image:
        </div>
        <div id="field">
            <input type="file" name="upload_image" id="upload_image" value="">
        </div>
        <div id="lebel">
            <font color="red">&nbsp;</font>Description:
        </div>
        <div id="field">
            <textarea name="description" id="description"></textarea>
        </div>
        <div id="lebel">
            &nbsp;
        </div>
        <div id="button_field">
            <input type="submit" name="add_product_button" id="add_product_button" value="Add  Product">
        </div>
    </div>
</form>

我正在使用以下方法获取文本字段的值。

List fileItems = upload.parseRequest(request);
// Process the uploaded file items
Iterator i = fileItems.iterator();

while ( i.hasNext () )
{
    FileItem fi = (FileItem)i.next();
    if ( !fi.isFormField () )
    {
        // Get the uploaded file parameters
        String fieldName = fi.getFieldName();
        String value = fi.getString();
        fileName = fi.getName();
        String contentType = fi.getContentType();
        boolean isInMemory = fi.isInMemory();
        long sizeInBytes = fi.getSize();
        // Write the file
        if( fileName.lastIndexOf("\\") >= 0 )
        {
            file = new File( filePath +
            fileName.substring( fileName.lastIndexOf("\\"))) ;
        }
        else
        {
            file = new File( filePath +
            fileName.substring(fileName.lastIndexOf("\\")+1)) ;
        }
        fi.write( file ) ;
    }
    else
    {
        String name = fi.getFieldName();
        String value = fi.getString();
        if( name.equals("product_name") )
        {
            productName = value;
        }
        else if( name.equals("sku_no") )
        {
            skuNo = value;
        }
        else if( name.equals("in_date") )
        {
            newDateString = value;
        }
        else if( name.equals("description") )
        {
            productDesc = value;
        }
    }
}

但我没有得到我在表单中使用的名称为“descripton”的“TextArea”的值。

任何人都可以在提交表单时帮助我获取此文本区域的值。

谢谢

【问题讨论】:

  • 是“描述”中的拼写错误还是只是类型
  • 您能否展示 .parseRequest(request) 代码您如何获取描述参数,

标签: java jsp apache-commons-fileupload servlet-2.5


【解决方案1】:

它有一些问题。你可以给文本框赋予样式,让它看起来像文本区域,这会帮助你

【讨论】:

    【解决方案2】:

    找不到直接的解决方案。

    为了实现这一点,我使用了一个隐藏字段和 jquery。

    点击提交按钮,我在隐藏字段中找到文本区域的值,然后提交表单。

    这里是jquery代码:

    $('#add_product_button').click(function()
    {
            var description = $("#description").val();
            $("#hidden_description").val(description);
            $("add_product_form").submit();
    });
    

    【讨论】:

      【解决方案3】:

      我也遇到了同样的问题,我已经解决了!
      只需将您的 'textarea' 标签放在 'input type="file" ....' 标签之前,
      并且 Servlet 可以获取 textarea 值

      我不会说英语,希望你能理解我的解决方案:-)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-02
        • 2014-12-04
        • 1970-01-01
        • 2013-01-11
        • 2016-08-12
        • 1970-01-01
        • 2011-07-17
        相关资源
        最近更新 更多