Raistlin
Raistlin

Reputation: 1127

open thymeleaf link in a new browser window

I know that plenty of similar questions can be found here but none of them could help me with my problem.

What the client wants: click on a button, then open a new window with given parameters and show a view with dynamic data loaded from database.

What I have done so far: I have a view with links, not buttons:

<a th:href="@{/report/reportPrintView(al=false, mlVersion=${report.version}, sv=true, id=${report.orderId})}" target="_blank">Protokoll</a>
<a th:href="@{/report/reportPrintView(al=true, mlVersion=${report.version}, sv=false, id=${report.orderId})}" target="_blank">Airline</a>

and a controller which is called by the GET request:

@GetMapping("/report/reportPrintView")
public String showReportPrint(@RequestParam("al") boolean al, @RequestParam("mlVersion") String mlVersion, @RequestParam("sv") boolean sv, @RequestParam("id") String id, Model model) {

    ........ do some magic .......

    return "/report/reportPrintView";
}

The view is displayed in a new browser tab and works as expected, but as said before, the client wants a new window.

To get a solution for the clients wishes I tried something like this:

function openWin(id, mlVersion, al, sv) {
    var url = "/report/reportPrintView.html?al=" + al + "&mlVersion=" + mlVersion + "&sv=" + sv + "&id=" + id;
    ReportPrintPreview = window.open("about:blank", "ReportPrintPreview", "width=666,height=700left=250,top=50,dependent=yes,menubar=no,status=no,resizable=yes,toolbar=no,scrollbars=yes");
    ReportPrintPreview.location.href = url;
    ReportPrintPreview.focus();
    return false;
}
.
.
.
<button th:onclick="'openWin(\'' + ${report.orderId} + '\', \'' + ${report.version} + '\', false, true)'">Protokoll</button>
<button th:onclick="'openWin(\'' + ${report.orderId} + '\', \'' + ${report.version} + '\', true, false)'">Airline</button>

What happens here is that a new window is opened with a 404 error and the web page with the button shows a 400 error. So I assume that the controller doesn't get the GET request and cannot show the view (as is a reasonable result because it's not a Thymeleaf call like @{/report/....}). Is there any way to get this running?

Upvotes: 1

Views: 3257

Answers (2)

Metroids
Metroids

Reputation: 20497

This is how I would structure that.

JavaScript

function openWin(url) {
    ReportPrintPreview = window.open("about:blank", "ReportPrintPreview", "width=666,height=700left=250,top=50,dependent=yes,menubar=no,status=no,resizable=yes,toolbar=no,scrollbars=yes");
    ReportPrintPreview.location.href = url;
    ReportPrintPreview.focus();
    return false;
}

Thymeleaf

<button
  th:data-url="@{/report/reportPrintView(al=false, mlVersion=${report.version}, sv=true, id=${report.orderId})}"
  onclick="openWin(this.getAttribute('data-url'))">Protokoll</button>

<button
  th:data-url="@{/report/reportPrintView(al=true, mlVersion=${report.version}, sv=false, id=${report.orderId})}"
  onclick="openWin(this.getAttribute('data-url'))">Airline</button>

Upvotes: 1

Alien
Alien

Reputation: 15908

For thymeleaf it works in different way. try below.

th:target="_blank"

Upvotes: 0

Related Questions