Java SpringMVC使用json而不是${variable}
基本上,我目前正在进行一个MVC项目,我可以使用以下类将值从服务器传递到客户端Java SpringMVC使用json而不是${variable},java,ajax,spring-mvc,Java,Ajax,Spring Mvc,基本上,我目前正在进行一个MVC项目,我可以使用以下类将值从服务器传递到客户端 package org.assessme.com; import java.text.DateFormat; import java.util.Date; import java.util.Locale; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller
package org.assessme.com;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class UserManagementController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/userManagement", method = RequestMethod.GET)
public Object home(Locale locale, Model model) {
logger.info("User management view controller loaded...");
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "userManagement";
}
}
然后可以使用符号
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello world!
</h1>
<P> The time on the server is ${serverTime}. </P>
</body>
</html>
我希望userManagement返回视图userManagement(就像现在一样),但也返回用户的json响应
有人能给我一些建议吗
谢谢,在中,作者解释了如何使用Spring实现这一点
在您的控制器中,您应该有如下内容来返回JSON
@RequestMapping(value="/availability", method=RequestMethod.GET)
public @ResponseBody AvailabilityStatus getAvailability(@RequestParam String name) {
for (Account a : accounts.values()) {
if (a.getName().equals(name)) {
return AvailabilityStatus.notAvailable(name);
}
}
return AvailabilityStatus.available();
}
在您看来,应该使用JQuery编写请求
$(document).ready(function() {
// check name availability on focus lost
$('#name').blur(function() {
checkAvailability();
});
});
function checkAvailability() {
$.getJSON("account/availability", { name: $('#name').val() }, function(availability) {
if (availability.available) {
fieldValidated("name", { valid : true });
} else {
fieldValidated("name", { valid : false,
message : $('#name').val() + " is not available, try " + availability.suggestions });
}
});
}
编辑:您还应该检查。如果将Jackson jar添加到libs中,Spring将开始将pojo解析为JSON。从我看来,这只返回JSON值,而不是我希望页面路由到的视图。可以同时做这两件事吗?谢谢,这似乎就是我想要的:)谢谢
$(document).ready(function() {
// check name availability on focus lost
$('#name').blur(function() {
checkAvailability();
});
});
function checkAvailability() {
$.getJSON("account/availability", { name: $('#name').val() }, function(availability) {
if (availability.available) {
fieldValidated("name", { valid : true });
} else {
fieldValidated("name", { valid : false,
message : $('#name').val() + " is not available, try " + availability.suggestions });
}
});
}