![](/img/trans.png)
[英]Can we increment version of MongoDB document version manually in Spring Data MongoDB?
[英]How we can read document information on spring mvc & mongodb
我试图使用ajax调用在Spring MVC的mongodb上获得一个人的记录。
这是index.jsp中的ajax代码:
$(document).on("click","a.edit",function(){
var id=this.id;
$.ajax({
url:"edit.htm",
data:{id:id},
success:function(response){
alert(response);
}
});
});
这是PersonService类的get方法:
public Person getPerson(String id) {
return mongoOperations.findOne( Query.query(Criteria.where("_id").is(id)),
Person.class,
COLLECTION_NAME) ;
}
这是PersonController类中的ajaxEditPerson方法:
@RequestMapping(value = "/edit", method = RequestMethod.GET)
public @ResponseBody String ajaxEditPerson(
@ModelAttribute Person person,
ModelMap model,
@RequestParam(value="id") String id) {
model.addAttribute("personOne", personService.getPerson(id));
return "test";
}
我想使用ETL在index.jsp中获取人员信息:
${personOne.name}
但是它不会将响应发送到index.jsp。
我怎样才能解决这个问题?
谢谢
如乔拉姆所说,您正在将字符串“测试”返回给您的控制者
但是您应该返回personOne对象。
将您的方法更改为关注并确保您的项目在路径上具有Jackson或手动配置json映射
@RequestMapping(value = "/edit", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Person ajaxEditPerson(
@ModelAttribute Person person,
ModelMap model,
@RequestParam(value="id") String id) {
return personService.getPerson(id);
}
您可以使用2种不同的技术。
您可以将其放在servlet-xml
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/jsp directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
然后,将index.jsp
放在/WEB-INF/jsp/index.jsp
文件夹中。
@RequestMapping(value = "/edit", method = RequestMethod.GET)
public String ajaxEditPerson(
@ModelAttribute Person person,
ModelMap model,
@RequestParam(value="id") String id) {
model.addAttribute("personOne", personService.getPerson(id));
return "index";
}
要么
@RequestMapping(value = "/edit", method = RequestMethod.GET)
public String ajaxEditPerson(
@ModelAttribute Person person,
ModelMap model,
@RequestParam(value="id") String id) {
model.addAttribute("personOne", personService.getPerson(id));
return new ModelAndView("index", model);
}
然后,您可以在index.jsp中使用JSTL来访问放置在模型中的对象。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Person Object</title>
</head>
<body>
This is a person Object.<br>
Name: <c:out value="${personOne.name}"/>
</body>
</html>
正如Josef所指出的,另一种方式(类似于REST)是返回对象personOne的json字符串。 然后,该json字符串将在客户端的浏览器中进行解释,并且呈现将通过客户端浏览器上运行的javascript代码进行。 您需要在服务器上使用json序列化程序将Person对象转换为json字符串。 一个建议是使用杰克逊。
@RequestMapping(value =“ / edit”,方法= RequestMethod.GET,产生= MediaType.APPLICATION_JSON_VALUE)public @ResponseBody Person ajaxEditPerson(@RequestParam String id){return personService.getPerson(id); }
然后在您的javascript AJAX调用中,您可以访问json字符串:
$(document).on("click","a.edit",function(){
var id=this.id;
$.ajax({
url:"edit",
data:{id:id},
success:function(response){
// response is an Object: json string of Person
alert(response.name);
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.