快速开始-用Controller-Interface提交Get请求
在Spring中不支持控制器接口的验证或绑定。它却能容易地处理来自Get方式的请求。
这个示例在Controller中仅仅返回一个Model-Map的Json字符串,没有包含错误或者格式转换。
下面显示了创建一个简单的Json GET 控制器的所有代码。
Spring ApplicationContext
<beans>
<bean name="simpleJsonGetController"
class="org.thing.spring.json.controller.SimpleJsonGetController"/>
<bean name="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.json">simpleJsonGetController</prop>
</props>
</property>
</bean>
<bean name="viewResolver"
class="org.springframework.web.servlet.view.XmlViewResolver" />
</beans>
Spring view.xml
<beans>
<bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"/>
</beans>
form.html
<head>
<title>
First Test Spring Json Demo
</title>
<script type="text/javascript" src="script/prototype.js"></script>
<script type="text/javascript" src="script/behaviour.js"></script>
<script type="text/javascript" src="script/behaviour-roles.js"></script>
<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"/>
</head>
<body>
<h1>Spring JSON DEMO</h1>
<h2>Spring Ajax Get (ControlerInterface)</h2>
<b>firstname : </b><span id="firstname"></span><br/>
<b>secondname : </b><span id="secondname"></span><br/>
</br>
<button id="getName">get name</button>
<button id="clearName">clear name</button><br/>
</body>
JavaScript behaviour-roles.js
var printResult = function(transport){
var result =
"Status : " + transport.status
+ "\n"
+ "\n"
+ "Json-Result:"
+ "\n" + transport.responseText;
alert(result);
};
var myrules = {
'button#getName' : function(element){
element.onclick = function(){
new Ajax.Request('hello.json', { method:'get',
onSuccess: function(transport, json){
var json = transport.responseText.evalJSON();
printResult(transport);
$('firstname').innerHTML = json.firstname;
$('secondname').innerHTML = json.secondname;
}
});
}
},
'button#clearName' : function(element){
element.onclick = function(){
$('firstname').innerHTML = '';
$('secondname').innerHTML = '';
}
}
};
Behaviour.register(myrules);
Controller 源码
public class SimpleJsonGetController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Map model = new HashMap();
model.put("firstname", "Peter");
model.put("secondname", "Schmitt");
return new ModelAndView("jsonView", model);
}
}
效果:
Status : 200
Result:
{"firstname":"Peter","secondname":"Schmitt"}
快速开始- 用Command-Controller提交Post请求
Command-Controller提供一个完整的CommandBean,Spring对它提供校验和绑定支持。但是你必须在你的控制器类里添加校验和绑定结果。它处理简单的Post请求非常容易。这个示例在Command-Controller中仅仅返回一个Model-Map的Json字符串,没有包含错误或者格式转换。
Spring ApplicationContext
<beans>
<bean name="simpleJsonPostFormController"
class="org.thing.spring.json.controller.SimpleJsonPostFormController">
<property name="commandClass"> <value>org.thing.spring.json.controller.SpringJsonForm</value>
</property>
</bean>
<bean name="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.json">simpleJsonPostCommandController</prop>
</props>
</property>
</bean>
<bean name="viewResolver"
class="org.springframework.web.servlet.view.XmlViewResolver" />
</beans>
Spring view.xml
<beans>
<bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"/>
</beans>
form.html
<head>
<title>
First Test Spring Json Demo
</title>
<script type="text/javascript" src="script/prototype.js"></script>
<script type="text/javascript" src="script/behaviour.js"></script>
<script type="text/javascript" src="script/behaviour-roles.js"></script>
<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"/>
</head>
<body>
<h1>Spring JSON DEMO</h1>
<h2>Spring Ajax Post (SimpleFormControler and CommandController)</h2>
<form method="post" id="form">
<input id="placeofbirth" type="text" name="placeofbirth" ><br>
<input id="birthday" type="text" name="birthday" ><br/>
<br/>
<b>place of birth : </b><span id="t_placeofbirth"></span><br/>
<b>birthday : </b><span id="t_birthday"></span><br/>
</form>
<br/>
<button id="clearData">clear name</button>
<button id="cc_postData">send data to CommandController</button>
</body>
JavaScript behaviour-roles.js
var printResult = function(transport){
var result =
"Status : " + transport.status
+ "\n"
+ "\n"
+ "Json-Result:"
+ "\n" + transport.responseText;
alert(result);
};
var myrules = {
'button#clearData' : function(element){
element.onclick = function(){
$('t_placeofbirth').innerHTML = '';
$('t_birthday').innerHTML = '';
$('error').innerHTML = '';
},
'button#cc_postData' : function(element){
element.onclick = function(){
new Ajax.Request('hello.json', {
method:'post',
parameters: $('form').serialize(false),
onSuccess: function(transport){
var json = transport.responseText.evalJSON();
printResult(transport);
$('t_placeofbirth').innerHTML = json.placeofbirth;
$('t_birthday').innerHTML = json.birthday;
$('error').innerHTML = '';
},
onFailure: function(transport){
printResult(transport);
addErrors(transport);
}
});
}
}
};
Behaviour.register(myrules);
CommandBean
public class SpringJsonForm {
private String placeofbirth;
private Date birthday;
public String getPlaceofbirth() {
return placeofbirth;
}
public void setPlaceofbirth(String placeofbirth) {
this.placeofbirth = placeofbirth;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
控制器源码
public class SimpleJsonPostCommandController extends AbstractCommandController {
@Override
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception{
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
binder.registerCustomEditor(Date.class, editor);
}
@Override
protected ModelAndView handle(HttpServletRequest request,
HttpServletResponse response, Object command,
BindException exception) throws Exception {
SpringJsonForm bean = (SpringJsonForm) command;
ModelAndView modelAndView = new ModelAndView("jsonView");
modelAndView.addObject("birthday", bean.getBirthday());
modelAndView.addObject("placeofbirth", bean.getPlaceofbirth());
return modelAndView;
}
}
结果
Status : 200
Result:
{"placeofbirth":"Sydney","birthday":"Wed Jan 30 00:00:00 GMT 2008"}
分享到:
相关推荐
ext + spring Json view + springMVC + Freemaker
Spring4.1开始直接支持@JsonView注解。要使用@JsonView,首先我们需要定义视图,我们可以定义多个视图。一个视图定义可以继承其他视图定义。我们的POJO使用@JsonView注解属性传递已经定义的视图。在Spring的...
使spring-mvc支持ajax 包含源代码以及使用教程
NULL 博文链接:https://gongsy.iteye.com/blog/1856240
json-lib-2.4-jdk15.jar commons-lang-2.6.jar commons-beanutils.jar
如果是作为客户端的HTTP+JSON接口工程,没有JSP等view视图的情况下,使用Jersery框架开发绝对是第一选择。而在基于Spring3 MVC的架构下,对HTTP+JSON的返回类型也有很好的支持。但是,在开发工作中,对功能的升级是...
主要介绍了自定义spring mvc的json视图的实现思路解析,本文给大家介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下
Build lightning-fast web applications and REST APIs using Spring MVC and its asynchronous processing capabilities with the view technologies of your choice Explore simplified but powerful data access ...
Spring Json-View向Spring-MVC添加了JavaScript Object Notation(JSON)支持。 它被深度集成到Spring-MVC模块中,并且可以与各种标准控制器一起使用。
基于springboot集成JsonView Demo通俗易懂在demo里面
Open EntityManager in View 29.4. Using H2’s Web Console 29.4.1. Changing the H2 Console’s Path 29.5. Using jOOQ 29.5.1. Code Generation 29.5.2. Using DSLContext 29.5.3. jOOQ SQL Dialect 29.5.4. ...
Spring Web model-view-controller (MVC)框架是围绕 DispatcherServlet 设计的,并分发请求到处理程序(handler),Spring MVC支持可配置的处理程序映射(handler mapping),视图解析(view resolution)、 区域设置...
第一章《Java EE 简介》学习源码 Hello Spring Boot 示例、使用热部署、添加REST支持 2018-2018030802:02-ch02-aop 第二章《Spring Boot 基础》学习源码 在 Spring Boot 中使用 AOP 2018-2018030901:03-ch03-...
快速启动Spring CLI示例 10.3.从早期版本的Spring Boot升级 11.开发您的第一个Spring Boot应用程序 11.1.创建POM 11.2.添加Classpath依赖项 11.3.编写代码 11.3.1.@RestController和@...
弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,...
Spring MVC 3实例,包含上传下载,还有Spring mvc jsr303表单验证技术,还有一个spring mvc ajax json等 欢迎下载 自己研究,简单易懂 如果有注解不懂,可以看看...
SpringJPA分页+杰克逊视图该项目显示了一个使用jackson混入的解决方案,以使spring jpa的Page接口与@JsonView一起@JsonView 。跑步应用程序gradle bootRun 测试gradle test
For machine clients it will produce a JSON response with details of the error, the HTTP status and the exception message. For browser clients there is a ‘whitelabel’ error view that renders the same...
基于Java Spring,Spring Boot,带有MySQL的Hibernate ORM,Spring HATEOAS,Spring Fox(Swagger API文档),JWT和Redis的电子商务REST API。 REST API端点 所有输入和输出均使用JSON格式。 要打开Swagger(交互式...
速度视图,freemarker视图,pdf视图,excel视图,xml视图,jsonview等)。是一个综合性的项目。该项目后期会陆续集成一些好的框架进来一些说Spring Web Flow,Jbpm,WebService,Compass,Solr,nutch等。总之是一个...