2016-04-19 1 views
0

Bonjour im avoir un strugle avec JSP et Spring,passage de paramètre avec JSP au contrôleur au printemps

<html> 
<head>Title 
</head> 
    <body> 
     Welcome in my view 
     <h1>Animal's Database</h1> 
     <br> 
     <strong>${message}</strong><br>Give me:<br> 
     <a href="writeCat" >Cat's list</a><br> 
     <a href="writeDog">Dog's list</a><br> 
     <a href="writeSnake">Snake's list</a> 

    </body> 

et c'est mon contrôleur

public String getAnimalList(Model model){ 
    model.addAttribute("animalList", animalDAO.getAnimalList("Cat")); 
    return "list"; 
} 
@RequestMapping("/writeDog") 
public String getAnimalList1(Model model){ 
    model.addAttribute("animalList", animalDAO.getAnimalList("Dog")); 
    return "list"; 
} 
@RequestMapping("/writeSnake") 
public String getAnimalList2(Model model){ 
    model.addAttribute("animalList", animalDAO.getAnimalList("Snake")); 
    return "list"; 
} 

ce que je suis en train de faire est d'obtenir débarrasser de la méthode 3x getAnimalList, mais je ne peux pas comprendre, comment passer une chaîne de JSP au contrôleur, par exemple. "Chat", "Serpent", "Chien".
Je sais que dans le corps du contrôleur je devrais aller avec request.getParameter(XXX). Le problème pour moi est de faire un jsp pour envoyer la valeur de chaîne de l'animal correspondant au contrôleur.

Répondre

1

Vous pouvez passer la chaîne du nom de l'animal dans la variable de chemin et pour cette URL pour accéder au service de repos deviendra basepath/write/animalName,

@RequestMapping("/write/{animal}") 
public String getAnimalList1(Model model, @PathVariable("animal") String animal){ 
    model.addAttribute("animalList", animalDAO.getAnimalList(animal)); 
    return "list"; 
} 

avec la variable de chemin que vous pouvez ajouter le nom de l'animal dans href,

<a href="/write/Cat" >Cat's list</a><br> 
<a href="/write/Dog">Dog's list</a><br> 
<a href="/write/Snake">Snake's list</a> 
+0

c'était clair pour moi, mais comment ça devrait être sur le côté jsp? Cat's list
URL – filemonczyk

+0

? Animale = Cat & other = OtherIfYouWant – crm86

+0

l'URL après Cat's list
va comme cette URL/writeCat (avis qui manque "/"), donc je l'ai changé @RequestMapping, mais quand même se demander pourquoi il réduit la "/"de l'URL href – filemonczyk