0

我有这个问题,我真的要疯了,没有结果。我有一个表单,并且所有字段都需要通过 validator.xml 我的表单包含一个用于上传图像的字段(必需) 当我单击提交按钮时,会显示一个等待通过的页面(在 struts.xml 中配置的 execAndWait)。我的大问题是:等待页面总是重定向到我的带有文本的表单页面(文件字段和强制性)。这是代码:

/register.jsp

<!-- al submit chiama l'action register -->
<action name="register" class="action.Register" method="execute" >

<interceptor-ref name="defaultStack" />

 <interceptor-ref name="fileUpload">
            <param name="maximumSize">10000000</param>
             <param name="allowedTypes">image/jpeg,image/gif,image/jpg</param>

 </interceptor-ref>

   <interceptor-ref name="params"></interceptor-ref>
  <interceptor-ref name="execAndWait">
  </interceptor-ref>        
    <result name="success">index.jsp</result>
    <result name="input">/register.jsp</result>  
    <result name="wait">/test.jsp</result>  
</action>

等待页面:

<meta http-equiv="refresh" content="5;url=<s:url includeParams="all" />"/>
</head>
<body>

<p>your request is processing...</p>
<img src="images/indicator.gif"/>

我的表格:

 <s:form method="post" action="register" validate="false" enctype="multipart/form-data">
            <s:textfield key="utenteBean.nome" name="utenteBean.nome" value="a" />
            <s:textfield key="utenteBean.nickname" name="utenteBean.nickname" value="a" />
            <sj:datepicker key="utenteBean.nato" name="utenteBean.nato"
                showButtonPanel="true" displayFormat="dd/mm/yy"  value="25/09/1983"/>
            <s:textfield key="utenteBean.professione" name="utenteBean.professione"   value="a"/>
            <s:textfield key="utenteBean.eta" name="utenteBean.eta" value="3"/>
            <s:textfield key="utenteBean.dj_preferito" name="utenteBean.dj_preferito" value="a" />
            <s:textfield key="utenteBean.rave_fatti" name="utenteBean.rave_fatti" value="3" />
            <s:textfield key="utenteBean.sito_preferito" name="utenteBean.sito_preferito" value="a" />
            <s:textfield key="utenteBean.come_siveste" name="utenteBean.come_siveste" value="a" />
            <s:textarea key="utenteBean.messaggio" name="utenteBean.messaggio" value="a"/>
            <s:file label="file" name="file" requiredLabel="true"" ></s:file>

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

非常感谢您的帮助

4

3 回答 3

0

我通过将我的 bean 放入会话来解决它。经过数小时的工作,我找不到更好的解决方案。如果其他人有同样的问题,我可以看到我所做的更改。行动

package action;



import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import Bean.UtenteBeanAction;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import Utility.Util;


public class Register extends ActionSupport implements SessionAware {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private UtenteBeanAction utenteBean;


    private ServletContext context;
    Map<String, Object> session;


    public String execute(){


        session.put("utente", utenteBean);
        if (utenteBean.getFile() != null) {
            File file = utenteBean.getFile();
            // /System.out.println(file.getName());
            try {
                Util.saveFile(file, utenteBean.getFileFileName());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return ERROR;
            }
        } else {
            return ERROR;
        }   

        return SUCCESS;
    }



    public void validate() {


        if (session.get("utente") != null) {
            this.utenteBean = (UtenteBeanAction) session.get("utente");
        }

        if (this.utenteBean.getFileFileName() == null) {
                this.addFieldError("utenteBean.file", "errore");
        }

    }


    public UtenteBeanAction getUtenteBean() {
        return utenteBean;
    }

    public void setUtenteBean(UtenteBeanAction utenteBean) {
        this.utenteBean = utenteBean;
    }




    public ServletContext getContext() {
        return context;
    }

    public void setContext(ServletContext context) {
        this.context = context;
    }

    @Override
    public void setSession(Map<String, Object> session) {
        // TODO Auto-generated method stub

        this.session=session;

    }

}

我希望我能有所帮助。感谢您的支持。

于 2014-04-14T14:12:12.690 回答
0

我想你输入了错误的字段名称file tag。你能告诉我们你的动作课吗?关于那个而不是:

<s:file label="file" name="file" requiredLabel="true"" ></s:file>

尝试使用:

<s:file label="file" name="utenteBean.file" requiredLabel="true"" ></s:file>

或者只是在您的操作类中检查文件属性的名称。

于 2014-04-13T05:56:04.350 回答
0

是的,当然......在我的动作类下面:

公共类注册扩展 ActionSupport 实现 SessionAware {

/**
 * 
 */
private static final long serialVersionUID = 1L;

private UtenteBeanAction utenteBean;

private File file;
private String fileContentType;
private String fileFileName;
private String filesPath;
private ServletContext context;
Map<String, Object> session;


public String execute(){

    if (file != null) {
        File file = this.file;
        // /System.out.println(file.getName());
        try {
            Util.saveFile(file, fileFileName);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return ERROR;
        }
    } else {
        return ERROR;
    }   
    return SUCCESS;
}



 public void validate(){

     if(this.fileFileName==null){
         this.addFieldError("file", "errore");  
     }
 }


public UtenteBeanAction getUtenteBean() {
    return utenteBean;
}

public void setUtenteBean(UtenteBeanAction utenteBean) {
    this.utenteBean = utenteBean;
}


public File getFile() {
    return file;
}

public void setFile(File file) {
    this.file = file;
}

public String getFileContentType() {
    return fileContentType;
}

public void setFileContentType(String fileContentType) {
    this.fileContentType = fileContentType;
}

public String getFileFileName() {
    return fileFileName;
}

public void setFileFileName(String fileFileName) {
    this.fileFileName = fileFileName;
}

public String getFilesPath() {
    return filesPath;
}

public void setFilesPath(String filesPath) {
    this.filesPath = filesPath;
}

public ServletContext getContext() {
    return context;
}

public void setContext(ServletContext context) {
    this.context = context;
}

@Override
public void setSession(Map<String, Object> session) {
    // TODO Auto-generated method stub

    this.session=session;


}

}

非常感谢你的帮助 ...

于 2014-04-13T12:17:30.023 回答