java - Why no fields found -
i using org.apache.commas.*
, org.apache.commas.disk.*
, org.apache.commas.fileupload.servlet.*
packages file uploading in jsp program , there no need of struts , working great, data worked this, when added struts 2 core libraries web project using myeclipse 8.5 not working , have no fields found. may program upload.parserequest
. frankly, unable understand problem, share program too
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 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> = 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 category values(?,?,?)"); while (it.hasnext()) { fileitem fileitem = it.next(); if (fileitem.getfieldname().equals("cat")) { pname = fileitem.getstring(); system.out.println("category name " + 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 = ps.executeupdate(); if (i == 1) { %> <head> <script type="text/javascript"> function myfunction() { var r = confirm("new category added successfully!!!\nif want add more new category 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>
the best thing rewrite jsps remove scriptlets , move business logic action classes.
you use struts2 <s:if>
, <s:else>
tags render content conditionally.
the commons-fileupload
default implementation uploading files in struts2, use correctly run example struts2 project struts-2-upload-multiple-files-example.
Comments
Post a Comment