浏览 528 次
|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2008-04-08
Struts2做一个表单提交时,里面有一项文件上传的控件并不是必填项,允许为空。可是使用拦截器的话,如果不上传文件,那是肯定被拦截下来,不允许提交,请问各位,有法子避免这种情况吗?下面是我用的struts-context代码:
<action name="saveShopFriendLink" class="shopFriendLinkAction" method="saveFriendLink"> <result name="success" type="redirect">/platmanage/shop/managerMyFriendLink.action</result> <result name="input">/platmanage/shop/myFriendLink-err.jsp</result> <interceptor-ref name="fileUpload"> <param name="allowedTypes">image/bmp,image/bmp,image/gif</param> <param name="maximumSize">1048576</param> </interceptor-ref> <interceptor-ref name="defaultStack" /> <param name="uploadPath">/uploadFile/shop</param> </action> 同时再问一个问题,上面代码的拦截器名称name="fileUpload"是固定的么?如果一个表单里面有两个文件上传控件,怎么配置呢?感激不尽! 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
时间:2008-04-08
恩,这个...
name="fileUpload"是struts-default.xml里配置的,当然不能改了,除非你重新定义它. 在defaultStack里面已经有fileUpload了,你不需要再次引用. struts-default.xml里这样写的 <interceptor-stack name="defaultStack"> <interceptor-ref name="exception"/> <interceptor-ref name="alias"/> <interceptor-ref name="servletConfig"/> <interceptor-ref name="prepare"/> <interceptor-ref name="i18n"/> <interceptor-ref name="chain"/> <interceptor-ref name="debugging"/> <interceptor-ref name="profiling"/> <interceptor-ref name="scopedModelDriven"/> <interceptor-ref name="modelDriven"/> <interceptor-ref name="fileUpload"/> <interceptor-ref name="checkbox"/> <interceptor-ref name="staticParams"/> <interceptor-ref name="params"> <param name="excludeParams">dojo\..*</param> </interceptor-ref> <interceptor-ref name="conversionError"/> <interceptor-ref name="validation"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name="workflow"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> </interceptor-stack> 你可以看下相关的代码是怎么写的.:) 说不定就是你的特殊引用(fileUpload)出的问题呢,没仔细看... |
|
| 返回顶楼 | |
|
时间:2008-04-08
两个文件上传控件?那也是用的同一个interceptor啊。
if (isNonEmpty(contentType)) {
// get the name of the file from the input tag
String[] fileName = multiWrapper.getFileNames(inputName);
if (isNonEmpty(fileName)) {
// Get a File object for the uploaded File
File[] files = multiWrapper.getFiles(inputName);
if (files != null) {
for (int index = 0; index < files.length; index++) {
if (acceptFile(files[index], contentType[index], inputName, validation, ac.getLocale())) {
parameters.put(inputName, files);
parameters.put(inputName + "ContentType", contentType);
parameters.put(inputName + "FileName", fileName);
}
}
}
} else {
log.error(getTextMessage("struts.messages.invalid.file", new Object[]{inputName}, ActionContext.getContext().getLocale()));
}
} else {
log.error(getTextMessage("struts.messages.invalid.content.type", new Object[]{inputName}, ActionContext.getContext().getLocale()));
}
看了一下代码,貌似是这儿的问题。如果为空的话,它可能就获得不了contentType了。所以报错。你自己定义一个interceptor,不就行了。 |
|
| 返回顶楼 | |
|
时间:2008-04-08
很感谢楼上两位这么快回复阿!我仔细拜读了你们的答案,觉得还是需要自己写一个拦截器才能彻底解决我的两个问题,用默认定义的还是不能解决的。感谢两位给我提供解决思路!
|
|
| 返回顶楼 | |





