Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么找不到字段_Java_Jsp_File Upload_Struts2 - Fatal编程技术网

Java 为什么找不到字段

Java 为什么找不到字段,java,jsp,file-upload,struts2,Java,Jsp,File Upload,Struts2,我使用org.apache.commas.*,org.apache.commas.disk.*,org.apache.commas.fileupload.servlet.*包在JSP程序中上传文件,不需要Struts,它工作得很好,数据很容易处理,但是,当我使用MyEclipse 8.5将Struts 2核心库添加到我的web项目中时,它不起作用,并且找不到任何字段。可能是程序正在使用上传.parseRequest。 坦率地说,我无法理解这个问题,所以我也分享了我的计划 AddCategory.

我使用
org.apache.commas.*
org.apache.commas.disk.*
org.apache.commas.fileupload.servlet.*
包在JSP程序中上传文件,不需要Struts,它工作得很好,数据很容易处理,但是,当我使用MyEclipse 8.5将Struts 2核心库添加到我的web项目中时,它不起作用,并且找不到任何字段。可能是程序正在使用
上传.parseRequest
。 坦率地说,我无法理解这个问题,所以我也分享了我的计划

AddCategory.jsp

<html>
<head>
  <meta http-equiv="refresh" content="30">
</head>

<script type="text/javascript">
  function blank() {


    if (document.cate.cat.value == "Enter New Category") {
      alert(" Category must not be blanked !!!");
      document.cate.cat.focus();
      return false;
    }
    else if (!document.getElementById("file1").value) {
      alert("No file selected");
      return false;
    }
    else {
      return true;
    }
  }

</script>
<form name="cate" action="CategoryAdded.jsp" method="post" enctype="multipart/form-data" onsubmit="return blank()">
  <table width="100%" border="0">
    <tr>
      <th colspan="2" scope="col">
        <div align="center">Create New Category</div>
      </th>
    </tr>
    <tr>
      <td width="50%">
        <div align="right">Enter New Category:</div>
      </td>
      <td width="50%">
        <input name="cat" type="text" id="cat" value="Enter New Category"
               onFocus="if(this.value== 'Enter New Category'){   this.value='' ; this.style.background='white';}"
               onBlur="if(this.value==''){this.value='Enter New Category';  this.style.background='lightyellow'}">
      </td>
    </tr>

    <tr>
      <td width="50%">
        <div align="right">Upload photo:</div>
      </td>
      <td width="50%"><input name="file1" type="file" id="file1"></td>
    </tr>


    <tr>
      <td colspan="2">
        <div align="center">
          <input type="submit" name="Submit" value="Add Category">
        </div>
      </td>
    </tr>
  </table>
</form>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<%@page import="java.io.*" %>
<%@ page language="java" errorPage="" %>
<%@ page import="java.sql.*" %>
<%@ page import="org.apache.commons.io.*" %>
<%@page import="java.util.Iterator,java.util.List" %>
<%@page import="org.apache.commons.*,org.apache.commons.fileupload.disk.*,org.apache.commons.fileupload.servlet.*" %>
<%@ page import="java.util.*" %>
<%@page import="org.apache.commons.fileupload.FileItemFactory" %>
<%@page import="org.apache.commons.fileupload.FileItem" %>
<%@page import="org.apache.commons.fileupload.FileUploadException" %>
<%@page import="p1.DBInfo" %>
<%@page import="p1.Identy" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<%
  String pname = "";
  Identy id = new Identy();
  String cod = id.code();

  boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
  if (!isMultipartContent) {
    System.out.println("No multipart found");
    return;
  }


  FileItemFactory factory = new DiskFileItemFactory();
  ServletFileUpload upload = new ServletFileUpload(factory);
  try {
    List<FileItem> fields = upload.parseRequest(request);
    Iterator<FileItem> it = fields.iterator();

    if (!it.hasNext()) {
      System.out.println("No fields     found");
      return;
    }


    DBInfo obj = new DBInfo();
    Connection cn = obj.getConn();
    PreparedStatement ps = cn.prepareStatement("insert into category values(?,?,?)");

    while (it.hasNext()) {

      FileItem fileItem = it.next();

      if (fileItem.getFieldName().equals("cat")) {
        pname = fileItem.getString();
        System.out.println("category name is " + pname);
      }

      boolean isFormField = fileItem.isFormField();

      if (!isFormField) {
        String s = fileItem.getName().substring(fileItem.getName().lastIndexOf("\\") + 1);

        fileItem.write(new File("D:\\Practice\\ShoppingCart\\WebRoot\\images\\" + s));
        System.out.println(s);
        fileItem.getOutputStream().close();

        ps.setString(3, "D:\\Practice\\ShoppingCart\\WebRoot\\images\\" + s);
      }
    }

    ps.setString(1, pname);
    ps.setString(2, pname + cod);

    int i = ps.executeUpdate();

    if (i == 1) {
%>
<head>


  <script type="text/javascript">
    function myFunction() {


      var r = confirm("New Category Added Successfully!!!\nIf you Want to Add more New Category then Press Ok!!!");
      if (r == true) {

        window.location = "AddCategory.jsp";


      }
      else {
        window.location = "Tryy.jsp";
      }

    }
  </script>


</head>

<body onload="myFunction()">
</body>

<%
    }
    cn.close();

  } catch (Exception e) {
    e.printStackTrace();
  }
%>
</html>

最好的方法是重写JSP以删除Scriptlet,并将业务逻辑移动到action类

您还可以使用Struts2
标记有条件地呈现内容


commons fileUpload
是在Struts2中上载文件的默认实现,要正确使用它,您可以运行一个示例Struts2项目,如。

我无法理解您的问题。找不到哪个字段。错误是什么,您希望发生什么?当您使用Struts2时,为什么要在JSP中执行此文件上载逻辑,我建议让struts2为您透明地处理这个问题。此外,您甚至在JSP中执行DB操作,这违背了使用任何MVC框架的全部目的。但我对struts2不太了解……以及如何使用struts使用文件上载……基本上如何使用struts 2进行文件上载我的意思是“未找到字段”当我运行这个程序时,因为我给出的if条件“if(!it.hasNext())for List's iterator”有问题。如果你能仔细地再次看到这个程序,如果我想使用jsp上传文件(因为我不知道如何使用struts 2对于像我这样的新手来说是非常复杂的),那么用简单的方式上传文件我能做些什么呢