请打大家帮我看一下struts2 action 跳转问题。提示:404 谢谢 !
出现的错误是 :
HTTP Status 404 - /pic_savePicInfo.action
--------------------------------------------
type Status report
message /pic_savePicInfo.action
description The requested resource (/pic_savePicInfo.action) is not available.
jsp 页面的form:
<s:form action="../pic_savePicInfo.action" enctype="multipart/form-data" method="post">
<tr>
<td align="right">标题:
<s:hidden name="classid" value="1"/></td>
<td><s:textfield name="picname"></s:textfield></td>
</tr>
<tr>
<td align="right">图片存放路径:</td>
<td><s:textfield name="url"></s:textfield></td>
</tr>
<tr>
<td align="right">上传:</td>
<td><s:file name="imagefile"></s:file></td>
</tr>
<tr>
<td align="right">排序:</td>
<td><s:textfield name="number"></s:textfield>(按正常顺序,数值小于10)</td>
</tr>
<tr>
<td> </td>
<td><s:submit type="submit" value="上传图片"></s:submit></td>
</tr>
</s:form>
struts2 的action:
<action name="pic_*" class="PicUploadAction" method="{1}">
<interceptor-ref name="defaultStack"></interceptor-ref>
<!-- 是struts2为我们提供的文件上传的拦截器 -->
<interceptor-ref name="fileUpload">
<!--允许上传的类型 -->
<param name="allowedTypes">image/gif,image/jpeg</param>
<!-- 允许文件大小 -->
<param name="maximumSize">20000000</param>
</interceptor-ref>
<result name="success">admin/pictureShow.jsp</result>
<result name="error">/admin/main.jsp</result>
<result name="input">/upload.jsp</result>
</action>
spring的 bean 配置:
<!-- pic -->
<bean id="PicBizImp" class="com.biz.PicBizImp" scope="prototype">
<property name="picdao" ref="PfpictureDAO"></property>
</bean>
<bean id="PicUploadAction" class="com.action.PicUploadAction">
<property name="picbiz" ref="PicBizImp"></property>
</bean>
Action:
package com.action;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.pojo.Pfpicture;
import com.pojo.Pftype;
public class PicUploadAction extends ActionSupport{
/*
private Integer id;
private Pftype pftype;//图片类型是大的还是小的
private Integer number;//用来排序
private String picname;//图片名称
private String pictrueAddress;//图片存放地址
private String bigPicture;//大图片存放地址
private String smailPicture;//小图片存放地址
private String flag;//状态
private String info;//说明
*/
private Pftype classid;
private String picname;
private String url;
private File imagefile;
private String number;
private String smallImageFile;
private String imagefileFileName; // 上传文件名 ***FileName 表示上传文件名
private String imagefileContentType; // 文件的上传类型 ***ContentType 表示上传文件的类型
private com.biz.PicBizInf picbiz;
//-----------------------------
//用于获取上传文件的后缀
public static String getExtention(String imageFile) {
int pos = imageFile.lastIndexOf(".");
return imageFile.substring(pos);
}
public String execute() throws Exception {
// 为保存客户端上传的文件构造一个新的文件名
String newFileName = picname
+ this.getExtention(this.getImagefileFileName());
System.out.println("this.getMyfileFileName()---"+this.getImagefileFileName());
System.out.println("newFileName---"+newFileName);
// getServletContext().getRealPath()获取当前应用的根目录
File file = new File(org.apache.struts2.ServletActionContext
.getServletContext().getRealPath("\\"+url+"\\" + newFileName));
if (!file.getParentFile().exists()) {// 判断路径是否存在
file.getParentFile().mkdirs(); // 创建一个新的
}
try {
// import org.apache.commons.io.FileUtils;
FileUtils.copyFile(imagefile, file);// 将上传的文件拷贝到服务器物理地址中 //myfile 指旧文件
// file 新文件
imagefileFileName=newFileName;
return SUCCESS;
} catch (Exception e) {
return ERROR;
}
}
public void savePicInfo(Pftype classid,String picname,String url,String imagefile,String smallImageFile,
String imagefileFileName) {
// TODO Auto-generated method stub
Pfpicture spi=new Pfpicture();
spi.setPicname(picname);
spi.setPictrueAddress(imagefileFileName);
spi.setPftype(classid);
System.out.println("|------------------------------------");
picbiz.save(spi);
}
//--------------------------------
public String getUrl() {
return url;
}
public Pftype getClassid() {
return classid;
}
public void setClassid(Pftype classid) {
this.classid = classid;
}
public void setUrl(String url) {
this.url = url;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getSmallImageFile() {
return smallImageFile;
}
public void setSmallImageFile(String smallImageFile) {
this.smallImageFile = smallImageFile;
}
public String getPicname() {
return picname;
}
public void setPicname(String picname) {
this.picname = picname;
}
public File getImagefile() {
return imagefile;
}
public void setImagefile(File imagefile) {
this.imagefile = imagefile;
}
public String getImagefileFileName() {
return imagefileFileName;
}
public void setImagefileFileName(String imagefileFileName) {
this.imagefileFileName = imagefileFileName;
}
public String getImagefileContentType() {
return imagefileContentType;
}
public void setImagefileContentType(String imagefileContentType) {
this.imagefileContentType = imagefileContentType;
}
public com.biz.PicBizInf getPicbiz() {
return picbiz;
}
public void setPicbiz(com.biz.PicBizInf picbiz) {
this.picbiz = picbiz;
}
}
[解决办法]
<s:form action="../pic_savePicInfo.action" enctype="multipart/form-data" method="post">
action的路径不要写相对路径,你写成绝对路径
[解决办法]
看看web.xml中servlet(Action)如何映射的 urlpattern
还有Action的请求路径是否之类的
action="/yourAppContext/XyzAction">
[解决办法]
我认为<s:form action="../pic_savePicInfo.action" enctype="multipart/form-data" method="post">这句action的问题,路径不对。
[解决办法]
<s:form action="../pic_savePicInfo.action" enctype="multipart/form-data" method="post">
问题还是在../pic_savePicInfo.action。
建议写法:
<s:form action="pic_savePicInfo.action" namespace="pic_savePicInfo请求所在命名空间" enctype="multipart/form-data" method="post">
[解决办法]
肯定是路径有问题
你再把路径好好查一遍
[解决办法]
是pic_savePicInfo.action没配的对,没看到这个XML,不过../pic_savePicInfo.action可能有问题