一、前言
通过xwork的MVC框架的com.opensymphony.xwork.Action控制层定义FileUploadAction实现类,将业务数据逻辑通过xml映射配置返回指定jsp模块页面进行视图展示,详情代码示例(完整项目源码参见“webworkDemo完整项目源码工程下载”)。
二、代码示例
1、FileUploadAction类
import java.io.File;@b@import com.opensymphony.xwork.Action;@b@ @b@public class FileUploadAction implements Action{@b@ File picture;@b@ String directory;@b@@b@ public String execute() throws Exception {@b@ picture.renameTo(new File(getPicturePath()));@b@ return SUCCESS;@b@ }@b@ @b@ /**@b@ * @param directory The directory to set.@b@ */@b@ public void setDirectory(String directory) {@b@ this.directory = directory;@b@ }@b@ @b@ /**@b@ * @return Returns the PicturePath.@b@ */@b@ public String getPicturePath() {@b@ return directory+picture.getName();@b@ }@b@ /**@b@ * @param picture The picture to set.@b@ */@b@ public void setPicture(File picture) {@b@ this.picture = picture;@b@ }@b@}
2. xwork-upload.xml配置
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd">@b@<xwork>@b@ <package name="upload" extends="default" namespace="/upload">@b@ <action name="fileUpload" class="com.skyon.demo.upload.FileUploadAction" >@b@ <param name="directory">C:\temp\</param>@b@ <result name="success" type="dispatcher">/upload/uploadSuccess.jsp</result>@b@ <interceptor-ref name="fileUploadStack"/>@b@ </action>@b@ </package>@b@</xwork>
3. fileUpload.jsp、uploadSuccess.jsp页面
<%@page contentType="text/html;charset=GBK"%>@b@<%@taglib prefix="ww" uri="webwork"%>@b@<form name="fileUpload" enctype="multipart/form-data" action="fileUpload.action" method="post">@b@请选择你需要上传的文件:<input type="file" name="picture"><p>@b@<input type="submit" value="上传文件">@b@</form>
<%@page contentType="text/html;charset=GBK"%>@b@<%@taglib prefix="ww" uri="webwork"%>@b@文件上传成功,位置为:<ww:property value="picturePath"/>