2012-08-13 53 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); 
    } 
} 

这将节省一排在你的信息表。

相关问题