我已经在实体类里获取到了服务器上的绝对路径 如:
public String getImageFile() {
String path=FacesContext.getCurrentInstance().getExternalContext().getRealPath("/resources/img/"); //获得上传文件的服务器上的绝对路径
path+="\\";
this.imageFile=path+imageFile;
System.out.println("imageFile="+imageFile);
return this.imageFile;
}
public void setImageFile(String imageFile) {
this.imageFile = imageFile;
}输出了:
imageFile=D:\workspance\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\rechfaces01\resources\img\b0005.jpg页面:
<rich:dataTable value="#{bookbean.books}" var="b" id="mytable" rowKeyVar="r" rows="5">
<rich:column>
<f:facet name="header">
<input type="checkbox" title="请选择"></input>
</f:facet>
<h:selectBooleanCheckbox value="#{b.check}"></h:selectBooleanCheckbox>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="序号"/>
</f:facet>
<h:outputText value="#{r+1}"/>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="图书编号"/>
</f:facet>
<h:outputText value="#{b.bookId}"/>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="图书名"/>
</f:facet>
<h:outputText value="#{b.bookName}"/>
<rich:tooltip>
<h:graphicImage name="#{b.imageFile}" height="100" width="100"></h:graphicImage>
</rich:tooltip>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="作者"/>
</f:facet>
<h:outputText value="#{b.author}"/>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="出版社"/>
</f:facet>
<h:outputText value="#{b.publisher}"/>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="出版日期"/>
</f:facet>
<h:outputText value="#{b.publishingTime}"/>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="价格"/>
</f:facet>
<h:outputText value="#{b.price}"/>
</rich:column>
<f:facet name="footer">
<rich:dataScroller page="3" maxPages="5">
<f:facet name="first">
<h:outputText value="首页"/>
</f:facet>
<f:facet name="last">
<h:outputText value="末页"/>
</f:facet>
<f:facet name=""></f:facet>
</rich:dataScroller>
</f:facet>
</rich:dataTable> private DataModel<Book> books;
public DataModel<Book> getBooks() {
if(books==null){
books=new ListDataModel<Book>();
books.setWrappedData(new BookDao().findAll());
}
return books;
}
public void setBooks(DataModel<Book> books) {
this.books = books;
}
public String getImageFile() {
String path=FacesContext.getCurrentInstance().getExternalContext().getRealPath("/resources/img/"); //获得上传文件的服务器上的绝对路径
path+="\\";
this.imageFile=path+imageFile;
System.out.println("imageFile="+imageFile);
return this.imageFile;
}
public void setImageFile(String imageFile) {
this.imageFile = imageFile;
}输出了:
imageFile=D:\workspance\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\rechfaces01\resources\img\b0005.jpg页面:
<rich:dataTable value="#{bookbean.books}" var="b" id="mytable" rowKeyVar="r" rows="5">
<rich:column>
<f:facet name="header">
<input type="checkbox" title="请选择"></input>
</f:facet>
<h:selectBooleanCheckbox value="#{b.check}"></h:selectBooleanCheckbox>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="序号"/>
</f:facet>
<h:outputText value="#{r+1}"/>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="图书编号"/>
</f:facet>
<h:outputText value="#{b.bookId}"/>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="图书名"/>
</f:facet>
<h:outputText value="#{b.bookName}"/>
<rich:tooltip>
<h:graphicImage name="#{b.imageFile}" height="100" width="100"></h:graphicImage>
</rich:tooltip>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="作者"/>
</f:facet>
<h:outputText value="#{b.author}"/>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="出版社"/>
</f:facet>
<h:outputText value="#{b.publisher}"/>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="出版日期"/>
</f:facet>
<h:outputText value="#{b.publishingTime}"/>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="价格"/>
</f:facet>
<h:outputText value="#{b.price}"/>
</rich:column>
<f:facet name="footer">
<rich:dataScroller page="3" maxPages="5">
<f:facet name="first">
<h:outputText value="首页"/>
</f:facet>
<f:facet name="last">
<h:outputText value="末页"/>
</f:facet>
<f:facet name=""></f:facet>
</rich:dataScroller>
</f:facet>
</rich:dataTable> private DataModel<Book> books;
public DataModel<Book> getBooks() {
if(books==null){
books=new ListDataModel<Book>();
books.setWrappedData(new BookDao().findAll());
}
return books;
}
public void setBooks(DataModel<Book> books) {
this.books = books;
}
解决方案 »
- jsp 导出word,如何搞?
- 在hibernate官网下载的hibernate3.3 解压后lib目录下面找不到commons-logging-*.jar包
- 用struts2上传文件得到文件路径
- 如何把带有子查询的SQL转换成HQL
- 新手spring+ibatis问题,看不懂问题出在哪里
- 请问struts结合hibernate该怎么做?急!!!
- 初学j2ee,安装sun application server时遇到一个问题?
- 欢迎加入我的群
- 加分!(在线等待)JBuilder7 和 WebLogic7 可不可以装在不同的机器上开发
- 关于weblogic的问题
- s2sh 本人需要一个例子
- JSP问题求解
<!-- 上传封面面板 -->
<rich:popupPanel id="uploadPanel" autosized="true">
<f:facet name="header">
<h:outputText value="上传图书封面图片"/>
</f:facet>
<f:facet name="controls">
<h:graphicImage name="/img/close.png" onclick="#{rich:component('uploadPanel')}.hide()"/>
</f:facet>
<h:form>
<rich:fileUpload acceptedTypes="jpg,png,gif,bmp"
deleteLabel="删除"
addLabel="添加"
clearAllLabel="删除所有"
uploadLabel="上传"
fileUploadListener="#{bookModel.uplaodFile}"
render="myTable"
onuploadcomplete="#{rich:component('uploadPanel')}.hide()"/>
</h:form>
</rich:popupPanel>
public void uplaodFile(FileUploadEvent event){
UploadedFile upload = event.getUploadedFile();
try{
//URL url = getClass().getResource("/");
//获取项目绝对路径
String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/");
System.out.println("path="+path);
//path = path.substring(0,path.indexOf("/WEB-INF"));
//创建文件对象
File file = new File(path+"/resources/img/",upload.getName());
//文件输出流对象
FileOutputStream out = new FileOutputStream(file);
//写入文件
out.write(upload.getData());
//刷新
out.flush();
out.close();
}catch(Exception e){
e.printStackTrace();
}
}