Spring, Hibernate - Sortowanie listy pobranych rekordów

0

Założenie : Użytkownikowi wyświetlana jest lista rekordów w kolejności domyślnej (bodajże w kolejności dodawania). Już podczas oglądania listy ma mieć możliwość wybrania z selecta jak posortowany ma być zbiór, oraz zatwierdzenia formularza, co ma skutkować przeładowaniem strony i wyświetleniem poprawnej kolejności listy.

Kontroler

@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String getList(Model model) {
		
		logger.debug("List with all user was sent from Controller");

		String[] optionsList = {"Without sorting", "Ascending by Name", "Descending by Name"};
		
		List<User> list = userService.getAll();
		model.addAttribute("users", list);
		model.addAttribute("options", optionsList);

		return pref + "list";
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public String getSortedList(@ModelAttribute("sortMethod") Integer sortMethod, Model model) {
		
		logger.debug("Sorted list");
		
		List<User> list = userService.getAll(sortMethod);
		model.addAttribute(list);
		
		return pref + "list";
	}

Zainicjowanie zmiennej sortMethod w kontrolerze

	@ModelAttribute
	public Integer getSortMethod() {
	return 0;
	}

Usługa obsługująca połączenia z bazą

public List<User> getAll() {
	
		logger.debug("Displaying all users");

		
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from User");
		
		return query.list();
	}
	
	public List<User> getAll(Integer sortMethod) {
		
		logger.debug("Displaying all users");

		if(sortMethod == 0 ) {
		
			Session session = sessionFactory.getCurrentSession();
            Query query = session.createQuery("select firstName, lastName, phone from User u order by u.firstName by asc");
			return query.list();
			
		}
		
		else {
			
			Session session = sessionFactory.getCurrentSession();
            Query query = session.createQuery("select firstName, lastName, phone from User u order by u.firstName by desc");
			return query.list();
			
		}
	}

Fragment pliku list.jsp

<p> Choose a sort method </p>

<form:form modelAttribute="sortMethod" method="POST" action="list">

	<form:select path="sortMethod">
	
		<form:option value="0">Ascending by Name</form:option>
		<form:option value="1">Descending by Name</form:option>
	
	</form:select>
	
	<input type="submit" value="Do it!" />

</form:form>

<P>  <a href="add">Add a new user</a> </P>

<table style="border: 1px solid; width: 500px; text-align:center">
 <thead style="background:#fcf">
  <tr>
   <th>First Name</th>
   <th>Last Name</th>
   <th>Phone</th>
   <th colspan="3"></th>
  </tr>
 </thead>
 <tbody>
 <c:forEach items="${users}" var="user">
   <c:url var="editUrl" value="edit?id=${user.id}" />
   <c:url var="deleteUrl" value="delete?id=${user.id}" />
  <tr>
   <td><c:out value="${user.firstName}" /></td>
   <td><c:out value="${user.lastName}" /></td>
   <td><c:out value="${user.phone}" /></td>
   <td><a href="${editUrl}">Edit</a></td>
   <td><a href="${deleteUrl}">Delete</a></td>
  </tr>
 </c:forEach>
 </tbody>
</table>
 
<c:if test="${empty users}">
 There are currently no users in the list. <a href="add">Add a new user</a>
</c:if>

Napotkane problemy

  1. Nie wiem czy mój tok rozumowania jest poprawny, niech mnie ktoś poprawi jeśli błędnie rozumuję.
  2. Błąd widoku dotyczący linii z "<form:select path="sortMethod">"

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'sortMethod' available as request attribute

czego nie rozumiem, bo RequestMapping z POSTEM ma ten atrybut z taką samą nazwą.

0

Podbijam

1

A jak zamiast metody antowanej jako ModelAttribute po prostu wepchniesz taki obiekt do Modelu tam gdzie tworzysz Widok /list z GET?
edit: poza tym dziwnie wygląda to że dla całego formularza (!) ustawiasz modelAttribute a potem jeszcze ustawiasz względem niego (!) binding path. W efekcie twój formularz select binduje się do sortMethod.sortMethod a takie cos nie istnieje.

1 użytkowników online, w tym zalogowanych: 0, gości: 1