mysql数据库中image字段保存的是图片的路径。
Struts2  相应代码如下:
  public class LawAction extends ECPageAction {
   private File  image;
   private  String imageFileName;   //set  get方法 public String add() throws IOException{
  String realPath=org.apache.struts2.ServletActionContext.getServletContext().getRealPath("/image");//图片的真
        实路径
    if(image!=null){
 File saveFile=new File(new File(realPath),imageFileName);
          if(!saveFile.getParentFile().exists())//文件夹是否存在
 saveFile.getParentFile().mkdirs();//文件夹不存在就创建
 FileUtils.copyFile(image, saveFile);
   
   }
      law.setImage(realPath+"\\"+imageFileName);//路径加文件名  为law对象的image属性赋值。
      lawService.insertLaw(law);//调用方法执行操作
      return SUCCESS   
     }
 } law对象的image属性就对应着数据库中表中的 image字段(存放着图片路径)。
 然后我再jsp页面如此操作:
  <img src="${law.image }" />
  结果图片先不出来!打开数据库看到 image字段中存储这这样的路径:
    D:\AllWorkspace\MyTest\.metadata\.plugins\org.eclipse.wst.server.core\tmp0  
     \wtpwebapps\ldfw\image\dsdsddasd.jpg请问 我要怎么要才能让图片显示出来呢??

解决方案 »

  1.   

    路径是错的,网站上线以后,你会发现,这样的路径是读不到图片的。src中得加主机头,然后再去加图片路径。
    还有el表达式,难道不需要把对象放入到session中吗?
      

  2.   

    去看哥的博客。strus2的。里边有。
      

  3.   

    你存绝对路径了,  其实应该是显示的出来的!
    上个项目一些同事就喜欢这么搞, 然后工程名换了都傻了!
    存的时候只存/image/....着一块,
    然后取的时候用Servlet API去获得前面的/contextPath这部分, 然后拼起来存入作用域
      

  4.   

    imageFileName是怎么得到结果的呢,你最好syso一下imageFileName,可能是你没作处理,保存的时候把文件的绝对路径也保存进去了,ie6和ie8的结果就会不一样。
      

  5.   


    恩 是这样子吗?把 realPath改成  realPath="/image" 然后 
    law.setImage(realPath+"/"+imageFileName);
    那么 jsp页面怎么写啊?<img src="。写什么呢?">
    谢谢各位
      

  6.   

    jsp如果用myeclipse的话应该有个basepath的吧,就用那个拼就好了