码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2 的开发应用

时间:2017-11-08 23:29:30      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:XML   inpu   set   rust   package   exe   apach   adc   保存   

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

技术分享

网页实现

技术分享

 

Struts2 的开发应用

标签:XML   inpu   set   rust   package   exe   apach   adc   保存   

原文地址:http://www.cnblogs.com/liuliuyiming/p/7806753.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!