1

I'm working on a spring web application using thymeleaf as a view resolver lately, and when developping my controllers, i faced a new situation :

i'm not really sure about it but can the @Pathvariable passed from the view be an object ? (a compound key of a model to be precise ? )

i used ids before, but most of them were simple int ids, i just want to know if passing an object (which is the primary key of my object) is possible , and not simple int ids ?

And Thank you

Aissasa
  • 231
  • 5
  • 18
  • I think you can... only problem is you will have to tell spring how to convert the string value(the path value will be a string right) to the object... spring uses property editor(via initBinder) to do it... so you can register a new property editor which can do the conversion then it should be possible – Arun P Johny Sep 04 '14 at 03:23

1 Answers1

1

You can use Spring PropertyEditor or Spring Converter see Spring Convertor

example

public class CategoryConverter implements Converter<String, Category>{

@Autowired
private CategoryService categoryService;

public Category convert(String id) {
    return categoryService.findById(Long.valueOf(id));
}
}

But you may meet some problem when saving object directly to database.

Tung Vo
  • 2,227
  • 5
  • 27
  • 45
  • i get your point, but the problem is that my model have a conpound primary key, which means, to find the whole object, i need two variables, and not one like the example you put. And i don't quite understand where to use this converter : in the controller ? – Aissasa Sep 04 '14 at 11:18