1

I am showing the image path in the class file as below

String val;
val+= "<img src=/"PATH_TO_FILE/" alt=/"sometext/">"

and trying to load the image in gsp view in a div using jquery which is in the val variable.

Image is successfully found in the specified path, but the problem is that, image is not displaying in the gsp page.

I do not want to load image with the following Grails tag given below:

<img src="${resource(dir: 'images', file: image.name)}" alt="Grails"/>

Is there any limitations in Grails using normal html img tag in Grails class to load image in view page?

4

1 回答 1

1

使用反斜杠而不是正斜杠,例如:

行动:

def test() {
    String val = "<img src=\"http://localhost:8080/formwizard/images/asd.png\" alt=\"sometext\">"
    [val: val]
}

看法:

<script>
    $(document).ready(function () {
        $("#divId").html('${val}');
    });
</script>
<div id="divId"></div>

注意:-如果您在操作 val 字符串中使用"( ),则在脚本中使用( ) 来获取它,如果使用( ),则在脚本中使用。 String val = "<img src=\"http://localhost:8080/formwizard/images/asd.png\" alt=\"sometext\">"''${val}''"<img src='http://localhost:8080/formwizard/images/asd.png' alt='sometext'>""${val}"

如果您使用的是ajax,那么

行动:

def test2() {
    String val = "<img src=\"http://localhost:8080/formwizard/images/asd.png\" alt=\"sometext\">"
    render val
}

看法:

<script>
  function getImg() {
    $.ajax({
      url: "${createLink(controller: 'dashboard', action: 'test2')}",
      success: function (data) {
        $("#divId").html(data);
      }
    });
  }
</script>
<a href="javascript:void(0)" onclick="getImg()">Get Image</a>
<div id="divId"></div>

编辑................................................. .....................

将您的代码更改为:

def grailsLinkGenerator
def test2() {
    def basePath = grailsLinkGenerator.serverBaseURL
    String val = "<td><a href='adddd'><img width=\"50\" height=\"50\" src=" + basePath + "/images/repository/HrCrEmp/PIC_/0004-001.png alt=\"Fawkner Park\"></a></td>";
    render val
}

注意:-如果图像扩展名是png并且如果图像扩展名是然后从图像名称jpg中删除连字符( )符号,则工作正常。-

于 2013-11-05T14:05:03.903 回答