以下代码是使用spring框架的Web应用程序的一部分。 html代码涉及一个表行,当单击结帐按钮时,将更新数据库中的信息并删除该特定行。我能够做到这一切,但页面刷新。我如何能够使用AJAX或其他方法来阻止它刷新页面并仍然执行任务?
// html代码
<td class="class-checkout-action-button">
enter code here<div>
<form th:action="@{/checkout/main}" th:object="${item}" method="POST">
<button id="input-checkout-confirm" type="submit" name="action-admin" value="action-checkout-confirm" onclick="return checkConfirm(this)">Check Out</button>
<input type="hidden" name="input-inventory-id" th:value="${item.id}"/>
</form>
</div>
</td>
//用于删除行的javascript代码
function checkConfirm(o){
var p=o.parentNode.parentNode.parentNode.parentNode;
if (confirm("Check out?") == true) {
p.parentNode.removeChild(p);
return true;
} else {
return false;
}
}
//控制器
@Controller
public class CheckOutViewController
{
//Logging
static Logger LOGGER = LoggerFactory.getLogger(CheckOutViewController.class.getName());
//Autowire Model
@Autowired CheckOutViewModel checkOutViewModel;
/**
* Controller Mapping for Checkout Main GET
* @param model Model
* @return View Name
* @throws SQLException
*/
@RequestMapping(value= "/checkout/main", method = RequestMethod.GET)
public String controllerCheckoutGET(Model model)
throws SQLException
{
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ": Checkout/Main Controller GET");
//Populate Session Attributes and Get Model
return checkOutViewModel.setAttributeCheckoutMain(model);
}
/**
* Controller Mapping for Checkout Main POST
* @param model Model
* @return View Name
* @throws SQLException
* @throws MessagingException
*/
@RequestMapping(value= "/checkout/main", method = RequestMethod.POST)
public String controllerCheckoutPOST(Model model, RedirectAttributes redirectAttributes,
@RequestParam(value="action-admin", required=true) String action,
@RequestParam(value="input-inventory-id", required=false) Integer id,
@ModelAttribute InventoryItemSearch inventorySearch, BindingResult errors)
throws SQLException, MessagingException
{
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ": Checkout/Main Controller GET");
//Switch on Admin Action
switch(action)
{
//Action: Check Search
case "action_checkout_item_search":
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ":Admin Action for Item Search");
//Get Model
return checkOutViewModel.setAttributeCheckOutSearch(redirectAttributes, inventorySearch);
//Action: Check Out Confirm
case "action-checkout-confirm":
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ":Admin Action for Check Out Confirm");
//Get Model
return checkOutViewModel.setAttributeCheckOutSubmit(redirectAttributes, id);
//Action: Check Out Cancel
case "action-checkout-cancel":
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ":Admin Action for Check Out Cancel");
//Get Model
return checkOutViewModel.setAttributesCheckOutCancel(redirectAttributes, id);
//Default
default:
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ":Admin Action Default");
//Get Model
return checkOutViewModel.setAttributeCheckoutMain(model);
}
}
}
//模型
@Component
public class CheckOutViewModel
{
//Logging
static Logger LOGGER = LoggerFactory.getLogger(CheckOutViewModel.class.getName());
//Autowired Item
@Autowired InventoryItemDAO inventoryItemDAO;
@Autowired InventoryItem inventoryItem;
//Autowired Search
@Autowired private InventoryItemSearch inventorySearch;
//Autowire Email
@Autowired MailUser mailUser;
/**
* Set Attributes for Inventory Main
* @param model Model
* @return View Name
* @throws SQLException
*/
public String setAttributeCheckoutMain(Model model)
throws SQLException
{
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ": Set Attributes for Checkout/Main");
//Check Model for Redirection
if (UtilsModel.isModelEmpty(model) || model == null)
{
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ": Get Inventory Pending Items and Set Login User");
//Set Inventory List and Login User
model.addAttribute(CheckOutViewConsts.INVENTORY_LIST, inventoryItemDAO.getInventoryItemsPending());
model.addAttribute(CheckOutViewConsts.LOGIN_USER, SecurityContextHolder.getContext().getAuthentication().getName());
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ": Reset Search, and Set Search List and Binding Search");
//Reset Search, and Set Search List and Binding Search
inventorySearch = new InventoryItemSearch();
model.addAttribute(InventoryItemSearchConsts.SEARCH_INVENTORY_BINDING, inventorySearch);
model.addAttribute(InventoryItemSearchConsts.SEARCH_INVENTORY, inventorySearch.getSearchList());
model.addAttribute(AdminViewConsts.PAGE_TITLE, "Check Out");
}
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ": Return Model for Checkout/Main");
//Set View Name and Return
return "checkout/main";
}
/**
* Set Attributes Check Out Search
* @param redirectAttributes Redirect Attributes
* @param itemSearch Item Search
* @return View Name
* @throws SQLException
*/
public String setAttributeCheckOutSearch(RedirectAttributes redirectAttributes,
InventoryItemSearch inventorySearch)
throws SQLException
{
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ": Set Attributes for Checkout/Main Search");
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ": Set Pending Inventory List, Login User, Search Binding and Search Attributes");
//Set Inventory List and Login User
redirectAttributes.addFlashAttribute(CheckOutViewConsts.INVENTORY_LIST, inventoryItemDAO.getInventoryItemsPending(inventorySearch));
redirectAttributes.addFlashAttribute(CheckOutViewConsts.LOGIN_USER, SecurityContextHolder.getContext().getAuthentication().getName());
//Set Search List and Binding Search
redirectAttributes.addFlashAttribute(InventoryItemSearchConsts.SEARCH_INVENTORY_BINDING, inventorySearch);
redirectAttributes.addFlashAttribute(InventoryItemSearchConsts.SEARCH_INVENTORY, inventorySearch.getSearchList());
//Set Search Attributes
redirectAttributes.addFlashAttribute(InventoryItemSearchConsts.SELECTED_SEARCH_INVENTORY, inventorySearch.getSearchInventory());
if (!inventorySearch.getSearchField().equals("")) { redirectAttributes.addFlashAttribute(InventoryItemSearchConsts.SEARCH_FIELD, inventorySearch.getSearchField()); }
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ": Redirect to Checkout/Main");
redirectAttributes.addFlashAttribute(AdminViewConsts.PAGE_TITLE, "Check Out");
//Set Redirect and Return
return "redirect:/checkout/main";
}
/**
* Set Attributes for Check Out Submit
* @param redirectAttributes Redirect Attributes
* @param id ID
* @return View Name
* @throws MessagingException
*/
public String setAttributeCheckOutSubmit(RedirectAttributes redirectAttributes,
Integer id) throws MessagingException
{
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ": Set Attributes for Checkout/Main Confirm");
redirectAttributes.addFlashAttribute(AdminViewConsts.PAGE_TITLE, "Check Out");
//Define Results
int checkoutResults = -1;
try
{
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ": Confirm Inventory Item Check Out");
//Get Inventory
inventoryItem = inventoryItemDAO.getInventoryItem(id);
//Confirm Reservation and Check Result
checkoutResults = inventoryItemDAO.checkoutInventoryItem(id, SecurityContextHolder.getContext().getAuthentication().getName());
redirectAttributes.addFlashAttribute(CheckOutViewConsts.MSG_STATUS, CheckOutViewMessage.getCheckoutCancelMessage(checkoutResults, inventoryItem));
redirectAttributes.addFlashAttribute(CheckOutViewConsts.INVENTORY_LIST, inventoryItemDAO.getInventoryItemsPending());
//Check Results
if (checkoutResults == 1)
{
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ": Check Out Successful, Send Email Notification");
//Send Email Notification
mailUser.sendMailCheckOut(inventoryItem, SecurityContextHolder.getContext().getAuthentication().getName());
}
}
catch (SQLException e)
{
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ": SQL Exception: " + e.getMessage() + " . Consuming Exception");
//Report Exception Message
String updateMessage = CheckOutViewMessage.getCheckoutMessage(checkoutResults, inventoryItem) + " " + e.getMessage();
redirectAttributes.addFlashAttribute(CheckOutViewConsts.MSG_STATUS, updateMessage);
}
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ": Set Pending Inventory List, Login User, Search Binding and Search Attributes");
//Set Login User
redirectAttributes.addFlashAttribute(CheckOutViewConsts.LOGIN_USER, SecurityContextHolder.getContext().getAuthentication().getName());
//Set Search List and Binding Search
redirectAttributes.addFlashAttribute(InventoryItemSearchConsts.SEARCH_INVENTORY_BINDING, inventorySearch);
redirectAttributes.addFlashAttribute(InventoryItemSearchConsts.SEARCH_INVENTORY, inventorySearch.getSearchList());
//Set Search Attributes
redirectAttributes.addFlashAttribute(InventoryItemSearchConsts.SELECTED_SEARCH_INVENTORY, inventorySearch.getSearchInventory());
if (!inventorySearch.getSearchField().equals("")) { redirectAttributes.addFlashAttribute(InventoryItemSearchConsts.SEARCH_FIELD, inventorySearch.getSearchField()); }
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ": Redirect to Checkout/Main");
//Set View Name and Return
return "redirect:/checkout/main";
}
/**
* Set Attributes for Check Out Cancel
* @param redirectAttributes Redirect Attributes
* @param id Id
* @return View Name
* @throws MessagingException
*/
public String setAttributesCheckOutCancel(RedirectAttributes redirectAttributes,
Integer id) throws MessagingException
{
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ": Set Attributes for Checkout/Main Cancel");
redirectAttributes.addFlashAttribute(AdminViewConsts.PAGE_TITLE, "Check Out");
//Define Results
int checkoutResults = -1;
try
{
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ": Cancel Inventory Item Check Out");
//Get Inventory
inventoryItem = inventoryItemDAO.getInventoryItem(id);
//Confirm Reservation and Check Result
checkoutResults = inventoryItemDAO.checkoutCancelInventoryItem(id, SecurityContextHolder.getContext().getAuthentication().getName());
redirectAttributes.addFlashAttribute(CheckOutViewConsts.MSG_STATUS, CheckOutViewMessage.getCheckoutCancelMessage(checkoutResults, inventoryItem));
redirectAttributes.addFlashAttribute(CheckOutViewConsts.INVENTORY_LIST, inventoryItemDAO.getInventoryItemsPending());
//Check Results
if (checkoutResults == 1)
{
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ": Check Out Cancel Successful, Send Email Notification");
//Send Email Notification
mailUser.sendMailCheckOutCancel(inventoryItem, SecurityContextHolder.getContext().getAuthentication().getName());
}
}
catch (SQLException e)
{
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ": SQL Exception: " + e.getMessage() + " . Consuming Exception");
//Report Exception Message
String updateMessage = CheckOutViewMessage.getCheckoutCancelMessage(checkoutResults, inventoryItem) + " " + e.getMessage();
redirectAttributes.addFlashAttribute(CheckOutViewConsts.MSG_STATUS, updateMessage);
}
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ": Set Pending Inventory List, Login User, Search Binding and Search Attributes");
//Set Login User
redirectAttributes.addFlashAttribute(CheckOutViewConsts.LOGIN_USER, SecurityContextHolder.getContext().getAuthentication().getName());
//Set Search List and Binding Search
redirectAttributes.addFlashAttribute(InventoryItemSearchConsts.SEARCH_INVENTORY_BINDING, inventorySearch);
redirectAttributes.addFlashAttribute(InventoryItemSearchConsts.SEARCH_INVENTORY, inventorySearch.getSearchList());
//Set Search Attributes
redirectAttributes.addFlashAttribute(InventoryItemSearchConsts.SELECTED_SEARCH_INVENTORY, inventorySearch.getSearchInventory());
if (!inventorySearch.getSearchField().equals("")) { redirectAttributes.addFlashAttribute(InventoryItemSearchConsts.SEARCH_FIELD, inventorySearch.getSearchField()); }
//Logging
LOGGER.info(ConfigConsts.LOGGER_PREFIX + ": Redirect to Checkout/Main");
//Set View Name and Return
return "redirect:/checkout/main";
}
}