使用 Spring 控制器从数据库中列出对象
Posted
技术标签:
【中文标题】使用 Spring 控制器从数据库中列出对象【英文标题】:List objects from database using a Spring controller 【发布时间】:2015-02-04 21:53:12 【问题描述】:我正在使用 Java Spring 框架开发一个网站。这是一个互联网论坛。现在,登录后,我想列出当前存储在我的数据库中的所有线程。我将首先向您介绍我实施的重要部分。这篇文章很长,但不要气馁,这确实是一个简单的初级问题,任何具有基本 Web 编程经验的人都可以立即解决。我向你保证。
Thread 对象由它自己的类 Thread 定义。它是一个标准的实体类,包含一些字段、构造函数和 getter/setter。
@Entity
public class Thread
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
int _id;
private Date last_modified;
private int topics;
private String description;
private String name;
public Thread()
public Thread(int _id, Date last_modified, int topics, String description,
String name)
super();
this._id = _id;
this.last_modified = last_modified;
this.topics = topics;
this.description = description;
this.name = name;
public int getId()
return _id;
public void setId(int _id)
this._id = _id;
public Date getLast_modified()
return last_modified;
public void setLast_modified(Date last_modified)
this.last_modified = last_modified;
public int getTopics()
return topics;
public void setTopics(int topics)
this.topics = topics;
public String getDescription()
return description;
public void setDescription(String description)
this.description = description;
public String getName()
return name;
public void setName(String name)
this.name = name;
现在,进入下一层,我的 DAO 包中有相应的 DAO 接口:
@Transactional(propagation = Propagation.REQUIRED)
public interface ThreadDAO
public void addThread(Thread thread);
public List<Thread> listThread();
public void removeThread (int id);
public void editThread(Thread thread);
及其实现:
@Repository("threadDao")
@Transactional
public class ThreadDAOImpl implements ThreadDAO
@Autowired
SessionFactory sessionFactory;
@Override
public void addThread(Thread thread)
// TODO Auto-generated method stub
sessionFactory.getCurrentSession().save(thread);
@Override
public List<Thread> listThread()
// TODO Auto-generated method stub
return sessionFactory.getCurrentSession().createQuery("from Thread order by _id").list();
@Override
public void removeThread(int id)
// TODO Auto-generated method stub
Thread thread = (Thread) sessionFactory.getCurrentSession().load(
Thread.class, id);
if (null != thread)
sessionFactory.getCurrentSession().delete(thread);
@Override
public void editThread(Thread thread)
// TODO Auto-generated method stub
sessionFactory.getCurrentSession().update(thread);
为了将数据推送到我的控制器,我使用了自动连接服务:
@Transactional(propagation = Propagation.REQUIRED)
public interface ThreadService
public void addThread(Thread thread);
public List<Thread> listThread();
public void removeThread (int id);
public void editThread(Thread thread);
实现:
@Service("threadService")
@Transactional
public class ThreadServiceImpl implements ThreadService
@Autowired
ThreadDAO threadDAO;
@Override
public void addThread(Thread thread)
// TODO Auto-generated method stub
threadDAO.addThread(thread);
@Override
public List<Thread> listThread()
// TODO Auto-generated method stub
return threadDAO.listThread();
@Override
public void removeThread(int id)
// TODO Auto-generated method stub
threadDAO.removeThread(id);
@Override
public void editThread(Thread thread)
// TODO Auto-generated method stub
threadDAO.editThread(thread);
最后,我应该在控制器中执行 Service 方法:
@Controller
public class LoginController
@Autowired
UserService userService;
@Autowired
ThreadService threadService;
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login(
@RequestParam(value = "error", required = false) String error,
@RequestParam(value = "logout", required = false) String logout)
ModelAndView model = new ModelAndView();
if (error != null)
model.addObject("error", "Invalid username and password!");
if (logout != null)
model.addObject("msg", "You've been logged out successfully.");
model.setViewName("login");
return model;
@RequestMapping(value = "/standard", method = RequestMethod.GET)
public ModelAndView standardView()
ModelAndView model = new ModelAndView();
model.setViewName("standard");
return model;
这就是我的问题开始的地方。因为我通常不擅长网络编程,而且请求映射的概念对我来说一直很困惑。 “标准”布局现在包含用户登录的信息,并显示其用户名,另外我为我的线程添加了列表布局:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Access denied</title>
</head>
<body>
<!-- h1><spring:message code="access.denied"/></h1> -->
<h6>Logged in as: $pageContext.request.userPrincipal.name</h6>
<c:url value="/j_spring_security_logout" var="logoutUrl" />
<!-- csrt for log out-->
<form action="$logoutUrl" method="post" id="logoutForm">
<input type="hidden"
name="$_csrf.parameterName"
value="$_csrf.token" />
</form>
<script>
function formSubmit()
document.getElementById("logoutForm").submit();
</script>
<a href="javascript:formSubmit()"> Logout</a>
<h3><spring:message code="label.threadList"/></h3>
<c:if test="$!empty threadList">
<table class="data">
<tr>
<th><spring:message code="label.threadName"/></th>
<th><spring:message code="label.threadDescription"/></th>
<th><spring:message code="label.threadTopicCount"/></th>
<th><spring:message code="label.threadLastModified"/></th>
</tr>
<c:forEach items="$threadList" var="thread">
<tr>
<td>$thread.name </td>
<td>$thread.description </td>
<td>$thread.topics</td>
<td>$thread.last_modified</td>
</tr>
</c:forEach>
</table>
缺少的是控制器中的请求映射。我如何映射它,以便它传递线程条目,逐条记录,并在此布局中显示它们以及登录信息?
【问题讨论】:
【参考方案1】:只需将对象添加到控制器中的ModelAndView
实例:
model.addObject("threadList", threadService.listThread());
它应该可以在 JSP 页面中访问。
另外请不要在 DAO 层上使用事务 - 只能在服务层上使用它。见this post。
【讨论】:
我删除了事务性。确实像你说的那样,但我得到:java.lang.IllegalStateException:Bean 名称“线程”的 BindingResult 和普通目标对象都不能用作请求属性 代码是否与您发布的代码相同,因为在您的代码中thread
对象应该由 forEach
循环创建。是否可以在forEach
循环之外使用thread
变量?以上是关于使用 Spring 控制器从数据库中列出对象的主要内容,如果未能解决你的问题,请参考以下文章
Rails:如何使用 Rails 控制台列出数据库表/对象?
spring thymeleaf - 从 html 表中删除对象并将 id 传递给控制器
从 Spring Boot 控制器返回非 JSON 数据(对象列表)