java 从 Spring MVC 中的 JSP 按钮调用控制器方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34896877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call controller method from JSP button in Spring MVC
提问by jarosik
I would like to call a controller method using a button on a JSP page in Spring MVC, but I would like it to stay on a current page, don't reload it or anything, simply call a method. I found it difficult. My button is on cars.jsp
page. In order to stay on this page I have to do something like this:
我想使用 Spring MVC 中 JSP 页面上的按钮调用控制器方法,但我希望它停留在当前页面上,不要重新加载它或任何东西,只需调用一个方法。我觉得很难。我的按钮在cars.jsp
页面上。为了留在这个页面上,我必须做这样的事情:
@RequestMapping(value="/start")
public String startCheckingStatus(Model model){
System.out.println("start");
model.addAttribute("cars", this.carService.getCars());
return "car\cars";
}
button:
按钮:
<a href="<spring:url value="/cars/start"/>">Start</a>
But this is not a good solution because my page is actually reloaded. Can I just call controller
method without any refreshing, redirecting or anything? When I remove return type like so:
但这不是一个好的解决方案,因为我的页面实际上是重新加载的。我可以只调用controller
方法而不进行任何刷新、重定向或任何操作吗?当我像这样删除返回类型时:
@RequestMapping(value="/start")
public void startCheckingStatus(Model model){
System.out.println("start");
}
I got 404.
我得到了 404。
回答by Zeeshan
Add an onclick event on your button and call the following code from your javascript:
在您的按钮上添加一个 onclick 事件并从您的 javascript 中调用以下代码:
$("#yourButtonId").click(function(){
$.ajax({
url : 'start',
method : 'GET',
async : false,
complete : function(data) {
console.log(data.responseText);
}
});
});
If you want to wait for the result of the call then keep async : false
otherwise remove it.
如果您想等待调用的结果,则保留async : false
否则将其删除。
回答by Alan Hay
As mentioned elsewhere you can achieve this by implementing an Ajax based solution:
正如其他地方提到的,您可以通过实现基于 Ajax 的解决方案来实现这一点:
https://en.wikipedia.org/wiki/Ajax_(programming)
https://en.wikipedia.org/wiki/Ajax_(编程)
With Ajax, web applications can send data to and retrieve from a server asynchronously (in the background) without interfering with the display and behavior of the existing page.By decoupling the data interchange layer from the presentation layer, Ajax allows for web pages, and by extension web applications, to change content dynamically without the need to reload the entire page.
使用 Ajax,Web 应用程序可以异步(在后台)向服务器发送数据和从服务器检索数据,而不会干扰现有页面的显示和行为。通过将数据交换层与表示层分离,Ajax 允许 Web 页面以及扩展的 Web 应用程序动态更改内容,而无需重新加载整个页面。
To achieve this you will need to make changes to both the client and server side parts of your app. When using Spring MVC it is simply a case of adding the @ResponseBody
annotation to your controller method which:
为此,您需要对应用程序的客户端和服务器端部分进行更改。使用 Spring MVC 时,只需将@ResponseBody
注释添加到控制器方法中即可:
can be put on a method and indicates that the return type should be written straight to the HTTP response body (and not placed in a Model, or interpreted as a view name).
可以放在方法上并指示返回类型应直接写入 HTTP 响应正文(而不是放置在模型中,或解释为视图名称)。
Thus, for example, to return a simple String in the Ajax response we can do the following (without the @ResponseBody the framework would try and find a view named 'some status' which is obviously not what we want):
因此,例如,要在 Ajax 响应中返回一个简单的字符串,我们可以执行以下操作(如果没有 @ResponseBody,框架将尝试查找名为“some status”的视图,这显然不是我们想要的):
@RequestMapping(value="/start")
@ResponseBody
public String startCheckingStatus(Model model){
return "some status";
}
For the client part you need to add some javascript which will use the XMLHttpRequest Object to retrieve data from your controller.
对于客户端部分,您需要添加一些 javascript,它将使用 XMLHttpRequest 对象从您的控制器检索数据。
While there are various frameworks which can simplify this (e.g. JQuery) there are some examples at the below using vanilla javascript and it might be worth looking at some of these first to see what is actually going on:
虽然有各种框架可以简化这一点(例如 JQuery),但下面有一些使用 vanilla javascript 的示例,可能值得先查看其中一些以了解实际发生的情况:
http://www.w3schools.com/ajax/ajax_examples.asp
http://www.w3schools.com/ajax/ajax_examples.asp
If we take this specific example:
如果我们以这个具体例子为例:
http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_callback
http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_callback
and [1] copy the <button/>
and <script/>
elements to your JSP, [2] change the URL to point to your controller and, [3] create an element <div id="demo"></div>
in your JSP then, on clicking the button in your page, this <div/>
should be updated to display the String returned by your controller.
[1] 将<button/>
和<script/>
元素复制到您的 JSP,[2] 更改 URL 以指向您的控制器,并 [3]<div id="demo"></div>
在您的 JSP 中创建一个元素,然后单击页面中的按钮,这<div/>
应该更新以显示控制器返回的字符串。
As noted this is a lot of code for one action so you can use some JS framework to abstract a lot of it away.
如前所述,这是一个动作的大量代码,因此您可以使用一些 JS 框架将其中的很多代码抽象出来。