各位先進好,
在網路上找了很多資料還是沒辦法確定目前的寫法是否正確,
所以想請各位指點需要改進的地方,謝謝!
以下是DAO的LIST:
public List<MessageValue> getAll() throws Exception {
List<MessageValue> list = new ArrayList<>();
SessionFactory sf = HibernateUtil.getSessionFactory();
try (Session session = sf.openSession();) {
session.beginTransaction();
list=session.createCriteria(MessageValue.class).list();
return list;
} catch (Exception ex) {
Logger.getLogger(MessageDAO.class.getName()).log(Level.SEVERE,
"新增資料失敗 !", ex);
throw new Exception("新增資料失敗!", ex);
}
}
以下是Action.java:
public class MessageAction {
Collection<MessageValue> list = null;
private MessageValue mv = new MessageValue();
// private List<MessageValue> messageValue;
private List<MessageValue> messageList ;
static final Logger logger = LogManager.getLogger(MessageAction.class);
public String add() throws Exception {
logger.debug("SEEEEEEEE");
System.out.println(getId());
logger.info(mv.getId());
logger.debug(mv.getSubject());
logger.error(mv.getMessage());
logger.warn(mv.getCreateowner());
MessageDAO msdao = new MessageDAO();
messageList = msdao.getAll();
msdao.insert(mv);
getAll();
return "success";
}
public String update() throws Exception {
logger.debug("WATCHHHHHHHH");
System.out.println(getId());
logger.info(mv.getId());
logger.debug(mv.getSubject());
logger.error(mv.getMessage());
logger.warn(mv.getCreateowner());
MessageDAO msdao = new MessageDAO();
msdao.update(mv);
getAll();
return "success";
}
public String delete() throws Exception {
System.out.println(getId());
logger.info(mv.getId());
logger.debug(mv.getSubject());
logger.error(mv.getMessage());
logger.warn(mv.getCreateowner());
MessageDAO msdao = new MessageDAO();
msdao.delete(mv);
getAll();
return "success";
}
public List<MessageValue> getMessageList() {
return messageList;
}
public void setMessageList(List<MessageValue> messageList) {
this.messageList = messageList;
}
public Collection<MessageValue> getAll() throws Exception {
MessageDAO dao= new MessageDAO();
list = dao.getAll();
return dao.getAll();
}
// Function that handles the form submit
public String updateMessageValue() {
for (MessageValue mv : messageList) {
System.out.println(mv.getId());
}
return "messageList";
}
下面就是其他getter & setter
這是我的JSP:
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<title>Hello World</title>
</head>
<body>
<%
MessageDAO dao= new MessageDAO();
Collection<MessageValue> list = null;
list = dao.getAll();
%>
<script>
function update(formName) {
//var messageId = document.getElementById("").innerHTML;
var v=document.getElementById(formName);
v.action = "message_update";
v.submit();
}
function clearMsg(formName) {
var v=document.getElementById(formName);
v.action = "message_delete";
v.submit();
}
</script>
<%
if (list != null && list.size() > 0) {
%>
<%
for (MessageValue mv : list) {
%>
<form id="<%=mv.getId()%>">
<table>
<tr>
<td id="messageId<%= mv.getId()%>" name="id<%= mv.getId()%>">ID</td>
<td>
<input name="id" value="<%=mv.getId() == 0 ? "" : mv.getId()%>" /></td>
<tr>
<tr>
<td>CreateOwner</td>
<td><input name="createowner"
value="<%=mv.getCreateowner() == null ? "" : mv.getCreateowner()%>" /></td>
</tr>
<tr>
<td>Subject</td>
<td><input name="subject"
value="<%=mv.getSubject() == null ? "" : mv.getSubject()%>" /></td>
</tr>
<tr>
<td>Message</td>
<td><input name="message"
value="<%=mv.getMessage() == null ? "" : mv.getMessage()%>" /></td>
</tr>
</table>
</form>
<button type="submit" onclick="update('<%= mv.getId()%>')">修改留言</button>
<button type="submit" onclick="clearMsg('<%= mv.getId()%>')">刪除留言</button>
<%
}}
%>
</body>
</html>
我知道JSP上不要寫程式碼比較好,所以我想把JSP上面的<% MessageDAO dao= new MessageDAO(); Collection<MessageValue> list = null; list = dao.getAll(); %>
移到Action.java,但不知道我目前寫在裡面的List是否正確?
以及要怎麼在JSP上面把LIST的資料帶進去給我的欄位資料使用??