Struts 2 问题
各路大神,我在用struts 2 写上传文件时,老是报错。但是,我不知道怎么修改。
下面是服务器提示的错误
No result defined for action action.Upload and result success
description The requested resource (No result defined for action action.Upload and result success) is not available
下面是struts-xml代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="main" extends="struts-default">
<action name="loginPerson"
class="action.Login">
<result name="login">/Login.jsp</result>
<result name="success">/Success.jsp</result>
</action>
<action name="upload"
class="action.Upload">
<result name="input">/Success.jsp</result>
<result name="success">/Upsuccess.jsp</result>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">application/msword</param>
</interceptor-ref>
</action>
</package>
</struts>
下面是/Success.jsp 的代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/struts-tags" prefix="struts"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Success.jsp' starting page</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>
<struts:actionerror/>
<struts:form action="upload" enctype="multipart/form-data" method="post">
<struts:label value="上传文件"></struts:label>
<struts:file name="picture" label="文件"></struts:file>
<struts:submit value="开始上传" method="upload"></struts:submit>
</struts:form>
</body>
</html>
下面是Upload 的代码
package action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Upload extends ActionSupport
{
private File picture;
private String pictureContentType;
private String pictureFileName;
public String execute()
{
return "input";
}
public String upload() throws Exception
{
File saved = new File(ServletActionContext.getServletContext().getRealPath("upload"),pictureFileName);
InputStream ins = null;
OutputStream ous = null;
try
{
saved.getParentFile().mkdirs();
ins = new FileInputStream(picture);
ous = new FileOutputStream(saved);
byte [] b = new byte[1024];
int len = 0;
while((len=ins.read(b))!= -1)
{
ous.write(b,0,len);
}
}catch(Exception e)
{
e.printStackTrace();
}finally
{
if(ous != null) ous.close();
if(ins != null) ins.close();
}
return SUCCESS;
}
public void prepare() throws Exception
{
// Clear last error messages
clearErrorsAndMessages();
}
public File getPicture()
{
return picture;
}
public void setPicture(File picture)
{
this.picture = picture;
}
public String getPictureContentType()
{
return pictureContentType;
}
public void setPictureContentType(String pictureContentType)
{
this.pictureContentType = pictureContentType;
}
public String getPictureFileName()
{
return pictureFileName;
}
public void setPictureFileName(String pictureFileName)
{
this.pictureFileName = pictureFileName;
}
}
下面是/Upsuccess.jsp的代码
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>My JSP 'Upsuccess.jsp' starting page</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>
上传成功
</body>
</html>
我的Upsuccess.jsp 在 WebRoot 目录下
[解决办法]
我以前做的有个示例,现在贴出来LZ看看对你有什么帮助没有……
html代码:
<tr><td align="right">业务图片:</td><td><input type="file" name="file" id="imgurl" style="border:1px solid #7F9DB9;"/><td> </tr>
[解决办法]
<action name="upload"
class="action.Upload">
<result name="input">/Success.jsp</result>
<result name="success">/Upsuccess.jsp</result>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">application/msword</param>
</interceptor-ref>
</action>
No result defined for action action.Upload and result success
没有发现你配置的没有返回值为 success 的对象方法呀...
[解决办法]
你把action 的名字命名为UploadAction 然后改改相应的代码,首先连action都没认。