当前位置:编程学习 > JAVA >>

关于struts2配置下一个上传文件映射错误

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>多文件上传</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  
  <body>
   服务器将处理多文件上传
   <s:form  action="upload.action"  method="post" enctype="multipart/form-data">
   <s:file name="upload" label="上传文件一"></s:file>
   <s:file name="upload" label="上传文件二"></s:file>
   <s:file name="upload" label="上传文件三"></s:file>
   <s:submit value="上传"></s:submit>
   </s:form>
  </body>
</html>



package file.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.imageio.stream.FileImageInputStream;



import com.opensymphony.xwork2.ActionSupport;


public class UploadAction extends ActionSupport{
//私有的静态常量   上传文件的路径地址(文件夹提前创建)
private static final String PATH = "D:\\java\\项目测试\\UploadFlie\\";
//版本控制 
//序列化时保持版本的兼容性,在版本升级时反序列化仍保持对象的唯一性
private static final long seriaVersionUID =1L;
private List<File> upload;
private List<String> uploadFileName;
public List<File> getUpload(){
return upload;
}
public void setUpload(List<File> upload){
this.upload=upload;
}
public List<String> getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(List<String> uploadFileName){
this.uploadFileName = uploadFileName;
}
public String execute() throws Exception{
//判断文件是否为空
if(upload!=null){
//循环遍历文件
for(int i=0;i<upload.size();i++){
//取到文件流
InputStream is =new FileInputStream(upload.get(i));
//创建输出流
OutputStream os= new FileOutputStream(PATH+uploadFileName.get(i));
//缓存字节
byte buffer[]=new byte[1024];
//缓存字节
int count=0;
//int read(byte[] b) 
//从输入流中读取一定数量的字节并将其存储在缓冲区数组 b 中。 
while ((count=is.read(buffer))>0){
os.write(buffer,0,count);

}
os.close();
is.close();
}

}else {
return INPUT;
}
return SUCCESS;
}




}


<?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.devMode" vale="true"/>
<!-- 规定上传文件大小 -->
<constant name="struts.multipart.maxSize" value="102400000"></constant>
<!-- 配置action -->
<package nam="default" extends="struts-default" >
<action name="upload" class="file.action.UploadAction">
<result name="success" >/success.jsp</result>
<result name="input">/input.jsp</result>
</action>
</package>

</struts>


<?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">
  <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  <!-- 拦截所有请求 -->
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


报错信息:警告: Could not find action or result
There is no Action mapped for namespace / and action name upload. - [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:178)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:478)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
--------------------编程问答-------------------- <package nam="default" extends="struts-default" > --------------------编程问答--------------------
引用 1 楼  的回复:
<package nam="default" extends="struts-default" >
改过来了,还是报错

2012-9-24 0:07:37 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find action or result
There is no Action mapped for namespace / and action name upload. - [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:178)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:478)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
--------------------编程问答-------------------- struts.xml 确实是放在 src 目录中了么?也就是确认其会被发布到:WEB-INF\classes目录中? --------------------编程问答-------------------- 不错哦 --------------------编程问答-------------------- 我认为是很狗血的错误。 --------------------编程问答--------------------
引用 3 楼  的回复:
struts.xml 确实是放在 src 目录中了么?也就是确认其会被发布到:WEB-INF\classes目录中?
我确认放置到SRC目录,但还是以上错误
--------------------编程问答-------------------- 表单里面加上一个
namespace="/"试试看 --------------------编程问答-------------------- 你的struts配置文件,名字错了,你写成strutsl.xml了。 --------------------编程问答--------------------
引用 8 楼  的回复:
你的struts配置文件,名字错了,你写成strutsl.xml了。

哥们慧眼呀,在配置时最怕的就是粗心大意了,写错一个单词找半天的错误(在能找到的前提下),LZ要细心哦 --------------------编程问答-------------------- 这种问题确实够让人哭笑不得。不过也确实犯过类似的问题。 --------------------编程问答--------------------
引用 8 楼  的回复:
你的struts配置文件,名字错了,你写成strutsl.xml了。


多了个 L, 害死人啊。。。还好楼主贴了图。
补充:Java ,  Web 开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,