【问题标题】:Image source unknown when url is returned from controller从控制器返回 url 时图像来源未知
【发布时间】:2020-09-06 05:45:26
【问题描述】:

我是 Ui 设计的新手,我正在尝试使用 JSP 和 Spring Boot 编写简单的应用程序。 思路是在控制器中获取谷歌二维码URL,返回JSP页面:

https://chart.googleapis.com/chart?chs=200x200&chld=M%%7C0&cht=qr&chl=otpauth%3A%2F%2Ftotp%2FSpringRegistration%3Atest_user%3Fsecret%3DI2S5OTJXDG5NBWVY%26issuer%3DSpringRegistration

所以,我的控制器如下所示:

@GetMapping("/confirmRegistration")
public String confirmRegistration(final HttpServletRequest request,
                                  final Model model) {
    final User user = userService.getUser(token);
    String qrUrl = userService.generateQRUrl(user);
    log.info("QR URL: {}", qrUrl);
    model.addAttribute("qr_code", qrUrl);
    return "redirect:/qrcode";
}

我的 JSP 看起来像 -

<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html lang="en">
  <head>
    <title>Confirm your account</title>
  </head>
  <body>
    <label>Scan following QR code.</label>
    <c:if test="${param.url != null}">
        <span> hi there </span>
        ${param.url}
    </c:if>
    <c:url value="${qr_code}" />
    <img src="${qr_code}" width=256 height=256 />
    <br />
    <img src="${param.qr[0]}"/>
  </body>
</html>

但它没有填充图像。 UI 页面如下所示:

有人可以帮我吗?

【问题讨论】:

  • log.info("QR URL: {}", qrUrl); 给了什么?这是否为 nqrUrl 提供了正确的值?
  • 是的,它会打印网址,如果我在浏览器中打开该网址,我可以看到二维码。

标签: spring-boot spring-mvc jsp


【解决方案1】:

我认为,这个问题与return 声明有关。您应该指向带有图像 url 的模型转发到的 jsp 页面的名称。但在此之前,需要定义和配置InternalResourceViewResolver

欲了解更多信息(redirecting / forwarding

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多