我正在使用Java Spring框架开发网站。这是一个互联网论坛。现在,登录后,我想列出当前存储在我的数据库中的所有线程。我将首先向您介绍我实施的重要部分。帖子很长,但不要气馁,这真的是一个简单的基本问题,任何具有网络编程基础经验的人都可以立即解决。我向你保证。
Thread对象由其自己的Thread类定义。它是一个标准的Entity类,包含一些字段,构造函数和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;
}
}
那就是我的问题开始的地方。因为我在网络编程方面总体上很糟糕,而且请求映射的概念对我来说一直很混乱。标准&#39; layout现在包含用户登录的信息,并显示它的用户名,另外我为我的线程添加了列表布局:
<%@ 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>
缺少的是控制器中的请求映射。我如何映射它,以便它传递线程条目,逐个记录,并在登录信息中以此布局显示它们?
答案 0 :(得分:1)
只需将对象添加到控制器中的ModelAndView
实例:
model.addObject("threadList", threadService.listThread());
它应该可以在JSP页面中访问。
另外请不要在DAO层上使用事务 - 只在服务层上使用它。请参阅this post。