补充:如果你不是用ajax技术,不会Facelets ,也不会自己客制底层的类似process的实现,那么JSF只有在JSP页面下才可以使用,离开JSP根本就用不了,如果你非要在纯HTML调用JSF的方法,通过ajax访问中转jsp页面或servlet等服务是最好的方式,如果不会用,那你就在HTML页面里加一个跳转动作,跳转到一个跟它长的一摸一样的jsp里做JSF的绑定处理。因为JSF标签只能在JSP里面使用。
html是静态页面,执行端在客户也就是前台,backbean是java类,部署在服务器上,是后台服务器端。
想在静态html页面调用服务器上的类的方法,目前推荐使用的是Ajax技术,当然你也可以自己研究底层通讯技术写一些封装html标签,例如很早之前的Process技术,以及被大部分人遗忘的applet技术等等。
最简单、方便可行、不需要提交页面也就可以调用backbean的方式还是用Ajax吧。
如果你对Ajax没有研究,推荐你使用JQuery封装好的Ajax来实现普通html中调用服务器类的方法,上手比较容易。
使用jsf加myfaces的upload插件实现上传文件的功能需要的jar包:tomahawk-1.1.6.jar,jsf-impl.jar,commons-fileupload-1.2.jar,commons-io-1.3.2.jar,commons-el-1.0.jar
程序:
package model.map
import control.MapMDelegate
import java.io.BufferedInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Random
import javax.faces.application.FacesMessage
import javax.faces.context.FacesContext
import org.apache.myfaces.custom.fileupload.UploadedFile
public class FileUploadForm {
public FileUploadForm() {
}
private String name
private UploadedFile upFile
public void setName(String name) {
this.name = name
}
public String getName() {
return name
}
public void setUpFile(UploadedFile upFile) {
this.upFile = upFile
}
public UploadedFile getUpFile() {
return upFile
}
public String upload() throws IOException {
try {
InputStream in = new BufferedInputStream(upFile.getInputStream())
try {
byte[] buffer = new byte[(int)upFile.getSize()]
Date now = new Date()
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss")
String dt2 = sdf2.format(now)
//取得后缀名
String name = upFile.getName()
int l = name.lastIndexOf(".")
name = name.substring(l)
//生成2位随机数
Random random = new Random()
String sRand=""
String rand = null
for (int count=0count<2count++){
rand=String.valueOf(random.nextInt(10))
sRand+=rand
}
//生成在服务器端保存的文件名
String saveName = dt2 + sRand + name
this.setName(saveName)
String trace = D:\\"+ saveName//本地测试
FileOutputStream fileOutputStream =
new FileOutputStream(trace)//这里可以把上传的文件写服务器目录,或者数据库中 ,或者赋值给name用于文件名显示
while (in.read(buffer) >0) {
fileOutputStream.write(buffer)
}
} catch (Exception x) {
System.out.print("Exception")
FacesMessage message =
new FacesMessage(FacesMessage.SEVERITY_FATAL,
x.getClass().getName(), x.getMessage())
FacesContext.getCurrentInstance().addMessage(null, message)
return null
}
finally {
in.close()
FacesContext facesContext = FacesContext.getCurrentInstance()
facesContext.getExternalContext().getApplicationMap().put("fileupload_bytes",
upFile.getBytes())
facesContext.getExternalContext().getApplicationMap().put("fileupload_type",
upFile.getContentType())
}
System.out.println("End")
return "OK"
} catch (Exception x) {
System.out.print("Exception")
FacesMessage message =
new FacesMessage(FacesMessage.SEVERITY_FATAL,
x.getClass().getName(), x.getMessage())
FacesContext.getCurrentInstance().addMessage(null, message)
return null
}
}
public boolean isUploaded() {
FacesContext facesContext = FacesContext.getCurrentInstance()
return facesContext.getExternalContext().getApplicationMap().get("fileupload_bytes") !=
null
}
}
页面:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page contentType="text/htmlcharset=GBK"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>
<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=GB18030"/>
<title>add-map</title>
</head>
<body>
<h:outputText value="#{MapMDelegate.msg}" style="color:rgb(255,0,0)"/>
<h:form enctype="multipart/form-data">
<t:inputFileUpload value="#{fileuploadForm.upFile}"/>
<br/>
<h:message for="fileupload"/><h:commandButton value="上传"
action="#{fileuploadForm.upload}" />
</h:form>
<h:form>
<br/>
图纸名称 <h:inputText value="#{fileuploadForm.name}" disabled="true"/>
</h:form>
</body>
</html>
</f:view>
记得要在工程的web.xml文件中加入filter
<!--Tomahawk-->
<filter>
<filter-name>extensionsFilter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
<init-param>
<description>Set the size limit for uploaded files.
Format: 10 - 10 bytes
10k - 10 KB
10m - 10 MB
1g - 1 GB</description>
<param-name>uploadMaxFileSize</param-name>
<param-value>10m</param-value>
</init-param>
<init-param>
<description>Set the threshold size - files
below this limit are stored in memory, files above
this limit are stored on disk.
Format: 10 - 10 bytes
10k - 10 KB
10m - 10 MB
1g - 1 GB</description>
<param-name>uploadThresholdSize</param-name>
<param-value>1k</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>extensionsFilter</filter-name>
<url-pattern>/faces/*</url-pattern>
</filter-mapping>
还有把你的程序设置成request哦
<managed-bean>
<managed-bean-name>fileuploadForm</managed-bean-name>
<managed-bean-class>model.map.FileUploadForm</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>