Extjs+struts实现文件上传
本人最近一直在研究extjs,感觉其功能强大复杂,前段时间做了个项目,要使用extjs实现文件上传,下面写出研究成果,与各位一起分享。
于是就想在Extjs中直接调用后台业务逻辑方法,但不知道这样如何实现,如何在url中写调用业务逻辑方法呢,盼哪位高手给小弟解惑,深表感谢!
本人最近一直在研究extjs,感觉其功能强大复杂,前段时间做了个项目,要使用extjs实现文件上传,下面写出研究成果,与各位一起分享。
- Ext.Ajax.request({
- url:'/doc.do?method=deleteDocTag',
- success:function(){globalStore.load();},
- params:{tagId:u,docId:docId}
- });
Ext.Ajax.request({ url:'/doc.do?method=deleteDocTag', success:function(){globalStore.load();}, params:{tagId:u,docId:docId} });
于是就想在Extjs中直接调用后台业务逻辑方法,但不知道这样如何实现,如何在url中写调用业务逻辑方法呢,盼哪位高手给小弟解惑,深表感谢!
Extjs+struts实现文件上传
本人最近一直在研究extjs,感觉其功能强大复杂,前段时间做了个项目,要使用extjs实现文件上传,下面写出研究成果,与各位一起分享。
1.fileUpload.js
本人最近一直在研究extjs,感觉其功能强大复杂,前段时间做了个项目,要使用extjs实现文件上传,下面写出研究成果,与各位一起分享。
1.fileUpload.js
- Ext.onReady(function(){
- var form = new Ext.form.FormPanel({
- renderTo:'file',
- labelAlign: 'right',
- title: '文件上传',
- labelWidth: 60,
- frame:true,
- url: '../upload.do?op=uploadFile',//fileUploadServlet
- width: 300,
- height:200,
- fileUpload: true,
- items: [{
- xtype: 'textfield',
- fieldLabel: '文件名',
- name: 'file',
- inputType: 'file'//文件类型
- }],
- buttons: [{
- text: '上传',
- handler: function() {
- form.getForm().submit({
- success: function(form, action){
- Ext.Msg.alert('信息', '文件上传成功!');
- },
- failure: function(){
- Ext.Msg.alert('错误', '文件上传失败');
- }
- });
- }
- }]
- });
- });
2.uploadAction.java 其中使用了cos上传组件,下载地址:http://www.servlets.com/cos/cos-05Nov2002.zip
- package zx.struts.actions;
- import java.io.File;
- import java.util.Enumeration;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import com.oreilly.servlet.MultipartRequest;
- public class UploadAction extends DispatchAction{
- public ActionForward uploadFile(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)throws Exception {
- String saveDirectory ="F://jboss-4.2.2.GA//server//default//deploy//userDemo.war//upload"; //文件上传后,保存的位置
- int maxPostSize =3 * 5 * 1024 * 1024 ; //每个文件最大5MB,最多3个文件,所以...
- //response的编码为"gb2312"
- MultipartRequest multi =
- new MultipartRequest(request, saveDirectory, maxPostSize,
- "gb2312");
- //输出反馈信息
- Enumeration files = multi.getFileNames();
- while (files.hasMoreElements()) {
- String name = (String)files.nextElement();
- File f = multi.getFile(name);
- if(f!=null){
- String fileName = multi.getFilesystemName(name);
- String lastFileName= saveDirectory+"//" + fileName;
- System.out.println("上传的文件:"+lastFileName);
- }
- }
- return null;
- }
- }
package zx.struts.actions; import java.io.File; import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import com.oreilly.servlet.MultipartRequest; public class UploadAction extends DispatchAction{ public ActionForward uploadFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception { String saveDirectory ="F://jboss-4.2.2.GA//server//default//deploy//userDemo.war//upload"; //文件上传后,保存的位置 int maxPostSize =3 * 5 * 1024 * 1024 ; //每个文件最大5MB,最多3个文件,所以... //response的编码为"gb2312" MultipartRequest multi = new MultipartRequest(request, saveDirectory, maxPostSize, "gb2312"); //输出反馈信息 Enumeration files = multi.getFileNames(); while (files.hasMoreElements()) { String name = (String)files.nextElement(); File f = multi.getFile(name); if(f!=null){ String fileName = multi.getFilesystemName(name); String lastFileName= saveDirectory+"//" + fileName; System.out.println("上传的文件:"+lastFileName); } } return null; } }
Ext.onReady(function() {
var store1 = new Ext.data.SimpleStore({
fields: [ 'id','role'],
data :[['1','圈子1'],['2','圈子2']]
});
var form = new Ext.form.FormPanel({
renderTo : 'form',
title : '用户注册',
height : 200,
width : 400,
labelWidth : 100,
labelAlign : 'right',
frame : true,
waitMsgTarget:true,
defaults : {
width : 180
},
items: [
{
xtype : 'textfield', name : 'userName', fieldLabel : '用户名', allowBlank:false
},
{
xtype : 'textfield', name : 'password', fieldLabel : '密 码', inputType : 'password',allowBlank:false
},
{
xtype : 'textfield', name : 'password1', fieldLabel : '确认密码', inputType : 'password',allowBlank:false
},
new Ext.form.ComboBox( {
fieldLabel : '圈 子',
name : 'role',
mode: 'local',
store : store1,
displayField:'role',
valueField: 'id',
triggerAction: 'all',
emptyText:'请选择圈子...',
allowBlank:false
} )
],
buttons: [{
text: '提交',
handler: function(){
if(form.getForm().isValid()){
form.getForm().submit({
url:'../user.do?op=addEmploye',
waitMsg:'正在提交注册信息...'
//success: onRegSuccess,
//failure: onFailure
});
}else{
Ext.Msg.alert('信息','请信息填写完成在提交!');
}
}
},{
text: '重置',
handler: function(){
form.getForm().reset();
}},
{
text: '返回',
handler: function(){
window.location.href="javascript:history.back(-1)";
}
}]
});
});
上面是一个注册信息表单,当提交时我想判断密码和确认密码是否一致,请问各位大虾:如何获取输入的密码值呢,为什么我用form.getForm().password.value不对呢?谢谢各位了!
var store1 = new Ext.data.SimpleStore({
fields: [ 'id','role'],
data :[['1','圈子1'],['2','圈子2']]
});
var form = new Ext.form.FormPanel({
renderTo : 'form',
title : '用户注册',
height : 200,
width : 400,
labelWidth : 100,
labelAlign : 'right',
frame : true,
waitMsgTarget:true,
defaults : {
width : 180
},
items: [
{
xtype : 'textfield', name : 'userName', fieldLabel : '用户名', allowBlank:false
},
{
xtype : 'textfield', name : 'password', fieldLabel : '密 码', inputType : 'password',allowBlank:false
},
{
xtype : 'textfield', name : 'password1', fieldLabel : '确认密码', inputType : 'password',allowBlank:false
},
new Ext.form.ComboBox( {
fieldLabel : '圈 子',
name : 'role',
mode: 'local',
store : store1,
displayField:'role',
valueField: 'id',
triggerAction: 'all',
emptyText:'请选择圈子...',
allowBlank:false
} )
],
buttons: [{
text: '提交',
handler: function(){
if(form.getForm().isValid()){
form.getForm().submit({
url:'../user.do?op=addEmploye',
waitMsg:'正在提交注册信息...'
//success: onRegSuccess,
//failure: onFailure
});
}else{
Ext.Msg.alert('信息','请信息填写完成在提交!');
}
}
},{
text: '重置',
handler: function(){
form.getForm().reset();
}},
{
text: '返回',
handler: function(){
window.location.href="javascript:history.back(-1)";
}
}]
});
});
上面是一个注册信息表单,当提交时我想判断密码和确认密码是否一致,请问各位大虾:如何获取输入的密码值呢,为什么我用form.getForm().password.value不对呢?谢谢各位了!