-2

I need to pass jslt object (List) to a spring controller using ajax.

The page is JSPX and i get de Object in JS code using EL (expression language)

var diasLaborales = "${diasLaborales}";

$.ajax({ 
                url: './configuracionagendas/addRangoHorario.json',
                type: 'POST',
                data: {diasLaborales: diasLaborales}
....
...
.....

this result of var diasLaborales is a string with this format

[DiaLaboral
  [idDia=1,
   nombreDia=Lunes,
   configuracionAgenda=<null>,
   rangosHorario=<null>,
   id=<null>,
   version=<null>],
  DiaLaboral[idDia=2,
   nombreDia=Martes,
   configuracionAgenda=<null>,
   rangosHorario=<null>,
   id=<null>,version=<null>],
  DiaLaboral[
   idDia=3,
   nombreDia=Miércoles,
   configuracionAgenda=<null>,
   rangosHorario=<null>,
   id=<null>,version=<null>]
]

Then I can pass this generated string to the controller but I need to parse it in a List<DiaLaboral>...But I can not

PD: Does not work without quotes in ${diasLaborales}

var diasLaborales = ${diasLaborales};

Gives an error unexpected token < ...

Thanks


EDIT 2:

the solution was to send a json!

thanks!

user1373316
  • 99
  • 1
  • 9

2 Answers2

1

It looks like your format is as follows, correct?

[DiaLaboral
  [idDia=1,
   nombreDia=Lunes,
   configuracionAgenda=<null>,
   rangosHorario=<null>,
   id=<null>,
   version=<null>],
  DiaLaboral[idDia=2,
   nombreDia=Martes,
   configuracionAgenda=<null>,
   rangosHorario=<null>,
   id=<null>,version=<null>],
  DiaLaboral[
   idDia=3,
   nombreDia=Miércoles,
   configuracionAgenda=<null>,
   rangosHorario=<null>,
   id=<null>,version=<null>]
]

It looks like the example might be missing one or more "]" and or "[".

If this were XML or Json, you'd have the luxury of being able to use a library. Here, you just need to parse it.

Try looking at Java String.split():

How to split a string in Java

paulsm4
  • 114,292
  • 17
  • 138
  • 190
1

So you have this format:

[DiaLaboral
  [idDia=1,
   nombreDia=Lunes,
   configuracionAgenda=<null>,
   rangosHorario=<null>,
   id=<null>,
   version=<null>],
  DiaLaboral[idDia=2,
   nombreDia=Martes,
   configuracionAgenda=<null>,
   rangosHorario=<null>,
   id=<null>,version=<null>],
  DiaLaboral[
   idDia=3,
   nombreDia=Miércoles,
   configuracionAgenda=<null>,
   rangosHorario=<null>,
   id=<null>,version=<null>]
]

You probably want to split the input so that you have separate DiaLaboral sections, e.g.:

  1. Remove the leading "[Dialaboral", and the trailing "]".
  2. Split the remaining string on ",Dialaboral".

This leaves you (in this case) three pieces that look like: [idDia=1, nombreDia=Lunes, configuracionAgenda=<null>, rangosHorario=<null>, id=<null>, version=<null>]

Then, you can parse each piece using a helper function:

public static DiaLaboral stringToDL(String input) {
    // remove leading "[" and trailing "]"
    // split on ","
    // for each resulting string:
        // split on "="
        // use the resulting two strings as key & value to initialize DiaLaboral
}

Note: as some people have mentioned, there are several datatypes which were created to store data as text and easily convert it back into program data / objects. XML and JSON are two of these. If at all possible, I would recommend not re-inventing the wheel; from the perspective of programming norms and maintainability implications, it would be better to use an existing format (which will also be easily automatically parseable!) than to create your own.

MyStackRunnethOver
  • 4,872
  • 2
  • 28
  • 42