0

Пытаюсь отобразить запрос, на выборку информации в виде таблицы о пользователе, который зашел в систему (профиль пользователя), если использую цикл <c:forEach items="${userInfo}" var="user">, то выводится только заголовок таблицы, если без forEach, то выводится таблица с одной строкой, но пустая. Сам запрос выполняется:

Hibernate: select user0_.id as id0_, user0_.address as address0_, user0_.birthdate as birthdate0_, user0_.category_id as category14_0_, user0_.comment as comment0_, user0_.confirmPassword as confirmP5_0_, user0_.email as email0_, user0_.enabled as enabled0_, user0_.gender as gender0_, user0_.login as login0_, user0_.name as name0_, user0_.password as password0_, user0_.surname as surname0_, user0_.tel as tel0_ from user user0_ where user0_.login=?

UserDaoImpl:

@Repository
public class UserDaoImpl implements UserDao {
    @Autowired
    private SessionFactory sessionFactory;
...
 @SuppressWarnings("unchecked")
    public User getLogin(String login){
        Query q = sessionFactory.getCurrentSession().createQuery(
                "FROM User where login=:login"); 
        q.setString("login", login);
         return (User) q.uniqueResult();
     }
...
}

UserServiceImpl:

@Service
public class UserServiceImpl implements UserService{
 @Autowired
    private UserDao userDao;
...
   @Transactional
    public User getLogin(String login){
        return userDao.getLogin(login);
    }
...
}

UserController:

@Controller
    public class UserController {
    ...
    @Autowired
        private UserService userService;
    @RequestMapping(value="/user/find", method = RequestMethod.GET)
        public String findUsers(@ModelAttribute("findUser") String userLogin, Map<String, Object> map) {
            map.put("userInfo", userService.getLogin(userLogin));
            map.put("user", new User());
            return "profile";
    }

signup.jsp:

  <%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
    <%@ taglib prefix="calendar" uri="http://java.sun.com/jsp/jstl/fmt" %>

        <html>
        <t:template>
            <body>
                     <form:form method="post" action="/user/find" commandName="findUser">
                <table class="user-table">
                    <tr>
                        <th colspan="9"><span class="users-label">
                    <spring:message code="label.users"/></span></th>
                    </tr>
                    <tr>
                        <th><spring:message code="label.name"/></th>
                        <th><spring:message code="label.surname"/></th>
                        <th><spring:message code="label.birthdate"/></th>
                        <th><spring:message code="label.address"/></th>
                        <th><spring:message code="label.tel"/></th>
                        <th><spring:message code="label.email"/></th>
                        <th><spring:message code="label.login"/></th>
                        <th><spring:message code="label.category"/></th>
                       </tr>
                    <c:forEach items="${userInfo}" var="user">
                        <tr>
                            <td>${user.name}</td>
                            <td>${user.surname}</td>
                            <td><calendar:formatDate
                            value="${user.birthdate}" pattern="dd-MM-yyyy"/></td>
                            <td>${user.address}</td>
                            <td>${user.tel}</td>
                            <td>${user.email}</td>
                            <td>${user.login}</td>
                            <td>${user.category.name}</td>
                            <td><a href="${pageContext.request.contextPath}/user/edit/${user.id}">
                            <spring:message code="label.edit"/></a></td>

                        </tr>
                    </c:forEach>
                </table>
                    </form:form>
            </body>
        </t:template>
        </html>

Сейчас делаю так:

UserDaoImpl:

@SuppressWarnings("unchecked")
    public User getLogin(String login){
        return (User) sessionFactory.getCurrentSession().createQuery(
        "FROM User where login=:login").setParameter("login", login).uniqueResult();
    }

UserServiceImpl:

@Transactional
    public User getLogin(String login){
        return userDao.getLogin(login);
    }

UserController:

 @RequestMapping(value = "/user/profile", method = RequestMethod.GET)
    public String profile(ModelMap model) {
        model.put("user", new User());
        return "profile";
    }

 @RequestMapping(value = "/user/profile", method = RequestMethod.POST)
    public String userProfile(@ModelAttribute("findUser") String userLogin, Model model) {
        model.addAttribute("user", (userService.getLogin(userLogin)));
        return "view-profile";
    }

menu.jsp (ссылка на профиль):

...
<td><span class="profile"><a href="/user/profile">
         <spring:message code="label.profile"/></a>
                </span></td>
...

profile.jsp:

<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
<%@ taglib prefix="calendar" uri="http://java.sun.com/jsp/jstl/fmt" %>

<html>
<t:template>
    <body>

        <table class="user-table">
                <tr>
                <th><spring:message code="label.name"/></th>
                <th><spring:message code="label.surname"/></th>
                <th><spring:message code="label.birthdate"/></th>
                <th><spring:message code="label.address"/></th>
                <th><spring:message code="label.tel"/></th>
                <th><spring:message code="label.email"/></th>
                <th><spring:message code="label.login"/></th>
                <th><spring:message code="label.category"/></th>
                <%--<th><spring:message code="label.password"/></th>--%>
                <th>&nbsp;</th>
            </tr>

                <tr>
                    <td>${user.name}</td>
                    <td>${user.surname}</td>
                    <td><calendar:formatDate
                    value="${user.birthdate}" pattern="dd-MM-yyyy"/></td>
                    <td>${user.address}</td>
                    <td>${user.tel}</td>
                    <td>${user.email}</td>
                    <td>${user.login}</td>
                    <%--<td>${user.password}</td>--%>
                    <td>${user.category.name}</td>

                    <td><a href="${pageContext.request.contextPath}/user/edit/${user.id}">
                    <spring:message code="label.edit"/></a></td>

                </tr>

        </table>

    </body>

</t:template>
</html>

view-profile.jsp:

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
<html>
<head>
    <title>View Profile</title>
</head>

<body>

<form:form method="post"
           action="${pageContext.request.contextPath}/user/profile"
           commandName="findUser">
    <form:hidden path="id" />
    <jsp:include flush="true" page="profile.jsp" />
</form:form>

</body>

</html>

Но выходит все та же пустая таблица.

2
  • А в логах сервака что пишется?
    – maxus
    4 сен 2014 в 9:41
  • Там нет никаких ошибок.
    – Loff
    4 сен 2014 в 17:46

1 ответ 1

1
  1. Что-то я не понял, зачем здесь forEach? userService.getLogin - возвращает конкретного пользователя, а не список.

  2. map.put("user", new User()); - зачем эта строчка? То, что вы записали в map, то и будет отображаться в view jsp.

В общем, скорее всего, решение такое:

        map.put("user", userService.getLogin(userLogin));
        return "profile";

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.