我正在撰写 java/ spring 网络应用程序, 每个用户都可以在其中拥有自己的单词列表。 每次用户添加/ 删除一个单词时, 都会有一个 ajax 呼叫, 发送 JSON 来添加/ 删除此单词, 当服务器侧面完成后, 将 JSON 对象中的当前单词列表还给 jQuery 。
对小 JSON 单词列表来说, 添加和删除的速度非常快, 但当单词列表变大时, Ajax 调用速度就会变慢。 添加/ 删除调用将会非常频繁 。
JSON文字列表示例[polish words]:
[{"word":"abrys","wordId":646},{"word":"acpan","wordId":647},
{"word":"akrania","wordId":457},{"word":"akwawita","wordId":648},
{"word":"amalgamat","wordId":399},{"word":"amurski","wordId":465},
{"word":"amurskie","wordId":1030},{"word":"ananke","wordId":649},
{"word":"androlog","wordId":650}]
jj查询 [或多或少]:
$("#add_word_submit").live("click", function(e) {
e.preventDefault();
var word = $( #add_word ).serializeObject();
$.postJSON("word.html", word, function(words) {
showDividedAccountWords(words);
});
});
$(".delete_button").live("click", function(e) {
e.preventDefault();
if(confirm(Main.confirmWordDelete)) {
var wordId = $(this).attr("id").substring("delete_".length);
wordName["word"] = $(this).children("img").attr("alt");
$.ajax({
type: "DELETE",
url: "word/" + wordId + ".html",
success: function(words) {
showDividedAccountWords(words);
}
});
}
});
爪哇/春春:
@RequestMapping(value="/word", method = RequestMethod.POST)
public @ResponseBody List<Word> addNewWord(@RequestBody Word word, Principal principal) {
wordService.addNewWord(word, principal.getName());
return getCurrentWords(principal.getName());
}
@RequestMapping(value="/word/{wordId}", method = RequestMethod.DELETE)
public @ResponseBody List<Word> deleteWord(@PathVariable Long wordId, Principal principal) {
wordService.deleteWord(wordId, principal.getName());
return getCurrentWords(principal.getName());
}
public List<Word> getCurrentWords(String username) {
List<Word> accountWords = new ArrayList<Word>();
accountWords.addAll(wordService.listUserWords(username));
return accountWords;
}
您能否给我一些提示来重构它? 或许每次用户添加/删除单词时,我不应该返回当前单词列表?