2012-08-13 40 views
0

我在一個名爲information.jsp的jsp文件中創建了一個textarea,當用戶點擊提交時,它試圖讓它將輸入從它存儲到數據庫中。我做了information.hbm.xml文件如下圖所示:如何使用休眠功能將textarea輸入存儲到數據庫中?

information.jsp:

<textarea id = "desc" rows="5" cols="115" onkeypress="textCounter(this,20);"><c:out value="${informationView.storedDescription}"/></textarea> 

information.hbm.xml

<hibernate-mapping> 
    <class name="Information" table="INFO_USER"> 
     <id name="id" type="long" column="INFO_ID"> 
    <generator class="native"> 
    <param name="sequence">ID_SEQ</param> 
    </generator> 
</id> 

    <property name="description" column="DESC"/> 

</class> 
</hibernate-mapping> 

我然後做了類Information與getter和setter描述來存儲和檢索數據庫中的信息。我只是無法弄清楚如何從提交事件的textarea獲取輸入描述...

從我一直在閱讀我認爲我必須使InformationAction實際上讓它保存,當有人點擊提交但再次不確定。我是Hibernate的新手,在保存輸入到數據庫的過程中出錯的地方有點遺憾,如果有人重新打開頁面,它會自動檢索它以加載到textarea中。

我只是無法弄清楚我將如何將輸入從textarea傳遞到數據庫。 任何幫助將是偉大的,因爲我一直在這個工作了很長時間,無法弄清楚。如果您需要更多信息,請告知我,謝謝。

回答

0

是的,您將需要InformationActionInformationContoller,具體取決於您要使用的Web框架。您的操作或控制器需要具有映射到文本區域值的description屬性。如果你使用Struts2或Spring MVC等Web框架,這很容易實現。

現在進入休眠部分。您的操作需要具有可以讀取和寫入數據庫值的Hibernate Session對象。然後,您可以使用從前端獲得的description構造您的Information對象,然後在會話中調用saveOrUpdate()方法。

的代碼會是這樣的

public class InformationAction { 
    //Maps to text area value. Needs getter and setter 
    private String description; 

    //Inject session from hibernate configuration 
    private Session session; 

    public void someMethod() { 
    Information information = new Information(); 
    information.setDescription(description); 
    session.saveOrUpdate(information); 
    } 
} 

這將節省一排在你的信息表。

相關問題