在学习Struts2文件上传功能开发时,遇见一个问题,请高手进来帮我解决一下,谢谢啦!!!

爱吃猫的萌鱼 2010-10-24 10:14:19
我用的是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,有时间回答一下,有分数哦!
...全文
78 4 点赞 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
嘿嘿,谢谢 啊!真的,太感谢你了!现在没有报错了!就是出现了一点小问题,我想自己先找找!相信你是这方面的高手!能加你QQ吗?我想。。。嘿嘿,我都是自学Struts2,没有名师指导出错了还真是措手不及。。。以后有什么不懂多向你指教,虽然没有什么物质上的奖励,虽然也是只有感激之情,可是,对于我而言真是莫大的帮助,谢谢啦!qq:996475895!谢谢啦!真的,发自内心的!愿你好人有好报!
  • 打赏
  • 举报
回复
magicluo 2010-10-24
把这个代码加入到你的web.xml中

<!-- 配置struts2的核心过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

然后重启你的web服务器 就应该没打问题了

最核心的地方你没配
  • 打赏
  • 举报
回复
首先非常感谢你的建议!
你这么一说我才发现我没有配置web.xml
我是自己自学struts2,所以这些配置还不是很熟悉,我打开我的web.xml,发现是这样的
﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡
<?xml version="1.0" encoding="UTF-8" ?>
- <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡﹡
能教教我怎么配置吗?谢谢啦!不胜感激,真的,发自内心的感谢您!
  • 打赏
  • 举报
回复
magicluo 2010-10-24
我看你的问题出在 web.xml中
错误提示说的比较明白了,这通常是由于不经由Filter访问包含struts标签的jsp页面导致的


把web.xml文件中关于struts2 的过滤器改为 /* 看看

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
  • 打赏
  • 举报
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2010-10-24 10:14
社区公告
暂无公告