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.   

    第一个没用过,第二个   上传时我们可以再用text文本框作相应的处理
      

  2.   

    两个问题都是我在现实开发中碰到过的问题,呵呵,缘份呐`:
    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);//把上传的文件复制到目标就算完成上传
    希望对你有帮助...睡觉了
      

  3.   

    首先谢谢你的回答。
    对于第二个问题——文件上传的写法,你的例子中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
    结果不行,怎么办??
      

  4.   

    2 楼 samuelfisher 的第一个问题的回复,我用jQuery的validate验证写法,如:
    $().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"/>时的验证写法。
      

  5.   

    如果你的需求是这样<s:file name ="product.image" label ="Image File" />上传.!
    那么你的action中就应该只放一个product属性,
    这个product属性中定义三个属性
    private File image;
    private String imageFileName;
    private String imageContentType;
    并添加get/set