iT邦幫忙

0

在JSP上按修改,卻抓到新增方法

  • 分享至 

  • xImage

大大們好,最近剛學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:
http://ithelp.ithome.com.tw/upload/images/20170316/20104799JkW2c2yLD7.png

請問是我JSP上的submit設定錯誤,還是我DAO方法寫錯?
還是Action裡面的三個方法寫錯了?
怎麼修改都還是一樣顯示
http://ithelp.ithome.com.tw/upload/images/20170316/20104799GDaiY0X8Ri.png

請問我該修改哪個部分? 我完全沒有頭緒了.../images/emoticon/emoticon20.gif

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
海綿寶寶
iT邦大神 1 級 ‧ 2017-03-16 12:34:46

你要不要買本書
然後先照著範例做
等到熟悉了一點
再改成自己要的

chousen iT邦新手 5 級 ‧ 2017-03-16 12:44:01 檢舉

好的,已經有物色了幾本還沒決定,謝謝你。

我要發表回答

立即登入回答