So I am working on a JQuery. When I press the submit button, data will be added to database and an alert box will appear and display the id of the new data. Here are some of the code
$(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}
}});
}
}});
and here is the 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;
}
I have another method but it doesn’t work too.. Here are the codes
$.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
});
and the 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;
}
The question is, I can’t display the newId.. How can I pass it from controller to JQuery?
Edit : I found the answer
I found the answer.. So basically, I create another view named temporer.jsp, it’s function is just to help me storing the value
<%@ 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}
then I use this one
$.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;
}
Woow, hold your horses my friend 🙂 Creating a temporary view for it is a bad idea. There are several thing with your code that might get improved.
Generally speaking you are still using the old way of controller orchestration in Spring. Although it is still valid way of doing things, you should really take advantage of Spring annotations.
That way you will be able to write very simple and neat controllers, like such:
In this case there is no need to make additional views to return data that you need.
Have a look at documentation for details.
If you would like to stick to the ‘old way’ of implementing controllers, then please keep in mind that
formBackingObjectshouldn’t persist data in database unless you are absolutely know what you are doing.