• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Can i use html:file with other input fields?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can i use html:file with other input fields? I got null for formfile in the validate method but another fields is fine.

Any comments is welcome.

thanks,
anurak
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you can, but you must remember to specify enctype="multipart/form-data" in your <html:form> tag.
 
anurak th
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Merrill,

Thanks for your reply,I already set enctype="multipart/form-data".
Before i added another input type to jsp, it worked(file uploading).
After i add another input type, i got null for formfile but another attributes value ok.

thanks,
anurak
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not entirely sure that it will work for validation. When there is both file data and other data, Struts has to go through some extra steps. By the time the data reaches your Action class, everything should work, but it may not work in the validate method of your ActionForm. Check it and let us know: is the FormFile null when you try to access it in your Action class?
 
anurak th
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it also null in action.

anurak
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Show us the code in your ActionForm, Action class, and JSP.

Here's another possibility: Struts will return null for the FormFile if it determines that the size of the file exceeds the maximum size set in the <controller> stanza of your struts-config.xml file. Make sure that the file being uploaded does not exceed this size.
 
anurak th
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

======= my jsp ==========
<%@page contentType="text/html"%>
<%@page pageEncoding="tis-620"%>

<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

<html:form enctype="multipart/form-data" method="POST" action="/createFuncPage.do">
<div id="upload">
<table width="100%" border="0" cellspacing="5" cellpadding="2">
<tr>
<td colspan="2" bgcolor="#B7B7FF"><strong>ระบุไฟล์ Excel ที่ต้องการใช้</strong></td>
</tr>
<tr>
<td width="18%" bgcolor="#DDDDFF"><div align="right">เลือกไฟล์ :</div></td>
<td width="82%"><html:file property="file" /><html:errors property="file"/></td>
</tr>
</table>
<br />
</div>
<br/>
<div id="selectBase">
<table width="100%" border="0" cellspacing="5" cellpadding="2">
<tr>
<td colspan="2" bgcolor="#B7B7FF"><strong>เลือกฐานข้อมูลที่ 1 และฟังก์ชั่นที่ต้องการ </strong></td>
</tr>
<tr>
<td width="18%" bgcolor="#DDDDFF"><div align="right">ฐานข้อมูลที่ 1 : </div></td>
<td width="82%"><label>
<html:radio property="base1" value="1"/>
CUS
  
<html:radio property="base1" value="2"/>
WLG
  
<html:radio property="base1" value="3"/>
MAIN
  
<html:radio property="base1" value="4"/>
STOCK</label>  <html:errors property="base1"/></td>
</tr>
<tr>
<td bgcolor="#DDDDFF"><div align="right">ฟังก์ชั่น :</div></td>
<td><label>
<html:radio property="function1" value="1"/>
RC
  
  
<html:radio property="function1" value="2"/>
IV
  
    
<html:radio property="function1" value="3"/>
RO
     
<html:radio property="function1" value="4"/>
RI</label></td>
</tr>
</table>
</div>

....
</html:form>


======== my action form ===========
/*
* MainMenuActionForm.java
*
* Created on 8 กรกฎาคม 2550, 15:17 น.
*/

package cus.web;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;

/**
*
* @author Administrator
* @version
*/

//public class MainMenuActionForm extends org.apache.struts.action.ActionForm {
public class MainMenuActionForm extends UploadActionForm {
private FormFile file;
private String base1;
private String function1;
private String base2;
private String function2;
private String base3;
private String function3;
private String base4;
private String function4;
private String submit;
private String reset;

/**
*
*/
public MainMenuActionForm() {
super();
// TODO Auto-generated constructor stub
}

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
super.validate(mapping, request);
ActionErrors errors = new ActionErrors();
if (getFile() == null || getFile().getFileName().length() < 1) {
errors.add("file", new ActionMessage("error.file.required"));
// TODO: add 'error.name.required' key to your resources
}
if (getBase1() == null || getBase1().length() < 1) {
errors.add("base1", new ActionMessage("error.base1.required"));
// TODO: add 'error.name.required' key to your resources
}
return errors;
}


public String getBase1() {
return base1;
}

public void setBase1(String base1) {
this.base1 = base1;
}

public String getFunction1() {
return function1;
}

public void setFunction1(String function1) {
this.function1 = function1;
}

public String getBase2() {
return base2;
}

public void setBase2(String base2) {
this.base2 = base2;
}

public String getFunction2() {
return function2;
}

public void setFunction2(String function2) {
this.function2 = function2;
}

public String getBase3() {
return base3;
}

public void setBase3(String base3) {
this.base3 = base3;
}

public String getFunction3() {
return function3;
}

public void setFunction3(String function3) {
this.function3 = function3;
}

public String getBase4() {
return base4;
}

public void setBase4(String base4) {
this.base4 = base4;
}

public String getFunction4() {
return function4;
}

public void setFunction4(String function4) {
this.function4 = function4;
}

public String getSubmit() {
return submit;
}

public void setSubmit(String submit) {
this.submit = submit;
}

public String getReset() {
return reset;
}

public void setReset(String reset) {
this.reset = reset;
}

public FormFile getFile() {
return file;
}

public void setFile(FormFile file) {
this.file = file;
}
}


========= my action =========

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("MainMenuAction was called.");
MainMenuActionForm aForm = (MainMenuActionForm)form;
FormFile file = aForm.getFile();
System.out.println("aaa"+aForm.getBase1());
System.out.println("FILENAME = " + file.getFileName()); ====> I GOT NULL HERE
// write excel file to server directory.
FileOutputStream f = new FileOutputStream("c:/cus/" + file.getFileName());
f.write(file.getFileData());
f.close();

String path = checkFunctionToDo(aForm);

return mapping.findForward(path);

}

The file that i used for testing is very small in size.

Hey, thank you very much for you help.

anurak
 
reply
    Bookmark Topic Watch Topic
  • New Topic