开发者

Spring MVC: Open link in new browser window in handler method

I have a controller that handle clicking on links. In handler method i have to do something (on db) and open clicked url in开发者_如何学Python 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";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜