在学习Struts2文件上传功能开发时,遇见一个问题,请高手进来帮我解决一下,谢谢啦!!!
我用的是Myeclipse6.5版本学习的,运行后提示了一个错误,找了半天也不知道错在哪儿了,错误代码是:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception org.apache.jasper.JasperException: The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location] org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:541) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:435) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location] org.apache.struts2.views.jsp.TagUtils.getStack(TagUtils.java:60) org.apache.struts2.views.jsp.StrutsBodyTagSupport.getStack(StrutsBodyTagSupport.java:44) org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(ComponentTagSupport.java:48) org.apache.jsp.jsp.upload_jsp._jspx_meth_s_005fform_005f0(upload_jsp.java:101) org.apache.jsp.jsp.upload_jsp._jspService(upload_jsp.java:70) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs.
Apache Tomcat/6.0.13
哎,根据错误找了半天还是找不出来,现在贴出源代码:
﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡
struts2的配置文件struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
" http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.i18n.encoding" value="gb2312"></constant>
<constant name="struts.multipart.saveDir" value="c:\"></constant>
<package name="struts2_chapter4" namespace="/" extends="struts-default">
<global-results>
<result name="global">/jsp/upload.jsp</result>
</global-results>
<action name="upload" class="action.UploadAction">
<result name="input">/jsp/upload.jsp</result>
<result name="success">/jsp/result.jsp</result>
</action>
<action name="index">
<result>/jsp/upload.jsp</result>
</action>
</package>
</struts>
﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡
upload.jsp文件的源文件:
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<title>上传文件</title>
</head>
<body>
<s:form action="upload" method="post" enctype="multipart/form-data">
<tr>
<td>上传文件:<s:file name="file"></s:file></td>
</tr>
<tr>
<td>再次上传文件:<s:file name="file"></s:file></td>
</tr>
<tr>
<td align="left"><s:submit name="submit" value="提交"></s:submit></td>
</tr>
</s:form>
</body>
</html>
﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡
result.jsp的源文件:
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<title>上传结果</title>
</head>
<body>
上传文件:
<s:property value="fileFileName"/>
</body>
</html>
﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡
uploadAction.java的源文件:
package action;
import java.util.List;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport{
private final static String UPLOADDIR="/upload";
private List<File> file;
private List<String> fileFileName;
private List<String> fileContentType;
public List<File> getFile(){
return file;
}
public void setFile(List<File> file){
this.file=file;
}
public List<String> getFileFileName(){
return fileFileName;
}
public void setFileFileName(List<String> fileFileName){
this.fileFileName=fileFileName;
}
public List<String> getFileContentType(){
return fileContentType;
}
public void setFileContentType(List<String> fileContentType){
this.fileContentType=fileContentType;
}
public String execute() throws Exception{
for(int i=0;i<file.size();i++){
upLoadFile(i);
}
return "success";
}
private void upLoadFile(int i)throws FileNotFoundException,IOException{
try{
InputStream in=new FileInputStream(file.get(i));
String dir=ServletActionContext.getRequest().getRealPath(UPLOADDIR);
File uploadFile=new File(dir,this.getFileFileName().get(i));
OutputStream out=new FileOutputStream(uploadFile);
byte[] buffer=new byte[1024*1024];
int length;
while((length=in.read(buffer))>0){
out.write(buffer,0,length);
}
in.close();
out.close();
}catch(FileNotFoundException ex){
ex.printStackTrace();
}catch(IOException ex){
ex.printStackTrace();
}
}
}
﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡
好了,源代码全贴出来了,如果需要源文件打包我可以站内发给你,虽然只是萍水相逢,还是感谢你的帮助!也虽然无以为报,感激之情无以言表!谢谢啦!我的qq是996475895,谢谢啦!
另外这是我在问问上的问题,竟然没人回答!http://wenwen.soso.com/z/q230068970.htm,有时间回答一下,有分数哦!