我正在尝试学习 Spring MVC,并且想亲 body 验 MVC。我有一个简单的 Web 应用程序,我在其中输入用户的字符串并将数据库中的一些结果显示给用户。所有这一切都发生在单个页面中,无需刷新页面。我们可以在 Controller 中使用RequestParam并访问JSP页面中的元素。 (我在这个项目中使用 Bootstrap) 例如在 home.jsp 中,
<form class="navbar-form navbar-right">
<input type="text" name="myValues" class="form-control" placeholder="product..." >
</form>
并且在 Controller 中,
@RequestMapping(value={"", "/", "/home"}, method = RequestMethod.GET)
public String home(Locale locale, Model model,@RequestParam(value="myValues", required=false) String myValues) {
logger.info("Welcome home! The client locale is {}.", locale);
这将帮助我在 Controller 中获取表单查询字符串。
然后我可以进行必要的处理并在 Controller 中使用 addAttribute 返回列表。 (Retailerdetail是我实现后端数据库的类)
ArrayList <RetailerDetail> rlist = mydata.getData();
model.addAttribute("name",rlist);
return "home";
并在jsp页面中显示。
<c:forEach items="${name}" var="element">
<tr>
<td>${element.name}</td>
</tr>
</foreach>
此时我在不使用 Jquery 或 js 的情况下执行此操作。我见过一些代码,人们在 Spring MVC 中使用 jquery 或 js 来实现 ajax。 我的问题是,这是AJAX吗?我们无需使用 Javascript 或 Jquery 即可获得与 AJAX 类似的功能。使用Spring MVC时为什么使用jquery或js来实现AJAX。您能否举一个具体的例子,我可能必须这样做?我已经阅读了相当多的 MVC 和 AJAX 教程,但对这个概念还没有完全理解。我意识到我在这里缺少一些基本概念。但如果你能解释一下,这将帮助我弄清楚很多。
这是诺斯雷德纳的回答:
"The rough idea in English: You have a web page. Some event (can be a button press or other form event, or just something triggered by a timer) occurs and triggers JavaScript code that asks the server for fresh information (like the latest value of GOOG stock). There's a piece of code on the server that collects the info you passed and sends some info back. That's different from the page-serving job the server usually has. When the server answers, a callback function (that you specified in the JavaScript call to the server) is called with the info from the server. Your JavaScript code uses the info to update something--like a GOOG stock chart."
在我的代码中,无需使用 Javascript 即可实现相同的功能?这意味着我们可以在不使用任何Javascript的情况下实现AJAX功能?在这种情况下,什么时候我们真的必须使用 Javascript 来实现 AJAX?
最佳答案
AJAX = Asynchronous JavaScript and XML.
根据定义,Ajax 使用 Javascript,因此您可以使用 javascript 或任何 javascript 框架 (JQuery) 来进行 ajax 调用
你可以在这里看到更多关于ajax的内容
http://www.w3schools.com/ajax/ajax_intro.asp
您可以像 REST API 一样公开您的 Spring 功能
这里有教程
关于javascript - Spring MVC中如何实现AJAX?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34138373/