页面重载未同步数据库更新求助(Spring+Hibernate+JS)
Hey there, let's dig into why your dropdown isn't showing the new Master option even after the database updates successfully. Here are the key issues and actionable fixes:
First, Let's Diagnose the Root Cause
Your window.location.reload(true) might not be working as expected for two main reasons:
- Browser/Server Caching: Even with
truefor forced reload, browsers or your Spring backend might be serving cached page data (like pre-rendered dropdown options). - Asynchronous Request Timing: You're reloading the page immediately after firing the
getJSONcall, but the database update might not have fully completed before the refresh (though you confirmed data is updated, this is still a common pitfall with async code).
Fix 1: Fix the Async Timing First
First, move the reload logic inside the getJSON callback to ensure the database update finishes before refreshing the page. This is a quick win:
function addMaster(obj){ var newName = document.getElementById('yourNewMasterInputId').value; // Make sure you're grabbing the input value correctly! if(newName == null || newName.trim() == ""){ alert("You have to enter a new name to add it."); return; } var jsonURL = '${urlBase}/addMaster/' + newName + '/' + updated + '/' + updateID + '/' + created + '/' + createID + '/' + create + '/' + agentID + '.json?jsoncallback=?'; // Wait for the database call to finish before reloading jQuery.getJSON(jsonURL, function(obj, textStatus){ window.location.reload(true); }).fail(function() { alert("Failed to add the new Master. Please try again."); }); }
Fix 2: Bypass Caching Entirely
If the reload still shows old data, browsers might be caching the page. Force a fresh request by adding a unique cache-busting parameter to the URL:
// Replace the reload line with this window.location.href = window.location.pathname + '?cacheBust=' + new Date().getTime();
This makes the URL unique every time, so the browser can't use cached content.
Fix 3: Local Refresh (Best for User Experience)
Instead of reloading the whole page, update just the dropdown dynamically. This is smoother for users and avoids caching issues entirely:
Step 1: Update your backend
Add a new endpoint to fetch the latest Master list (e.g., /getAllMasters.json) that returns all Masters including the new one.
Step 2: Modify your frontend code
function addMaster(obj){ var newName = document.getElementById('yourNewMasterInputId').value; if(newName == null || newName.trim() == ""){ alert("You have to enter a new name to add it."); return; } var jsonURL = '${urlBase}/addMaster/' + newName + '/' + updated + '/' + updateID + '/' + created + '/' + createID + '/' + create + '/' + agentID + '.json?jsoncallback=?'; jQuery.getJSON(jsonURL, function(response, textStatus){ // Fetch the updated Master list after successful creation jQuery.getJSON('${urlBase}/getAllMasters.json', function(masters){ var dropdown = document.getElementById('yourDropdownId'); // Replace with your dropdown's ID // Clear existing options dropdown.innerHTML = ''; // Add all Masters from the fresh data masters.forEach(function(master){ var option = document.createElement('option'); option.value = master.id; option.textContent = master.name; dropdown.appendChild(option); }); // Re-add the "Create New Master" option var createNewOption = document.createElement('option'); createNewOption.value = 'createNew'; // Match your existing value for this option createNewOption.textContent = 'Create New Master'; dropdown.appendChild(createNewOption); // Optional: Auto-select the newly created Master dropdown.value = response.newMasterId; // Make sure your addMaster endpoint returns the new ID }); }).fail(function() { alert("Failed to add the new Master. Please try again."); }); }
Fix 4: Check Backend Caching
If you're using Hibernate or Spring caching, make sure the Master data isn't stuck in cache:
- If using Hibernate's second-level cache, evict the Master cache after creating a new entry.
- If using Spring's
@Cacheableannotation, add@CacheEvict(value = "masters", allEntries = true)to youraddMasterservice method to clear the cached list.
内容的提问来源于stack exchange,提问作者TimGraupner

