遇到一个struts1.2上传文件奇怪的问题

开源中国首席颈椎砖家 发布于 2012/06/29 16:29
阅读 215
收藏 0
Form类定义了 FormFile upload_file get、set方法是eclipse生成的。 

jsp页面上添加 <input type="file" name="upload_file" id="upload_file"  />后提交,在action里Form表单里所有 值被置为null。 

去除 <input type="file" name="upload_file" id="upload_file"  />后提交,在action里Form表单里可以找到页面填写数据。 

配置文件 

<action 
      path="/upload" 
      type="com.fractal.clkmapp.upload.webapp.action.AppuploadAction" 
      name="appuploadForm" 
      scope="request" 
      parameter="method" 
      unknown="false" 
      validate="false" 
      > 
     <forward name="list" path="/page/upload/upload_list.jsp"></forward> 
     <forward name="edit" path="/page/upload/upload_edit.jsp"></forward> 
</action> 

jsp页面文件 

<%@ page language="java"  pageEncoding="UTF-8" import="java.util.*"%> 
<%@page import="com.fractal.clkmapp.upload.webapp.form.AppuploadForm"%> 
<%@ include file="/common/taglibs.jsp"%> 
<% 
AppuploadForm appuploadForm = (AppuploadForm)request.getAttribute("appuploadForm"); 
%> 
<!DOCTYPE html> 
<html> 
  <head> 
<script type='text/javascript' src='<%=Basepath %>/javascript/jquery.js'></script> 
<script type="text/javascript"> 

function validSubmit(){ 
var flag1=true; 
var app_name=$("#app_name").val(); 
var nameRegex = /[<>%'"]/; 
if(app_name == ''){ 
alert("请输入上传名称!"); 
flag1 = false; 
return false; 

if(nameRegex.test(app_name)){ 
alert("上传名称由汉字、字母、数字、常用符号组成!"); 
flag1 = false; 
return; 



var check3=document.getElementsByName("channels"); 
var flag2=false; 
for(var i=0;i<check3.length;i++){ 
if(check3[i].checked){ 
flag2=true; 
break; 


if(!flag2){ 
alert("至少选择一个渠道!"); 
return false; 

if(flag1 && flag2){ 
$("#appuploadForm").get(0).submit(); 


</script> 
</head> 

<body> 
<div class="r"> 
<div class="rbox w1"> 
<form action="upload.action?method=save" method="post" id="appuploadForm"  name="appuploadForm" enctype="multipart/form-data"> 

<input type="hidden" name="app_id" id="app_id" value="" /> 
<h2 class="h22">新增上传</h2><br/> 
<div class="w11"> 
<p class="p11">上传名称:</p> 
<p class="p22"><input type="text" name="app_name" id="app_name" maxlength="20" value=""  /></p> 
</div> 
<div class="w11"> 
<p class="p11">选择上传内容:</p> 
<p class="p22"><input type="file" name="upload_file" id="upload_file"/></p> 
</div> 
<c:if test='${flag==1}'> 
<div class="w11"> 
<p class="p11">选择下载渠道:</p> 
<p><c:forEach items="${channelList}" var="channeltype"  varStatus="status"> <input type="checkbox" name="channels"  value='<c:out value="${channeltype.id}"/>' > 
             <c:out value="${channeltype.channel_name}"/>&nbsp;&nbsp;<c:if test="${status.count%5==0}" ><br/></c:if></c:forEach></p> 
</div> 
</c:if> 
<br/> 
<br/> 
<div class="w12"> 

<span class="w121"><input type="button" value="保 存"  onclick="validSubmit()"/></span> 
<span class="w121"><a href="<%=Basepath %>/upload.action?method=list"><input type="button" value="取   消"  /></a></span> 
</div> 

</form> 
</div> 
</div> 

<% 
  String msg=(String)request.getAttribute("msg"); 
  if(msg!=null)out.println(msg); 
%> 

</body> 
</html> 

action文件 

public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, 
HttpServletResponse response) throws Exception { 

User cuuser = (User) request.getSession().getAttribute("user"); 
AppuploadForm appForm = (AppuploadForm) form; 

form文件 

public class AppuploadForm extends BaseForm implements java.io.Serializable { 

private String app_id; 
private String app_name; 
private String begin_time; 
private String end_time; 
private FormFile upload_file; 
private String[] channels; 
private String type; 

public String getApp_id() { 
        return app_id; 
        } 
public void setApp_id(String app_id) { 
        this.app_id = app_id; 
        } 
public String getApp_name() { 
        return app_name; 
        } 
public void setApp_name(String app_name) { 
        this.app_name = app_name; 
        } 
public String getBegin_time() { 
        return begin_time; 
        } 
public void setBegin_time(String begin_time) { 
        this.begin_time = begin_time; 
        } 
public String getEnd_time() { 
        return end_time; 
        } 
public void setEnd_time(String end_time) { 
        this.end_time = end_time; 
        } 

public String[] getChannels() { 
        return channels; 
        } 
public void setChannels(String[] channels) { 
        this.channels = channels; 
        } 
public String getType() { 
        return type; 
        } 
public void setType(String type) { 
        this.type = type; 
        } 
public FormFile getUpload_file() { 
        return upload_file; 
        } 
public void setUpload_file(FormFile upload_file) { 
        this.upload_file = upload_file; 
        } 
}
加载中
0
书痴
书痴

可能是页面缓存了吧!你重新打开个浏览器试试

返回顶部
顶部