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代码如下

    网页实现

  • 相关阅读:
    jquery序列化form表单使用ajax提交后处理返回的json数据
    log4net VS2012 日志layout自定义,error日志和info日志分别记录到不同文件中
    Bootstrap 填坑1 栅格浮动问题
    C# MVC Transaction 事务
    C# MVC 用户登录状态判断
    SQL中MAX()和MIN()函数的使用
    简单表的 分页存储过程 (不支持多表连接)
    SQL Server 生成数据库代码,包含数据
    js 将数值显示为金额
    Jquery.Validate 扩展方法
  • 原文地址:https://www.cnblogs.com/liuliuyiming/p/7806753.html
Copyright © 2011-2022 走看看