页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>addChairCushion.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<h3>ChairCushion 添加页面</h3>
<hr color="red">
<html:form action="operateChairCushion.do?method=addChairCushion" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>chairCushionName:</td>
<td>
<html:text property="chairCushionName" size="30">
</html:text>
</td>
<td></td>
</tr>
<tr>
<td>material:</td>
<td>
<html:text property="material" size="30">
</html:text>
</td>
<td></td>
</tr>
<tr>
<td>filling:</td>
<td>
<html:text property="filling" size="30">
</html:text>
</td>
<td></td>
</tr>
<tr>
<td>style:</td>
<td>
<html:text property="style" size="30">
</html:text>
</td>
<td></td>
</tr>
<tr>
<td>productUse:</td>
<td>
<html:text property="productUse" size="30">
</html:text>
</td>
<td></td>
</tr>
<tr>
<td>shape:</td>
<td>
<html:text property="shape" size="30">
</html:text>
</td>
<td></td>
</tr>
<tr>
<td>productPictureFile:</td>
<td><html:file property="productPictureFile" size="30"></html:file></td>
<td></td>
</tr>
<tr>
<td colspan="3" align="center">
<html:submit value="添加value">添加</html:submit>
<html:reset value="重置value">重置</html:reset>
</td>
</tr>
</table>
</html:form>
</center>
</body>
</html>
action:
public class LoadAction extends Action {
public ActionForward execute(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest servletRequest,
HttpServletResponse servletResponse)throws Exception {
LoadForm loadForm = (LoadForm) actionForm;
FormFile ff=loadForm.getFile();
if(ff==null)
{
return actionMapping.findForward("success");
}
String fname=ff.getFileName();
String size=Integer.toString(ff.getFileSize())+"bytes";
InputStream in=ff.getInputStream();
OutputStream out=new FileOutputStream("/"+fname);
int bytesRead=0;
byte[] buffer=new byte[8192];
while((bytesRead=in.read(buffer,0,8192))!=-1)
{
out.write(buffer,0,bytesRead);
}
out.close();
in.close();
loadForm.setFname(fname);
loadForm.setSize(size);
ff.destroy();
return actionMapping.findForward("success");
}
}
有以下几点需要注意:
1: <html:file>必须嵌套在<html:form>标签中。
2:<html:form>标签的method的属性必须设为"post".
3:<html:form>标签的编码类型enctype属性必须为"multipart/form-data"。
4:<html:file>标签必须设为property属性,这个属性和ActionForm Bean中FormFile类型的属性对应。
5:<html:text property="" disabled="true"></html:text>后,form获取不到值
6:<html:text property="" readonly="true"></html:text>后,可以获取值,而且页面不能修改值