Guest User

Untitled

a guest
May 2nd, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.50 KB | None | 0 0
  1. @RequestMapping(value="/ajax/saveVendor.do", method = RequestMethod.POST)
  2. public @ResponseBody AjaxResponse saveVendor( @Valid UIVendor vendor,
  3. BindingResult result,
  4. Locale currentLocale )
  5.  
  6. var vendor =
  7. {
  8. vendorId: 123,
  9. vendorName: "ABC Company",
  10. emails : [
  11. { emailAddress: "abc123@abc.com", flags: 2 },
  12. { emailAddress: "xyz@abc.com", flags: 3 }
  13. ]
  14. }
  15.  
  16. $.post("ajax/saveVendor.do", $.param(vendor), saveEntityCallback, "json" );
  17.  
  18. Invalid property 'emails[0][emailAddress]' of bean class [beans.UIVendor]: Property referenced in indexed property path 'emails[0][emailAddress]' is neither an array nor a List nor a Map; returned value was [abc123@abc.com]
  19.  
  20. package com.mycompany.beans;
  21.  
  22. import java.util.*;
  23. import org.apache.commons.lang.*;
  24. import com.mycompany.domain.Vendor;
  25. import com.mycompany.domain.VendorAttributes;
  26. import org.apache.commons.logging.*;
  27. import org.codehaus.jackson.annotate.JsonIgnore;
  28.  
  29. public class UIVendor
  30. {
  31. private final Log logger = LogFactory.getLog( this.getClass() );
  32. private Vendor vendor;
  33. private boolean ftpFlag;
  34. private String ftpHost;
  35. private String ftpPath;
  36. private String ftpUser;
  37. private String ftpPassword;
  38. private List<UINotificationEmail> emails = null;
  39.  
  40. public UIVendor() { this( new Vendor() ); }
  41. public UIVendor( Vendor vendor )
  42. {
  43. this.vendor = vendor;
  44. loadVendorAttributes();
  45. }
  46.  
  47. private void loadVendorAttributes()
  48. {
  49. this.ftpFlag = false;
  50. this.ftpHost = this.ftpPassword = this.ftpPath = this.ftpUser = "";
  51. this.emails = null;
  52.  
  53. for ( VendorAttributes a : this.vendor.getVendorAttributes() )
  54. {
  55. String key = a.getVendorFakey();
  56. String value = a.getVendorFaValue();
  57. int flags = a.getFlags();
  58.  
  59. if ( StringUtils.isBlank(key) || StringUtils.isBlank(value) ) continue;
  60.  
  61. if ( key.equals( "ftpFlag" ) )
  62. {
  63. this.ftpFlag = BooleanUtils.toBoolean( value );
  64. }
  65. else if ( key.equals( "ftpHost" ) )
  66. {
  67. this.ftpHost = value;
  68. }
  69. else if ( key.equals("ftpPath") )
  70. {
  71. this.ftpPath = value;
  72. }
  73. else if ( key.equals("ftpUser") )
  74. {
  75. this.ftpUser = value;
  76. }
  77. else if ( key.equals("ftpPassword") )
  78. {
  79. this.ftpPassword = value;
  80. }
  81. else if ( key.equals("email") )
  82. {
  83. UINotificationEmail email = new UINotificationEmail(value, flags);
  84. this.getEmails().add( email );
  85. }
  86. }
  87. }
  88.  
  89. private void saveVendorAttributes()
  90. {
  91. int id = this.vendor.getVendorId();
  92. List<VendorAttributes> attrs = this.vendor.getVendorAttributes();
  93. attrs.clear();
  94.  
  95. if ( this.ftpFlag )
  96. {
  97. VendorAttributes flag = new VendorAttributes();
  98. flag.setVendorId( id );
  99. flag.setStatus( "A" );
  100. flag.setVendorFakey( "ftpFlag" );
  101. flag.setVendorFaValue( BooleanUtils.toStringTrueFalse( this.ftpFlag ) );
  102. attrs.add( flag );
  103.  
  104. if ( StringUtils.isNotBlank( this.ftpHost ) )
  105. {
  106. VendorAttributes host = new VendorAttributes();
  107. host.setVendorId( id );
  108. host.setStatus( "A" );
  109. host.setVendorFakey( "ftpHost" );
  110. host.setVendorFaValue( this.ftpHost );
  111. attrs.add( host );
  112.  
  113. if ( StringUtils.isNotBlank( this.ftpPath ) )
  114. {
  115. VendorAttributes path = new VendorAttributes();
  116. path.setVendorId( id );
  117. path.setStatus( "A" );
  118. path.setVendorFakey( "ftpPath" );
  119. path.setVendorFaValue( this.ftpPath );
  120. attrs.add( path );
  121. }
  122.  
  123. if ( StringUtils.isNotBlank( this.ftpUser ) )
  124. {
  125. VendorAttributes user = new VendorAttributes();
  126. user.setVendorId( id );
  127. user.setStatus( "A" );
  128. user.setVendorFakey( "ftpUser" );
  129. user.setVendorFaValue( this.ftpUser );
  130. attrs.add( user );
  131. }
  132.  
  133. if ( StringUtils.isNotBlank( this.ftpPassword ) )
  134. {
  135. VendorAttributes password = new VendorAttributes();
  136. password.setVendorId( id );
  137. password.setStatus( "A" );
  138. password.setVendorFakey( "ftpPassword" );
  139. password.setVendorFaValue( this.ftpPassword );
  140. attrs.add( password );
  141. }
  142. }
  143. }
  144.  
  145. for ( UINotificationEmail e : this.getEmails() )
  146. {
  147. logger.debug("Adding email " + e );
  148. VendorAttributes email = new VendorAttributes();
  149. email.setStatus( "A" );
  150. email.setVendorFakey( "email" );
  151. email.setVendorFaValue( e.getEmailAddress() );
  152. email.setFlags( e.getFlags() );
  153. email.setVendorId( id );
  154. attrs.add( email );
  155. }
  156. }
  157.  
  158. @JsonIgnore
  159. public Vendor getVendor()
  160. {
  161. saveVendorAttributes();
  162. return this.vendor;
  163. }
  164.  
  165. public int getVendorId()
  166. {
  167. return this.vendor.getVendorId();
  168. }
  169. public void setVendorId( int vendorId )
  170. {
  171. this.vendor.setVendorId( vendorId );
  172. }
  173.  
  174. public String getVendorType()
  175. {
  176. return this.vendor.getVendorType();
  177. }
  178. public void setVendorType( String vendorType )
  179. {
  180. this.vendor.setVendorType( vendorType );
  181. }
  182.  
  183. public String getVendorName()
  184. {
  185. return this.vendor.getVendorName();
  186. }
  187. public void setVendorName( String vendorName )
  188. {
  189. this.vendor.setVendorName( vendorName );
  190. }
  191.  
  192. public String getStatus()
  193. {
  194. return this.vendor.getStatus();
  195. }
  196. public void setStatus( String status )
  197. {
  198. this.vendor.setStatus( status );
  199. }
  200.  
  201. public boolean isFtpFlag()
  202. {
  203. return this.ftpFlag;
  204. }
  205. public void setFtpFlag( boolean ftpFlag )
  206. {
  207. this.ftpFlag = ftpFlag;
  208. }
  209.  
  210. public String getFtpHost()
  211. {
  212. return this.ftpHost;
  213. }
  214. public void setFtpHost( String ftpHost )
  215. {
  216. this.ftpHost = ftpHost;
  217. }
  218.  
  219. public String getFtpPath()
  220. {
  221. return this.ftpPath;
  222. }
  223. public void setFtpPath( String ftpPath )
  224. {
  225. this.ftpPath = ftpPath;
  226. }
  227.  
  228. public String getFtpUser()
  229. {
  230. return this.ftpUser;
  231. }
  232. public void setFtpUser( String ftpUser )
  233. {
  234. this.ftpUser = ftpUser;
  235. }
  236.  
  237. public String getFtpPassword()
  238. {
  239. return this.ftpPassword;
  240. }
  241. public void setFtpPassword( String ftpPassword )
  242. {
  243. this.ftpPassword = ftpPassword;
  244. }
  245.  
  246. public List<UINotificationEmail> getEmails()
  247. {
  248. if ( this.emails == null )
  249. {
  250. this.emails = new ArrayList<UINotificationEmail>();
  251. }
  252. return emails;
  253. }
  254.  
  255. public void setEmails(List<UINotificationEmail> emails)
  256. {
  257. this.emails = emails;
  258. }
  259. }
  260.  
  261. {
  262. "vendorName":"MAIL",
  263. "vendorId":45,
  264. "emails":
  265. [
  266. {
  267. "emailAddress":"dfg",
  268. "success":false,
  269. "failure":false,
  270. "flags":0
  271. }
  272. ],
  273. "vendorType":"DFG",
  274. "ftpFlag":true,
  275. "ftpHost":"kdsfjng",
  276. "ftpPath":"dsfg",
  277. "ftpUser":"sdfg",
  278. "ftpPassword":"sdfg",
  279. "status":"A"
  280. }
  281.  
  282. {
  283. "vendorId":"45",
  284. "vendorName":"MAIL",
  285. "vendorType":"DFG",
  286. "ftpFlag":true,
  287. "ftpHost":"kdsfjng",
  288. "ftpUser":"sdfg",
  289. "ftpPath":"dsfg",
  290. "ftpPassword":"sdfg",
  291. "status":"A",
  292. "emails":
  293. [
  294. {
  295. "success":"false",
  296. "failure":"false",
  297. "emailAddress":"dfg"
  298. },
  299. {
  300. "success":"true",
  301. "failure":"true",
  302. "emailAddress":"pfc@sj.org"
  303. }
  304. ]
  305. }
  306.  
  307. @RequestMapping(value="/ajax/saveVendor.do", method = RequestMethod.POST)
  308. public @ResponseBody AjaxResponse saveVendor( @Valid @RequestBody UIVendor vendor,
  309. BindingResult result,
  310. Locale currentLocale )
  311.  
  312. @RequestMapping(value="/ajax/saveVendor.do", method = RequestMethod.POST)
  313. public @ResponseBody AjaxResponse saveVendor( @Valid UIVendor vendor,
  314. BindingResult result,
  315. Locale currentLocale )
  316.  
  317. $.post( "mycontroller.do", $.param(object), callback, "json" )
  318.  
  319. object[0][field]
  320.  
  321. @RequestMapping(value="/ajax/saveVendor.do", method = RequestMethod.POST)
  322. public @ResponseBody AjaxResponse saveVendor( @RequestBody UIVendor vendor,
  323. Locale currentLocale )
  324.  
  325. $.ajax(
  326. {
  327. url:"ajax/mycontroller.do",
  328. type: "POST",
  329. data: JSON.stringify( objecdt ),
  330. success: callback,
  331. dataType: "json",
  332. contentType: "application/json"
  333. } );
  334.  
  335. BindingResult result = new BeanPropertyBindingResult( object, "MyObject" );
  336. Validator validator = new MyObjectValidator();
  337. validator.validate( object, result );
  338.  
  339. public class Test {
  340.  
  341. private List<Map> field;
  342.  
  343. /**
  344. * @return the field
  345. */
  346. public List<Map> getField() {
  347. return field;
  348. }
  349.  
  350. /**
  351. * @param field the field to set
  352. */
  353. public void setField(List<Map> field) {
  354. this.field = field;
  355. }
  356. }
  357.  
  358. (function($) {
  359. // copy from jquery.js
  360. var r20 = /%20/g,
  361. rbracket = /[]$/;
  362.  
  363. $.extend({
  364. customParam: function( a ) {
  365. var s = [],
  366. add = function( key, value ) {
  367. // If value is a function, invoke it and return its value
  368. value = jQuery.isFunction( value ) ? value() : value;
  369. s[ s.length ] = encodeURIComponent( key ) + "=" + encodeURIComponent( value );
  370. };
  371.  
  372. // If an array was passed in, assume that it is an array of form elements.
  373. if ( jQuery.isArray( a ) || ( a.jquery && !jQuery.isPlainObject( a ) ) ) {
  374. // Serialize the form elements
  375. jQuery.each( a, function() {
  376. add( this.name, this.value );
  377. });
  378.  
  379. } else {
  380. for ( var prefix in a ) {
  381. buildParams( prefix, a[ prefix ], add );
  382. }
  383. }
  384.  
  385. // Return the resulting serialization
  386. return s.join( "&" ).replace( r20, "+" );
  387. }
  388. });
  389.  
  390. /* private method*/
  391. function buildParams( prefix, obj, add ) {
  392. if ( jQuery.isArray( obj ) ) {
  393. // Serialize array item.
  394. jQuery.each( obj, function( i, v ) {
  395. if (rbracket.test( prefix ) ) {
  396. // Treat each array item as a scalar.
  397. add( prefix, v );
  398.  
  399. } else {
  400. buildParams( prefix + "[" + ( typeof v === "object" || jQuery.isArray(v) ? i : "" ) + "]", v, add );
  401. }
  402. });
  403.  
  404. } else if (obj != null && typeof obj === "object" ) {
  405. // Serialize object item.
  406. for ( var name in obj ) {
  407. buildParams( prefix + "." + name, obj[ name ], add );
  408. }
  409.  
  410. } else {
  411. // Serialize scalar item.
  412. add( prefix, obj );
  413. }
  414. };
  415. })(jQuery);
  416.  
  417. buildParams( prefix + "[" + name + "]", obj[ name ], traditional, add );
  418.  
  419. buildParams( prefix + "." + name, obj[ name ], add );
  420.  
  421. vendor['emails[0].emailAddress'] = "abc123@abc.com";
  422. vendor['emails[0].flags'] = 3;
  423. vendor['emails[1].emailAddress'] = "xyz@abc.com";
  424. vendor['emails[1].flags'] = 3;
  425.  
  426. private List emailAddresses = new ArrayList();
  427.  
  428. myNestedJsonObject = {
  429. id : 24,
  430. name : 'Sample',
  431. address : {
  432. number : '323A',
  433. street : 'Avenue'
  434. },
  435. properties : [...]
  436. };
  437.  
  438.  
  439.  
  440. var body = {
  441. json : JSON.stringify(myNestedJsonObject)
  442. };
  443.  
  444. this.$http.post(restUrl, body, { emulateJSON: true }).then(
  445. response => {
  446. }, response => {});
  447.  
  448. public class DataJson {
  449.  
  450. private String json;
  451.  
  452. public String getJson() {
  453. return json;
  454. }
  455.  
  456. public void setJson(String json) {
  457. this.json = json;
  458. }
  459. }
  460.  
  461. @RequestMapping(value = "emit", method = RequestMethod.POST)
  462. public @ResponseBody ResponseEntity<ResponseData> emit(@ModelAttribute DataJson json) {
  463. ObjectMapper mapper = new ObjectMapper();
  464. ComprobanteDto cpeDto = mapper.readValue(json.getJson(), ComprobanteDto.class);
  465.  
  466. ....
  467. }
Add Comment
Please, Sign In to add comment