Я стырил пример с Хабра, чтобы разобраться во взаимодействии БД и Спринг. Исходный пример прекрасно работает. Тогда я решил пробовать произвести свою выборку из другой таблицы. По примеру создал всякие классы, домены, сервисы, добавил пару строк в контроллер, но выборка не происходит. Пусто. Если точнее, то выборка из примера работает, а моя выборка - нет. Что я делаю не так?
Вот домен:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
package net.schastny.contactmanager.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name = "NEWS")
public class News {
@Id
@Column(name = "ID")
@GeneratedValue
private Integer id;
@Column(name = "TITLE")
private String title;
@Column(name = "CONTENT")
private String content;
@Temporal(TemporalType.TIMESTAMP )
@Column(name = "PUBLISH_TIME" ,columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
private java.util.Date publish_time;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public java.util.Date getPublish_time() {
return publish_time;
}
public void setPublish_time(java.util.Date publish_time) {
this.publish_time = publish_time;
}
}
Вот сервис
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
package net.schastny.contactmanager.service;
import java.util.List;
import net.schastny.contactmanager.dao.NewsDAO;
import net.schastny.contactmanager.domain.News;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class NewsServiceImpl implements NewsService {
@Autowired
private NewsDAO newsDAO;
@Transactional
public List<News> listNews() {
return newsDAO.listNews();
}
}
Вот контроллер
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
package net.schastny.contactmanager.web;
import java.util.Map;
import net.schastny.contactmanager.domain.Contact;
import net.schastny.contactmanager.service.ContactService;
import net.schastny.contactmanager.domain.News;
import net.schastny.contactmanager.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class ContactController {
@Autowired
private ContactService contactService;
@Autowired
private NewsService newsService;
@RequestMapping("/index")
public String listContacts(Map<String, Object> map) {
map.put("contact", new Contact());
map.put("contactList", contactService.listContact());
map.put("news", new News());
map.put("newsList", newsService.listNews());
return "contact";
}
@RequestMapping("/")
public String home() {
return "redirect:/index";
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addContact(@ModelAttribute("contact") Contact contact,
BindingResult result) {
contactService.addContact(contact);
return "redirect:/index";
}
@RequestMapping("/delete/{contactId}")
public String deleteContact(@PathVariable("contactId") Integer contactId) {
contactService.removeContact(contactId);
return "redirect:/index";
}
}
Вот jsp
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
<%@ page language="java" contentType="text/html; charset=utf8"
pageEncoding="utf8"%>
<%@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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<title><spring:message code="label.title" /></title>
</head>
<body>
<a href="<c:url value="/logout" />">
<spring:message code="label.logout" />
</a>
<h2><spring:message code="label.title" /></h2>
<form:form method="post" action="add" commandName="contact">
<table>
<tr>
<td><form:label path="firstname">
<spring:message code="label.firstname" />
</form:label></td>
<td><form:input path="firstname" /></td>
</tr>
<tr>
<td><form:label path="lastname">
<spring:message code="label.lastname" />
</form:label></td>
<td><form:input path="lastname" /></td>
</tr>
<tr>
<td><form:label path="email">
<spring:message code="label.email" />
</form:label></td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td><form:label path="telephone">
<spring:message code="label.telephone" />
</form:label></td>
<td><form:input path="telephone" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit"
value="<spring:message code="label.addcontact"/>" /></td>
</tr>
</table>
</form:form>
<h3><spring:message code="label.contacts" /></h3>
<c:if test="${!empty contactList}">
<table class="data">
<tr>
<th><spring:message code="label.firstname" /></th>
<th><spring:message code="label.email" /></th>
<th><spring:message code="label.telephone" /></th>
<th> </th>
</tr>
<c:forEach items="${contactList}" var="contact">
<tr>
<td>${contact.lastname}, ${contact.firstname}</td>
<td>${contact.email}</td>
<td>${contact.telephone}</td>
<td><a href="delete/${contact.id}"><spring:message code="label.delete" /></a></td>
</tr>
</c:forEach>
</table>
</c:if>
<c:if test="${!empty newsList}">
<table class="data">
<c:forEach items="${newsList}" var="news">
<tr>
<td>${news.id}</td>
<td>${news.title}</td>
<td>${news.content}</td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>
Вот архив