dimanche 6 juillet 2014

Passing a collection to the Spring MVC controller in Angular JS


Vote count:

0




I am using angularjs on the front end and spring MVC for the back end logic


I am familiar with the below code



@RequestMapping(value = "/positions/open/save", method = RequestMethod.POST)
@ResponseBody
public EntityListingApiResponse<OpenPositionRating> saveOpen(@ModelAttribute OpenPositionRating obj, BindingResult bindingResult){
...//code to save
}


This works fine.


Now I want pass the collection of OpenPositionRating object. I tried it using @RequestParam annotation and @ModelAttribute annotation. In case I use @ModelAttribute annotation, it does call the method but I get empty list in the parameters of saveOpen method



@RequestMapping(value = "/positions/open/save", method = RequestMethod.POST)
@ResponseBody
public EntityListingApiResponse<OpenPositionRating> saveOpen(@ModelAttribute ArrayList<OpenPositionRating> obj, BindingResult bindingResult){
...//code to save
}


This doesn't work


Below is the code to call the above method (.js file):



var openPositionRatings = [
{ restrictedUntil: '2014-07-26', rating: 3, id: 20055, openPosition: null, restricted: false },
{ restrictedUntil: '2014-07-26', rating: 5, id: 20053, openPosition: null, restricted: false },
{ restrictedUntil: '2014-07-26', rating: 6, id: 45652, openPosition: null, restricted: true }
];
$http({
url:'api/products/pep/positions/open/save',
method:'POST',
dataType: 'JSON',
data: $.param(openPositionRatings,true),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(data){
}


here openPositionRating is explicit javascript object created for testing.


Below is my OpenPositionRating entity:



@Entity
@Table(name="OPEN_POSITION_RATING")
public class OpenPositionRating implements Serializable {
@Id
@Column(name="ID")
private Long id;

@OneToOne(cascade = {}, fetch = FetchType.LAZY)
@JoinColumn(name="POSITION_ID", unique = false)
private OpenPosition openPosition;

@Column(name="RATING", updatable=true, insertable=true, nullable=true)
private Integer rating;

@Column(name="RESTRICTED", updatable=true, length=1, nullable=false)
private Boolean restricted;

@Temporal(TemporalType.DATE)
@Column(name="RESTRICTED_UNTIL", updatable=true, nullable=true, length=7)
private Date restrictedUntil;

....//Constructors and setter getters
}


asked 58 secs ago






Aucun commentaire:

Enregistrer un commentaire