gpt4 book ai didi

java - Spring 3,将数据从jsp传递到 Controller

转载 作者:行者123 更新时间:2023-12-01 14:18:27 25 4
gpt4 key购买 nike

我正在使用 Spring 3。在我的 jsp 中我有一个表单

<form action='componentAction.html' method='POST'>
<input type='hidden' id='action_' name='componentAction' value=""/>
</form>

表单操作的值更改为开始、停止或暂停。

我还有 3 个按钮:开始、停止和暂停。按下这些按钮之一后,表单的值就会发生变化。

<input type="button" value="Start" onclick="changeFormValueAndSubmit('start')">
<input type="button" value="Stop" onclick="changeFormValueAndSubmit('stop')">
<input type="button" value="Pause" onclick="changeFormValueAndSubmit('pause')">

我希望能够在按下后通过 ajax 调用将表单的值发送到我的 Controller 。

有人可以告诉我正确的做法吗?另外我应该使用 POST 还是 GET?

我尝试过以下教程,例如
http://www.raistudies.com/spring/spring-mvc/ajax-spring-mvc-3-annonations-jquery/
http://hmkcode.com/spring-mvc-json-json-to-java/
http://java.dzone.com/articles/using-spring-mvc%E2%80%99s
但我却惨遭失败。

感谢任何人提前提供的帮助!

最佳答案

使用 JQuery,您可以在 ChangeFormValueAndSubmit() 函数中使用以下内容

function changeFormValueAndSubmit(buttonName) {
var formData = $('form').serialize();
jQuery.post('/path', formData, function(d) {
// handle response
});

...
}

注意:在此示例中,jquery 将发送表单中的所有数据字段。

让你在 JSP 中包含 jquery 框架,并在 spring 中拥有一个 Controller 方法来处理 POST。

POST 优于 GET。

在服务器端使用 Spring 3 注释,类似于

@Controller
public class ExampleController {

@ResponseBody
@RequestMapping(value = "/path", method=RequestMethod.POST)
public String post(@RequestParam String componentAction) {
// do work
...
return "OK"; // depends on what you need to send back...
}


}

您必须启用 MVC 注释。请参阅http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html#mvc-annotation-driven

关于java - Spring 3,将数据从jsp传递到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17885891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com