gpt4 book ai didi

java - 如何从 Controller 捕获特定值到 JQuery?

转载 作者:行者123 更新时间:2023-12-01 15:34:21 25 4
gpt4 key购买 nike

所以我正在研究 JQuery。当我按下提交按钮时,数据将被添加到数据库中,并且会出现一个警告框并显示新数据的 ID。这是一些代码

    $(function(){
$('#submit').click(function()
{
if{
str = "some.htm?type=input&memo="+$("#memo").val()+"&description="+$("#description").val();
$.ajax({

url : str,
async : 'false',
success : function (id)
{ alert("....."); // I want the alert to display the newID after the data are successfully inserted into database}
}});
}
}});

这是 Controller

protected Object formBackingObject(HttpServletRequest request) throws ServletException {
Something something = new Something();
dbHelper db = new dbHelper();


if(request.getParameter("type")!= null)
{
type = request.getParameter("type");
String memo = request.getParameter("memo");
String description = request.getParameter("description");

if(type.equalsIgnoreCase("input") == true)
{
String newId = db.getCode("idSome", "vrs_somedatabase", "PYR"); //will return newest id from database
db.InsertSome(id, memo, description); //insert data into database, ignore it

}
}
return iuom;

}

我有另一种方法,但它也不起作用..这是代码

$.post("some.htm", { memo: $("#memo").val(), description: $("#description").val() },
function(id) {
alert(....); // I want to display the newID after the data are successfully inserted into database
});

和 Controller

public ModelAndView onSubmit(Object command) throws Exception {
inpt = (some) command;

if
{
dbHelper db = new dbHelper();
String newId = db.getCode("idSome", "vrs_somedatabase", "PYR"); //will return newest Id from database
db.InsertSome(id, inpt.getMemo(), inpt.getdescription()); //insert into database, ignore it
}
ModelAndView model = new ModelAndView("someView","some","model");
ModelAndView model = new ModelAndView("someView");
model.addObject("newId",newId);

return model;


}

问题是,我无法显示 newId..如何将它从 Controller 传递到 JQuery?

编辑:我找到了答案

我找到了答案..所以基本上,我创建了另一个名为 temporer.jsp 的 View ,它的功能只是帮助我存储值

<%@ include file="/WEB-INF/jsp/include.jsp" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page language="java" import="java.util.*"%>
<%@ page language="java" import="java.text.*"%>

${newId}

那我就用这个

$.post("some.htm", { memo: $("#memo").val(), description: $("#description").val() },
function(newId) {
alert(newId);
});

public ModelAndView onSubmit(Object command) throws Exception {
inpt = (InputPaymentTerm) command;

......


ModelAndView model = new ModelAndView("temporer");
model.addObject("newId",newId);

return model;

}

最佳答案

哇,别着急,我的 friend :)为其创建临时 View 是一个坏主意。您的代码有几处可能需要改进。

一般来说,您仍在使用 Spring 中旧的 Controller 编排方式。尽管它仍然是有效的做事方式,但您确实应该利用 Spring 注释。

这样你就能够编写非常简单和整洁的 Controller ,如下所示:

@Controller
public class MyController() {

@RequestMapping("/some.htm")
public @ResponseBody Long someActionImpl() {
// some business logic
return id;
}

}

在这种情况下,无需创建额外的 View 来返回所需的数据

看看documentation了解详情。

如果您想坚持使用“旧方法”来实现 Controller ,那么请记住,formBackingObject 不应将数据保留在数据库中,除非您绝对知道自己在做什么。

关于java - 如何从 Controller 捕获特定值到 JQuery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9138201/

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