J'ai une page contenant une liste d'enregistrements. Je dois avoir un champ de recherche dans ma page de liste où, lorsque l'utilisateur tape un mot clé et clique sur le bouton de recherche, le contenu de la table doit être filtré en fonction du mot clé saisi par l'utilisateur.Rechercher un enregistrement en utilisant jsp-tags
C'est ce que j'ai fait jusqu'ici.
À mon avis, j'ai ceci:
<form:find finderName="BySearchWord" id="ff_ph_com_domain_Accommodation" path="/accommodations">
<field:input disableFormBinding="true" field="searchKeyword" id="f_ph_com_domain_Accommodation" max="255" required="true"/>
</form:find>
J'utilise jsp-tags nommés trouver pour cela.
Dans mon contrôleur, j'ai cette méthode.
@RequestMapping(params = "find=BySearchWord", method = RequestMethod.GET)
public String list(@RequestParam("searchKeyword") String searchKeyword, @RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, @RequestParam(value = "sortFieldName", required = false) String sortFieldName, @RequestParam(value = "sortOrder", required = false) String sortOrder, Model uiModel) {
if (page != null || size != null) {
int sizeNo = size == null ? 10 : size.intValue();
final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
uiModel.addAttribute("accommodations", Accommodation.findAllAccommodationBySearchBox());
} else {
uiModel.addAttribute("accommodations", Accommodation.findAllAccommodationBySearchBox());
}
addDateTimeFormatPatterns(uiModel);
return "accommodations/list";
}
Je suis honnêtement pas sûr s'il est juste de mettre cela ici, mais voici ce que j'ai sur mon domaine jusqu'à présent.
public static List<Accommodation> findAllAccommodationBySearchBox() {
return entityManager().createQuery("SELECT a FROM Accommodation a WHERE a.person.firstName LIKE :searchKeyword OR a.person.middleName LIKE :searchKeyword OR a.person.lastName LIKE :searchKeyword OR a.room.roomNumber LIKE :searchKeyword OR a.person.pvId LIKE :searchKeyword OR a.startDate LIKE :searchKeyword OR a.endDate LIKE :searchKeyword", Accommodation.class).getResultList();
}
public static List<Accommodation> findAllAccommodationBySearchBox(String searchKeyword) {
String jpaQuery = "SELECT a FROM Accommodation a WHERE a.person.firstName LIKE :searchKeyword OR a.person.middleName LIKE :searchKeyword OR a.person.lastName LIKE :searchKeyword OR a.room.roomNumber LIKE :searchKeyword OR a.person.pvId LIKE :searchKeyword OR a.startDate LIKE :searchKeyword OR a.endDate LIKE :searchKeyword";
if (searchKeyword == null || searchKeyword.length() == 0) throw new IllegalArgumentException("The search keyword is required");
searchKeyword = searchKeyword.replace('*', '%');
if (searchKeyword.charAt(0) != '%') {
searchKeyword = "%" + searchKeyword;
}
if (searchKeyword.charAt(searchKeyword.length() - 1) != '%') {
searchKeyword = searchKeyword + "%";
}
return entityManager().createQuery(jpaQuery, Accommodation.class).getResultList();
}
et ceci sur une autre classe:
public List<Accommodation> findAllAccommodationBySearchBox(String searchKeyword) {
if (searchKeyword == null || searchKeyword.length() == 0) throw new IllegalArgumentException("The description argument is required");
searchKeyword = searchKeyword.replace('*', '%');
if (searchKeyword.charAt(0) != '%') {
searchKeyword = "%" + searchKeyword;
}
if (searchKeyword.charAt(searchKeyword.length() - 1) != '%') {
searchKeyword = searchKeyword + "%";
}
EntityManager em = Accommodation.entityManager();
TypedQuery<Accommodation> q = em.createQuery("SELECT a FROM Accommodation a WHERE a.person.firstName LIKE :searchKeyword OR a.person.middleName LIKE :searchKeyword OR a.person.lastName LIKE :searchKeyword OR a.room.roomNumber LIKE :searchKeyword OR a.person.pvId LIKE :searchKeyword OR a.startDate LIKE :searchKeyword OR a.endDate LIKE :searchKeyword", Accommodation.class);
q.setParameter("searchKeyword", searchKeyword);
return q.getResultList();
}
Je suis la suite d'un tutoriel, je l'ai trouvé en ligne, mais la structure qui est un peu différent de ce que j'ai donc je ne pouvais pas tout à fait le point dehors ce que je n'ai pas fait. Je suis vraiment perdu maintenant. J'ai essayé de comprendre cela depuis presque un jour maintenant.
P.S. J'ai seulement pu saisir quelque chose sur le champ de recherche et rediriger la page après avoir cliqué sur le bouton de recherche, mais j'ai seulement reçu un message d'exception disant "Query argument searchKeyword introuvable dans la liste des paramètres fournis lors de l'exécution de la requête. "
J'espère que quelqu'un pourra m'aider.
C'est très compliqué, je l'admets. J'ai modifié ma méthode, puis ajouté un paramètre à findAllAccommodationBySearchBox() mais il renvoie toujours la même exception. Quel semble être le problème? –
Vous n'ajoutez pas de paramètre à la requête dans votre "première" méthode. Aussi, réponse à jour. – qwerty1423
J'ai supprimé ma première méthode et l'exception avait disparu. Au lieu de cela, il redirige juste vers la page de liste mais je ne pense pas qu'il exécute la requête car le contenu de la liste est toujours le même. –