DWZ文件上传表单提交
文件上传表单提交
因为Ajax不支持enctype="multipart/form-data" 所以用隐藏iframe来处理无刷新表单提交.
<form method="post" action="url" class="pageFormrequired-validate" enctype="multipart/form-data" onsubmit="return iframeCallback(this);">
或
<form method="post" action="url" class="pageFormrequired-validate" enctype="multipart/form-data"onsubmit="return iframeCallback(this, [navTabAjaxDone/dialogAjaxDone]);">
服务器端响应
DWZ-v1.2版本开始服务器返回和validateCallback格式保持一致:
{
"statusCode":"200",
"message":"操作成功",
"navTabId":"",
"rel":"",
"callbackType":"closeCurrent",
"forwardUrl":""
}
DWZ-v1.2以前版本使用隐藏iframe来处理无刷新表单提交时,服务器端需要返回以下js代码:
<script type="text/javascript">
var statusCode="200";
var message="操作成功";
var navTabId="";
var forwardUrl="";
var callbackType="closeCurrent"
var response = {statusCode:statusCode,
message:message,
navTabId:navTabId,
forwardUrl:forwardUrl,
callbackType:callbackType
};
if(window.parent.donecallback) window.parent.donecallback(response);
</script>
Java服务器端表单处理示例
public class NewsAction extends BaseAction {
private NewsManager manager = bf.getManager(BeanManagerKey.newsManager);
private News news = manager.newNews();
private Collection<News> newsList;
public String add() {
return INPUT;
}
public String insert() {
manager.createNews(news);
return ajaxForwardSuccess(getText("msg.operation.success"));
}
public String edit() {
news = manager.getNews(this.getNewsId());
return INPUT;
}
public String update() {
News m = manager.getNews(newsId);
m.copyProperties(news);
manager.updateNews(m);
return ajaxForwardSuccess(getText("msg.operation.success"));
}
public String publish() {
manager.publishNews(newsId);
return ajaxForwardSuccess(getText("msg.operation.success"));
}
public String disable() {
manager.disableNews(newsId);
return ajaxForwardSuccess(getText("msg.operation.success"));
}
public String delete() {
manager.removeNews(newsId);
return ajaxForwardSuccess(getText("msg.operation.success"));
}
}
// BaseAction 代码片段
public class BaseAction extends ActionSupport {
private int statusCode = 200;
private String message = null;
private String forwardUrl = null;
private String ajaxForward(int statusCode) {
this.statusCode = statusCode;
return OPERATION_DONE;
}
protectedString ajaxForwardSuccess(String message) {
this.message = message;
return ajaxForward(200);
}
protectedString ajaxForwardError(String message) {
this.message = message;
return ajaxForward(300);
}
public int getStatusCode() {
return statusCode;
}
public String getMessage() {
return message;
}
public String getForwardUrl() {
return forwardUrl;
}
public void setForwardUrl(String forwardUrl) {
this.forwardUrl = forwardUrl;
}
}
// 工具类判断是否ajax请求
public class ServerInfo {
public static boolean isAjax(HttpServletRequest request) {
if (request != null
&& "XMLHttpRequest".equalsIgnoreCase(request
.getHeader("X-Requested-With")))
return true;
return false;
}
}