- 浏览: 387287 次
- 性别:
- 来自: 株洲
文章分类
最新评论
-
自闭丨先生:
melin 写道缺少合并web.xml文件功能。不过使用了se ...
使用Maven WAR插件实现多Web应用间的重迭运用 -
331008019:
Hudson 系列的文章写得够细、够全! 学习了…
如何正确在Hudson中使用Maven构建Job -
selina2011874:
请问Hudson也就是现在的jenkins 能选择对某个版本的 ...
使用Hudson进行持续集成(九) -
lovefly_zero:
wv1124 写道使用Hudson调用shell执行make, ...
Use Hudson之项目构建 -
wv1124:
使用Hudson调用shell执行make,编译失败hudso ...
Use Hudson之项目构建
快速开始-用SimpleForm-Controller提交GET/POST请求
Command-Controller提供一个完整的CommandBean,Spring对它提供校验和绑定支持。这个示例在Command-Controller中返回一个Model-Map的Json字符串,json视图返回信息中包含字段错误、全局错误和绑定。支持CommandBean属性类型的转换。
- 通过formBackingObject方法触发GET请求。
- 通过onSubmitAction方法出发POST请求。
详细信息参见文档
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> <property name="formView"><value>jsonView</value></property> <property name="successView"><value>jsonView</value></property> </bean> <bean name="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/hello.json">simpleJsonPostFormController</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"> <property name="jsonErrors"> <list> <ref bean="statusError" /> <ref bean="modelflagError" /> </list> </property> </bean> <bean name="statusError" class="org.springframework.web.servlet.view.json.error.HttpStatusError"> <property name="errorCode"><value>311</value></property> </bean> <bean name="modelflagError" class="org.springframework.web.servlet.view.json.error.ModelFlagError"> <property name="name"><value>failure</value></property> <property name="value"><value>true</value></property> </bean> </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> </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/> <span id ="error" ></span> <br/> <button id="clearData">clear name</button> <<button id="sfc_postData">send data to SimpleFormController</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 addErrors = function(transport){ var json = transport.responseText.evalJSON(); var error = "Errorhandler Info: </br>" + "failture: " + json.failure +"</br>" + "status : + " + transport.status +"</br>" +"</br>" +"Spring Errorhandling: </br>" + "hasGlobalErrors : " + json.hasGlobalErrors +"</br>" + "</br>" + "hasFieldErrors : " + json.hasFieldErrors +"</br>"; if(json.fielderrors.birthday) error = error + "birthday : " + json.fielderrors.birthday +"</br>"; if(json.fielderrors.placeofbirth) error = error + "placeofbirth : " + json.fielderrors.placeofbirth +"</br>"; $('error').innerHTML = error; }; var myrules = { 'button#clearData' : function(element){ element.onclick = function(){ $('t_placeofbirth').innerHTML = ''; $('t_birthday').innerHTML = ''; $('error').innerHTML = ''; }, 'button#sfc_postData' : function(element){ new Ajax.Request('hello1.json', { method:'get', onSuccess: function(transport){ var json = transport.responseText.evalJSON(); printResult(transport); $('placeofbirth').value = json.command.placeofbirth; $('birthday').value = json.command.birthday; }, onFailure: function(transport){ var json = transport.responseText.evalJSON(); printResult(transport); addErrors(transport); } }); element.onclick = function(){ new Ajax.Request('hello1.json', { method:'post', parameters: $('form').serialize(false), onSuccess: function(transport){ var json = transport.responseText.evalJSON(); printResult(transport); $('t_placeofbirth').innerHTML = json.command.placeofbirth; $('t_birthday').innerHTML = json.command.birthday; $('error').innerHTML = ''; }, onFailure: function(transport){ var json = transport.responseText.evalJSON(); 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; } }
Source
public class SimpleJsonPostFormController extends SimpleFormController { 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 Object formBackingObject(HttpServletRequest request) throws Exception { SpringJsonForm bean = new SpringJsonForm(); bean.setBirthday(new Date()); bean.setPlaceofbirth("Sydney"); return bean; } public void onSubmitAction(Object command, BindException errors) { SpringJsonForm bean = (SpringJsonForm) command; } }
- GET-Response
Status : 200 Result: {"firstname":"Peter","secondname":"Schmitt"}
- POST Response
Response-Status : 311 {"command":{ "birthday":"30-01-2008", "placeofbirth":"Sydney" }, "failure":"true", "hasGlobalErrors":"true", "globalerrors": ["errormessage1","errormessage2"], "hasFieldErrors":"true", "fielderrors":{ "birthday":"Please enter a valide date!" }}
快速开始-使用JsonExceptionResolver抛出异常捕获信息的请求
实现异常处理是通过异常分析处理分析器来完成:JsonExceptionResolver。你仅仅需要注册一个JsonErrorHandlers或者JsonExceptionHandler操作一个返回信息以便抛出异常。
JsonErrorHandlers 在产生了错误的情况下发响应到客户端。详细信息参见Errorhandling
JsonExceptionHandler 转换java.lang.Exception对象并添加到Json字符串。详细信息参见Exceptionhandling
Spring ApplicationContext
<beans> <bean name="viewResolver" class="org.springframework.web.servlet.view.XmlViewResolver" /> <bean name="throwExceptionGetController" class="org.thing.spring.json.controller.ThrowExceptionGetController"/> <bean id="exceptionResolver" class="org.springframework.web.servlet.view.json.exception.JsonExceptionResolver"> <property name="exceptionView"><value>jsonView</value></property> <property name="errorHandler"> <list> <ref bean="statusError" /> <ref bean="modelflagError" /> </list> </property> <property name="exceptionHandler"> <list> <ref bean="stackTraceExceptionHandler" /> <ref bean="exceptionMessageExceptionHandler" /> </list> </property> </bean> <bean name="statusError" class="org.springframework.web.servlet.view.json.error.HttpStatusError"/> <bean name="modelflagError" class="org.springframework.web.servlet.view.json.error.ModelFlagError"/ <bean name="stackTraceExceptionHandler" class="org.springframework.web.servlet.view.json.exception.StackTraceExceptionHandler"/> <bean name="exceptionMessageExceptionHandler" class="org.springframework.web.servlet.view.json.exception.ExceptionMessageExceptionHandler"/> </beans>
Spring view.xml
<beans> <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"> <property name="jsonErrors"> <list> <ref bean="statusError" /> <ref bean="modelflagError" /> </list> </property> </bean> <bean name="statusError" class="org.springframework.web.servlet.view.json.error.HttpStatusError"> <property name="errorCode"><value>311</value></property> </bean> <bean name="modelflagError" class="org.springframework.web.servlet.view.json.error.ModelFlagError"> <property name="name"><value>failure</value></property> <property name="value"><value>true</value></property> </bean> </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> </head> <body> <h1>Spring JSON DEMO</h1> <h2>Exception-Handling by JsonExceptionResolver</h2> <button id="throwException">throw exception</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#throwException' : function(element){ element.onclick = function(){ new Ajax.Request('exception.json', { method:'get', parameters: {throwException: 'true'}, onFailure: function(transport){ var json = transport.responseText.evalJSON(); printResult(transport); } }); } } }; Behaviour.register(myrules);
Controller Source
public class ThrowExceptionGetController implements Controller { /** Logger for this class and subclasses */ protected final Log logger = LogFactory.getLog(getClass()); public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { if(request.getParameter("throwException")!= null) throw new Exception("You throw an exeption !"); Map model = new HashMap(); model.put("exception", "false"); return new ModelAndView("json1", model); } }
Result
- GET-Response
Response-Status : 311 { "failure":"true", "exception.message":"java.lang.Exception: You throw an exeption !", "exception.stacktrace": "java.lang.Exception: You throw an exeption ! \n\tat org.thing.spring.json.controller.[...] \n\tat org.springframework.web.servlet.mvc.[...] [...]" }
发表评论
-
Spring Json View之常见问题
2008-12-11 15:32 3124下载 在这里下载 SVN地址 View ... -
Spring Json View之快速开始(一)
2008-12-11 15:28 8978快速开始-用Controller-Interface提交G ... -
Spring Json View之演示总述
2008-12-11 15:27 1835演示程序 关于 这个演示程序显示了下列用例生成的 ... -
Spring Json View之其他配置
2008-12-11 15:24 2154其它配置 ContentType Enco ... -
Spring Json View之错误处理和异常处理
2008-12-11 15:23 2304错误处理 错误处理是在控制器增加一些公共或字段级别的错误 ... -
Spring Json View之校验
2008-12-11 15:21 1788校验 来自Post的请求校验的非常容易。仅仅需要按Sprin ... -
Spring Json View之绑定
2008-12-11 15:18 4561绑定-数据类型转换 1.用法说明 2 ... -
Spring Json View之概述
2008-12-11 15:17 2066文档中心 Spring json-vie ... -
Spring Json View之引论
2008-12-11 15:13 3600原文地址:http://spring-json.sour ...
相关推荐
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
主要介绍了自定义spring mvc的json视图的实现思路解析,本文给大家介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下
如果是作为客户端的HTTP+JSON接口工程,没有JSP等view视图的情况下,使用Jersery框架开发绝对是第一选择。而在基于Spring3 MVC的架构下,对HTTP+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 Boot 基础》学习源码 在 Spring Boot 中使用 AOP 2018-2018030901:03-ch03-mvc 第三章《MVC 框架》学习源码 URL 映射、文件上传、JSON 解析、JSR303 验证框架、跨域访问、通用错误处理 2018-...
Spring Web model-view-controller (MVC)框架是围绕 DispatcherServlet 设计的,并分发请求到处理程序(handler),Spring MVC支持可配置的处理程序映射(handler mapping),视图解析(view resolution)、 区域设置...
17. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 18. p:prefix="/WEB-INF/view/" p:suffix=".jsp" /> 19. 20. 21. class="org.springframework.web.multipart.commons....
快速启动Spring CLI示例 10.3.从早期版本的Spring Boot升级 11.开发您的第一个Spring Boot应用程序 11.1.创建POM 11.2.添加Classpath依赖项 11.3.编写代码 11.3.1.@RestController和@...
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(交互式...
#JSON Diff View轻松查看JSON对象的差异。 该工具的灵感来自