zoukankan      html  css  js  c++  java
  • Struts2 的开发应用

    理解MVC设计模式的基本概念和Java Web开发的两种模式Model1和Model2,以及Struts开发工作流程和基本应用。

    使用myeclipse的struts2开发时

    要在软件中添加struts2

    案例使用strust2开发实现网页的文件上传

    建立java类

    代码如下

    package po;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;

    import org.apache.struts2.ServletActionContext;

    import com.opensymphony.xwork2.ActionSupport;

    public class UploadAction extends ActionSupport
    {
    private String title;
    private File upload;
    private String uploadContentType;
    private String uploadFileName;

    //接受依赖注入的属性
    private String savePath;
    //接受依赖注入的方法
    public void setSavePath(String value)
    {
    this.savePath = value;
    }

    private String getSavePath() throws Exception
    {
    return ServletActionContext.getRequest().getRealPath(savePath);
    }

    public void setTitle(String title) {
    this.title = title;
    }

    public void setUpload(File upload) {
    this.upload = upload;
    }

    public void setUploadContentType(String uploadContentType) {
    this.uploadContentType = uploadContentType;
    }

    public void setUploadFileName(String uploadFileName) {
    this.uploadFileName = uploadFileName;
    }

    public String getTitle() {
    return (this.title);
    }

    public File getUpload() {
    return (this.upload);
    }

    public String getUploadContentType() {
    return (this.uploadContentType);
    }

    public String getUploadFileName() {
    return (this.uploadFileName);
    }
    @Override
    public String execute() throws Exception
    {
    System.out.println("开始上传单个文件-----------------------");
    System.out.println(getSavePath());
    System.out.println("==========" + getUploadFileName());
    System.out.println("==========" + getUploadContentType());
    System.out.println("==========" + getUpload());
    //以服务器的文件保存地址和原文件名建立上传文件输出流
    FileOutputStream fos = new FileOutputStream(getSavePath() + "\" + getUploadFileName());
    FileInputStream fis = new FileInputStream(getUpload());
    byte[] buffer = new byte[1024];
    int len = 0;
    while ((len = fis.read(buffer)) > 0)
    {
    fos.write(buffer , 0 , len);
    }
    return SUCCESS;
    }
    }然后在struts.xml中配置

    html代码如下

    网页实现

  • 相关阅读:
    Ultra-wideband (UWB) secure wireless device pairing and associated systems
    程序员常用工具整理
    Net 使用UEditor笔记
    社交中的黄金法则,你要细细体会品味
    社交中的黄金法则,你要细细体会品味
    社交中的黄金法则,你要细细体会品味
    交际中你所不知道的说话的12个技巧!
    交际中你所不知道的说话的12个技巧!
    交际中你所不知道的说话的12个技巧!
    好好的活,简简单单过!
  • 原文地址:https://www.cnblogs.com/liuliuyiming/p/7806753.html
Copyright © 2011-2022 走看看