大大們好,最近剛學struts2以及hibernate,但遇到一些問題卡了很久解不出來,
所以想在這請教各位!
我在JSP上想要修改訊息,但console卻跳錯誤訊息,訊息如下:
at com.tutorialspoint.struts2.value.MessageDAO.insert(MessageDAO.java:21)
at com.tutorialspoint.struts2.value.MessageAction.add(MessageAction.java:45)
這是我的DAO新增、修改、刪除以及抓取全部資料的getAll的方法:
public void insert(MessageValue mv) throws Exception {
if (mv == null) {
throw new IllegalArgumentException("資料不得為null");
}
SessionFactory sf = HibernateUtil.getSessionFactory();
try (Session session = sf.openSession();) {
session.beginTransaction();
session.save(mv);
session.getTransaction().commit();
} catch (Exception ex) {
Logger.getLogger(MessageDAO.class.getName()).log(Level.SEVERE,
"新增資料失敗 !", ex);
throw new Exception("新增資料失敗!", ex);
}
}
public void update(MessageValue mv) throws Exception {
if (mv == null) {
throw new IllegalArgumentException("資料不得為null");
}
SessionFactory sf = HibernateUtil.getSessionFactory();
try (Session session = sf.openSession();) {
session.beginTransaction();
System.out.println("Creating Person: " + mv.getId());
session.merge(mv);
session.getTransaction().commit();
} catch (Exception ex) {
Logger.getLogger(MessageDAO.class.getName()).log(Level.SEVERE,
"新增資料失敗 !", ex);
throw new Exception("新增資料失敗!", ex);
}
}
public void delete(MessageValue mv) throws Exception {
SessionFactory sf = HibernateUtil.getSessionFactory();
try (Session session = sf.openSession();) {
session.beginTransaction();
MessageValue data = new MessageValue();
data.setId(mv.getId());
session.delete(data);
session.getTransaction().commit();
} catch (Exception ex) {
Logger.getLogger(MessageDAO.class.getName()).log(Level.SEVERE,
"新增資料失敗 !", ex);
throw new Exception("新增資料失敗!", ex);
}
}
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 :
public class MessageAction {
private MessageValue mv = new MessageValue();
Collection<MessageValue> list = null;
MessageDAO msdao = new MessageDAO();
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);
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);
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);
return "success";
}
public List<MessageValue> getMessageList() {
return messageList;
}
public void setMessageList(List<MessageValue> messageList) {
this.messageList = messageList;
}
以下是其他Get & Set
這是我的JSP頁面:
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<title>Hello World</title>
</head>
<body>
<script type="text/javascript">
function update() {
alert(formName);
var v = document.getElementById(formName);
v.action = "message_update";
v.submit();
alert(formName);
}
function clearMsg(formName) {
var v = document.getElementById(formName);
v.action = "message_delete";
v.submit();
}
</script>
<s:iterator value="%{messageList}">
<form id="%{id}">
<!-- id=%{id} action="message" -->
<table>
<s:textfield name="Id" label="ID:" value="%{id}" />
<s:textfield name="Subject" label="Subject:" value="%{subject}" />
<s:textfield name="Message" label="Message:" value="%{message}" />
<s:textfield name="Createowner" label="Createowner:"
value="%{createowner}" />
<s:submit type="button" onclick="update('%{id}')">修改留言</s:submit>
<s:submit type="button" onclick="clearMsg()">刪除留言</s:submit>
</table>
</form>
</s:iterator>
</body>
</html>
最後是我的struts.xml:
請問是我JSP上的submit設定錯誤,還是我DAO方法寫錯?
還是Action裡面的三個方法寫錯了?
怎麼修改都還是一樣顯示
請問我該修改哪個部分? 我完全沒有頭緒了...