Struts2 的开发应用
Posted Marvellous
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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代码如下
网页实现
以上是关于Struts2 的开发应用的主要内容,如果未能解决你的问题,请参考以下文章
在Java构建Web应用程序中 什么是Struts2?定制开发电商购物小程序构成条件是什么?