Vaadin 导航到@RequestMapping
Vaadin navigating to @RequestMapping
我正在使用 Springboot 和 Vaadin 创建一个网络应用程序。
我确实编写了一个实现@RequestMapping 方法的控制器。此方法生成一个我应该能够在浏览器中查看或下载的 PDF。
如果我直接转到 link,一切正常。但是,当我使用 Vaadin 的导航时,我确实遇到了一些错误。 (但是当我刷新该页面时,它确实有效)。
下面你可以找到我的代码:
@RestController
public class PDFController {
@RequestMapping(value = "fichedownload", method = RequestMethod.GET,produces = MediaType.APPLICATION_PDF_VALUE)
public static ResponseEntity<InputStreamResource> ficheReport() throws IOException {
ByteArrayInputStream bis = GeneratePdfReport.ficheReport();
HttpHeaders headers = new HttpHeaders();
//Download
//headers.add("Content-Disposition", "attachment; filename=fiche.pdf");
//View in browser
headers.add("Content-Disposition", "inline; filename=fiche.pdf");
return ResponseEntity
.ok()
.headers(headers)
.contentType(MediaType.APPLICATION_PDF)
.body(new InputStreamResource(bis));
}
}
在某些 Vaadin 视图中
Button generatePDF = new Button();
generatePDF.addClickListener(event->{
UI.getCurrent().navigate("fichedownload");
});
手动 link 到 'http://localhost:8080/fichedownload' 给了我预期的结果。
generatePDF 按钮导航到 'http://localhost:8080/fichedownload' 但给出“无法找到 'fichedownload' 的路径 - 错误。
我是否必须设置路线,或者导航不是 'trigger' @RequestMapping?
UI.getCurrent().navigate(...)
和其他 Router
导航方法只能用于导航到在 Vaadin 注册的路由(例如使用 @Route
)。
对于您的情况,我建议您使用 Anchor
而不是按钮。它在浏览器中对应一个普通的<a>
link,你可以给它任意的URL。另一个好处是不需要服务器往返。
或者您可以尝试使用 Button
,但使用 UI.getCurrent().getPage().setLocation(...)
导航。如果这不起作用,请尝试 UI.getCurrent().getPage().executeJs("window.open('your_url', '_blank')")
.
我正在使用 Springboot 和 Vaadin 创建一个网络应用程序。 我确实编写了一个实现@RequestMapping 方法的控制器。此方法生成一个我应该能够在浏览器中查看或下载的 PDF。
如果我直接转到 link,一切正常。但是,当我使用 Vaadin 的导航时,我确实遇到了一些错误。 (但是当我刷新该页面时,它确实有效)。
下面你可以找到我的代码:
@RestController
public class PDFController {
@RequestMapping(value = "fichedownload", method = RequestMethod.GET,produces = MediaType.APPLICATION_PDF_VALUE)
public static ResponseEntity<InputStreamResource> ficheReport() throws IOException {
ByteArrayInputStream bis = GeneratePdfReport.ficheReport();
HttpHeaders headers = new HttpHeaders();
//Download
//headers.add("Content-Disposition", "attachment; filename=fiche.pdf");
//View in browser
headers.add("Content-Disposition", "inline; filename=fiche.pdf");
return ResponseEntity
.ok()
.headers(headers)
.contentType(MediaType.APPLICATION_PDF)
.body(new InputStreamResource(bis));
}
}
在某些 Vaadin 视图中
Button generatePDF = new Button();
generatePDF.addClickListener(event->{
UI.getCurrent().navigate("fichedownload");
});
手动 link 到 'http://localhost:8080/fichedownload' 给了我预期的结果。
generatePDF 按钮导航到 'http://localhost:8080/fichedownload' 但给出“无法找到 'fichedownload' 的路径 - 错误。 我是否必须设置路线,或者导航不是 'trigger' @RequestMapping?
UI.getCurrent().navigate(...)
和其他 Router
导航方法只能用于导航到在 Vaadin 注册的路由(例如使用 @Route
)。
对于您的情况,我建议您使用 Anchor
而不是按钮。它在浏览器中对应一个普通的<a>
link,你可以给它任意的URL。另一个好处是不需要服务器往返。
或者您可以尝试使用 Button
,但使用 UI.getCurrent().getPage().setLocation(...)
导航。如果这不起作用,请尝试 UI.getCurrent().getPage().executeJs("window.open('your_url', '_blank')")
.