javascript触发url
我有一个thymeleaf应用程序,它应该执行以下操作 当用户单击submit按钮时,将一些数据保留到数据库中。同一个提交按钮必须从另一个拥有自己数据库的服务器获取一些数据;和我的不同。这是我的代码(很明显,我做错了,因为我的问题在这里。) 我还尝试使用javascript,这是我最初的想法,但是javascript和返回“index”的控制器相互干扰 所以,我试着用你上面看到的来做 这是我的javascriptjavascript触发url,javascript,thymeleaf,Javascript,Thymeleaf,我有一个thymeleaf应用程序,它应该执行以下操作 当用户单击submit按钮时,将一些数据保留到数据库中。同一个提交按钮必须从另一个拥有自己数据库的服务器获取一些数据;和我的不同。这是我的代码(很明显,我做错了,因为我的问题在这里。) 我还尝试使用javascript,这是我最初的想法,但是javascript和返回“index”的控制器相互干扰 所以,我试着用你上面看到的来做 这是我的javascript function equipmentSearchFn() { let ke
function equipmentSearchFn() {
let keyword;
let url;
keyword = document.getElementById("keyword").value;
url = "http://auction.mainauctionservices.com/cgi-bin/mmcal.cgi?mainauction/keyword/" + keyword;
window.location = url;
}
理想情况下,javascript将是最快和最好的路由,但问题是当它通过控制器持久化数据时,im遇到问题,http POST试图打开一个包含结果的新页面
我不知道我是否工作过度,但这肯定让我工作过度
谢谢如果我正确理解了您的问题,那么您希望在发布到控制器后将浏览器重定向到外部URL。为此,只需从
searchNotification
方法返回字符串即可:
@PostMapping(value = "/index")
public void searchNotification(...) {
...
String url = "http://auction.mainauctionservices.com/cgi-bin/mmcal.cgi?mainauction/keyword/" + search;
return "redirect:" + url;
}
这很简单。。当我实施你的修正时,我真的笑了。。。这让我想起了设计原则。。保持简单和直接。谢谢你的帮助
<input type="text" name="keyword" id="keyword" placeholder="Search" th:field="*{search}">
public class IndexController {
@GetMapping({"", "index", "home"})
public String index(Model model) {
model.addAttribute("notification", new Notification());
return "index";
}
@PostMapping(value = "/index")
public void searchNotification(@Valid @ModelAttribute("notification") Notification notification,
@RequestParam("search") String search,
@RequestParam("email") String email,
Model model, BindingResult result,
HttpServletRequest request) {
if (result.hasErrors()) {
return "index";
}
notification.setSearch(search);
notification.setEmail(email);
model.addAttribute("notification", notification);
notificationService.save(notification);
String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
SimpleMailMessage simpleMailMessage = mailConstructor.sendSearchNotification(appUrl, request.getLocale(), notification);
javaMailSender.send(simpleMailMessage);
model.addAttribute("notificationSent", "true");
model.addAttribute("appUrl", appUrl);
return appUrl;
}
}
function equipmentSearchFn() {
let keyword;
let url;
keyword = document.getElementById("keyword").value;
url = "http://auction.mainauctionservices.com/cgi-bin/mmcal.cgi?mainauction/keyword/" + keyword;
window.location = url;
}
@PostMapping(value = "/index")
public void searchNotification(...) {
...
String url = "http://auction.mainauctionservices.com/cgi-bin/mmcal.cgi?mainauction/keyword/" + search;
return "redirect:" + url;
}