来源:http://hi.baidu.com/jsfcn/blog/item/011faf0fc2cff6eaab64578f.html
在很多web应用中,用户都可以下载文件.Seam 2.1.1.CR1 添加了对下载文件的支持,通过s:resource 和s:download标签可以很容易的创建restful的链接.要使用该功能,需要先做些配置.s:resource标签使用Seam的 DocumentStore来做文档服务,需要把该服务添加到web.xml文件中去.
web.xml
<servlet> <servlet-name>Document Store Servlet</servlet-name> <servlet-class>org.jboss.seam.document.DocumentStoreServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Document Store Servlet</servlet-name> <url-pattern/seam/docstore/*</url-pattern> </servlet-mapping>
在页面中, s:resource将作为文件下载提供者来定义在那里得到文件数据和meta-data描述信息.该页面应该和下面页面相似:
resources.xhtml
<s:resource xmlns="http://www.w3.org/1999/xhtml"
xmlns:s="http://jboss.com/products/seam/taglib"
data="#{resources.data}"
contentType="#{resources.contentType}"
fileName="#{resources.fileName}" />
- data 实际的文件数据. 可以为 java.util.File, 一个 InputStream 或则一个 byte[].
- contentType, e.g. “image/jpeg”.
- filename, e.g. “someFile.jpg”
其他参数请参考用户手册.
现在 resource.xhtml 将为下载文件服务,我们还需要传递一些参数来请求特定的文档.通过请求参数来实现该功能.例如:
http://localhost/app/resources.seam?fileId=1. 现在有2种方式来把参数设置到
resources.xhtml的backing bean中,可以通过 @RequestParameter
annotation或则页面定义参数.
@RequestParameter private Long fileId;
或则在 pages.xml 中
<page view-id=”/resources.xhtml”>
<param name="fileId" value="#{resources.fileId }"/>
</page>
Resources.java 代码如下:
@Name("resources")
@Scope(ScopeType.EVENT)
public class Resources
{
@RequestParameter
private String id;
private String fileName;
private String contentType;
private byte[] data;
@Create
public void create()
{
FileItem item = em.find(FileItem.class, fileId); // Get document representation
this.fileName = item.getFileName();
this.data = item.getData();
this.contentType = item.getContentType();
}
//.. getters setters
}
这样就可以通过restful方式来下载文件了,为了让下载文件更加方便,这里还有个s:download标签. 下面是使用示例:
<s:download src=”/resources.xhtml”>
<f:param name=”fileId” value=”#{someBean.downloadableFileId}”/>
</s:download>
这样就可以生成一个下载链接: http://localhost/resources.seam?fileId=1.
Seam 2.1.1.CR1增加了文件下载功能,可通过s:resource和s:download标签轻松创建RESTful链接。需配置DocumentStoreServlet,并在页面使用s:resource指定文件数据和元数据。
62

被折叠的 条评论
为什么被折叠?



