2013-08-16 58 views
0

我正在使用org.apache.commas.*,,org.apache.commas.fileupload.servlet.*用於在JSP程序中上傳文件的包,並且沒有必要使用Struts,而且它工作得很好,數據很容易處理,但是當我將Struts 2核心庫添加到我的web項目使用MyEclipse 8.5它不工作,沒有找到字段。可能是該程序與upload.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> 

CategoryAdded.jsp

<%@ 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> 
+1

我無法理解你的問題。什麼領域沒有被發現。錯誤是什麼,你期望會發生什麼? – gerrytan

+0

當你使用Struts2時,爲什麼你在JSP中做這個文件上傳邏輯,我建議讓struts2爲你處理這個透明的問題。而且你甚至在JSP中進行了DB操作,這個操作正在破壞使用任何MVC框架的全部目的。 –

+0

但我不知道struts 2這麼多.....以及如何使用文件上傳使用struts ...基本上如何做文件上傳與struts 2 – user2685033

回答

0

的最好的事情就是重寫用於刪除scriptlet並將業務邏輯移至動作類的JSP。

您也可以使用Struts2 <s:if><s:else>標籤有條件地呈現內容。

commons-fileUpload是在Struts2中上傳文件的默認實現,爲了正確使用它,你可以運行一個Struts2項目例如struts-2-upload-multiple-files-example

+0

如果我想使用文件上傳使用jsp(因爲我不不知道如何使用struts 2對於像我這樣的新手來說非常複雜),那麼我可以通過簡單的方式來上傳文件 – user2685033

相關問題