Impossible de remplir modelAttribute au printemps
-
26-12-2019 - |
Question
Je ne parviens pas à obtenir le ModelAttribute pour la deuxième demande.Ma première requête est la méthode initForm(). J'ai préparé l'objet Command et je suis capable d'afficher la commande en jsp.
Grâce à initForm(), je remplis la commande et cette commande que je veux dans editForm lorsque je ferai un appel ajax.
Voici mon formulaire de printemps
<form:form method="POST" action="addstudentdetails.htm" commandName="command">
Ignore what is inside this
Name: Shoaib Age:23 <a href="#" onclick="editstudentdetails(1,0)">edit</a>
</form:form>
Ma requête ajax :
function editStudentDetails(studentId,index){
$.ajax(
{url:"editstudentdetails.htm",
method:"GET",
data:{"action":"edit","id":studentId,"index":index},
success: function(data) {
jQuery("#studentDetailsDiv").html(data)
}
}
)
}
Dans editStudentDetails()
méthode j'ai la méthode appel ajax à emporter editForm()
du contrôleur.
Voici mon contrôleur :
@Controller
public class StudentDetailsController {
@Autowired
private StudentDetailsDAO studentDetailsDAO;
@RequestMapping(value="/studentdetails.htm",method = RequestMethod.GET)
public String initForm(HttpServletRequest request,ModelMap map){
String action=request.getParameter("action");
StudentDetailsCommand command=new StudentDetailsCommand();
System.out.println("in controller"+action);
command.setStudents(studentDetailsDAO.findAll());
map.addAttribute("command", command);
return "studentdetails";
}
@RequestMapping(value="/editstudentdetails.htm",method = RequestMethod.GET)
public String editForm(ModelMap map,HttpServletRequest request){
map.addObject("index", request.getParameter("index"));
StudentDetailsCommand command=(StudentDetailsCommand)map.get("command");
System.out.println(command);
System.out.println(command.getStudents());//NullPointerException here.
map.addObject("command", command);
return "studentdetails";
}
}
J'ai même essayé @ModelAttribute("studentDetailsCommand") mais cela n'a pas fonctionné.
Je suis nouveau sur Spring 3.0 et j'ai suivi toutes les solutions proposées ici mais rien n'a fonctionné. Quelqu'un peut-il m'aider s'il vous plaît ?
La solution
Les attributs du modèle n'existent que pendant le cycle de vie d'un HttpServletRequest
.Pensez à lire ma réponse ici.
Dans ton initForm
méthode, vous procédez comme suit
map.addAttribute("command", command);
cela ajoute un attribut nommé command
aux attributs du modèle.Cet attribut finira par trouver sa place dans le HttpServletRequest
attributs et être disponible pour votre JSP.Ici
<form:form [...] modelAttribute="studentDetailsCommand" commandName="command">
tout d'abord, modelAttribute
et commandName
ont le même objectif, c'est à dire.pour trouver un attribut dans le modèle.Si vous supprimez commandName
vous obtiendrez une exception car il n'y a aucun attribut de modèle nommé studentDetailsCommand
.Ici votre commandName
la valeur de écrase votre modelAttribute
la valeur.
Lorsque le conteneur de servlets a fini de restituer votre JSP, le contenu rendu est envoyé comme corps de la réponse HTTP.À ce stade, la demande a été traitée et le HttpServletRequest
et les attributs du modèle sont récupérés.
Lorsque vous envoyez votre nouvelle requête via AJAX, il n'y a plus d'attribut de modèle nommé studentDetailsCommand
(en fait, il n'y en a jamais eu).
Pensez à utiliser Attributs Flash.
En rapport: