I have a controller that handle clicking on links. In handler method i have to do something (on db) and open clicked url in new window (something like _blank attribute in links). I use "redirect:url", but it redirect of course in the same window. Any ideas ?
@RequestMapping(value = "/open.html")
public String open(@RequestParam(value="id") Integer id) {
Link link = linkDAO.get(id);
linkDAO.click(id);
return "redirect:"+link.getAddress();
}
I solved this using JavaScript and AJAX - as @Patrick suggest. Maybe it will be helpful for someone.
<a href="#" onclick="openLink(${link.id},'${link.address}');">Open</a>
openLink
function:
function openLink(id, url) {
jQuery.get('open.html?id='+id, function(data) {
if(data == 'OK') {
window.open(url);
}
}, 'text');
}
Handler method:
@ResponseBody
@RequestMapping(value = "/open.html")
public String open(@RequestParam(value="id") Integer id) {
Link link = linkDAO.get(id);
linkDAO.click(id);
return "OK";
}
only a simple change is required in html.
Add attribute target="_blank" in link
Visit W3Schools
Visit W3Schools