S2SH开发,Struts2的JSP中,这种写法:<s:textfield name="product.name"/>,form会接到name为“product.name”的值,但用这种写法时有如下两个不解之处:
1.怎么用jQuery做form验证?因为jQuery的基本写法是:
rules: {
name: {
required: true
},
其中“name”是form中的名称值,请教“product.name”时的验证写法。
2.文件上传在Action中怎么写?
找了很多文件上传的例子,都是<s:file name ="myFile" label ="Image File" />的样子,这时在Action中直接声明变量:
private File myFile;
就可以,这样的myFile是没有点的写法,有点时,如“product.myFile”,怎么在Action中写文件上传呢?
1.怎么用jQuery做form验证?因为jQuery的基本写法是:
rules: {
name: {
required: true
},
其中“name”是form中的名称值,请教“product.name”时的验证写法。
2.文件上传在Action中怎么写?
找了很多文件上传的例子,都是<s:file name ="myFile" label ="Image File" />的样子,这时在Action中直接声明变量:
private File myFile;
就可以,这样的myFile是没有点的写法,有点时,如“product.myFile”,怎么在Action中写文件上传呢?
解决方案 »
- 请教有关文件上传的问题。。。。有会的进来帮帮忙哦、、、、、、、、
- Spring 问题
- 我的action怎么没执行啊?急啊!!!
- hibernate 如何进行连接表(中间表)查询与更新
- 如何架设本地服务器?
- 大型人才服务玩站开发周期估测
- 关于javascript验证用户注册表单的问题
- SSH整合的问题
- 有人研究了jive的源程序吗???
- 新手求助:hibernate中表之间的关联真的很有必要用么?(比如onetoone,manytoone之类的,我直接在数据库中加外键不就可以了么?))
- 十万火急 正则表达式
- struts 2.0 <s:form action = "" theme="simple"> theme ="simple" 是什么意思呀????
first:
$(":text[name='product.name']").val();//这样取值second:
文件上传主要代码有几处,
1.有个配置文件struts.properties(设置文件上传大小,和临时目录)
2.action 中private File myFile; 的同时还要有private String myFileFileName;(注意后面的FileName加上后系统认识的就像execute()方法)
最后在execute方法中:
String targetDirectory=ServletActionContext.getRequest.getRealPath("/你项目下的路径");
File target=new File(targetDirectory,myFileFileName);//
FileUtils.copyFile(myFile,target);//把上传的文件复制到目标就算完成上传
希望对你有帮助...睡觉了
对于第二个问题——文件上传的写法,你的例子中myFile还是对应form中“<s:file name ="myFile" label ="Image File" />”的形式吧?要是<s:file name ="product.myFile" label ="Image File" />呢?
实际上我的form中要上传的是<s:file name ="product.image" label ="Image File" />,这样在Action中定义如下:
// ATTACHMENT UPLOAD READY BEGIN
private static final int BUFFER_SIZE = 1024 * 1024; private File image;
private String contentType;
private String fileName;
private String imageFileName;
private String caption; public void setMyFileContentType(String contentType) {
this.contentType = contentType;
} public void setMyFileFileName(String fileName) {
this.fileName = fileName;
} public void setImage(File image) {
this.image = image;
} public String getImageFileName() {
return imageFileName;
} public String getCaption() {
return caption;
} public void setCaption(String caption) {
this.caption = caption;
} private static void copy(File src, File dst) {
try {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src),
BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
} private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
} // ATTACHMENT UPLOAD READY END
上传时如下写:
// ATTACHMENT UPLOAD DO BEGIN
if (null != this.fileName && this.fileName != "") {
imageFileName = new Date().getTime() + getExtention(fileName);
File imageFile = new File(ServletActionContext
.getServletContext().getRealPath("/images/product")
+ "/" + imageFileName);
copy(image, imageFile);
System.out.println("fileName is:" + fileName);
System.out.println("imageFileName is:" + imageFileName);
System.out.println("imageFile is:" + imageFile);
}
// ATTACHMENT UPLOAD DO END
结果不行,怎么办??
$().ready(function() {
// validate signup form on keyup and submit
$("#product_add").validate({
rules: {
name: {
required: true
}
},
messages: {
name: {
required:"Please input a name."
}
}
});
});
这里的“rules”和“messages”下的“name”都是form中元素的name值,如:<s:textfield name="name"/>。
请教<s:textfield name="product.name"/>时的验证写法。
那么你的action中就应该只放一个product属性,
这个product属性中定义三个属性
private File image;
private String imageFileName;
private String imageContentType;
并添加get/set