0

我在一个名为 information.jsp 的 jsp 文件中创建了一个 textarea,当用户单击提交时,我试图让它将来自它的输入存储到数据库中。我制作了 information.hbm.xml 文件,如下所示:

信息.jsp:

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

信息.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 的类,用于描述存储和检索数据库中的信息。我只是不知道如何从提交事件的文本区域中将输入输入到描述中......

从我一直在阅读的内容来看,我认为InformationAction当有人点击提交但又不确定时,我必须做一个实际保存它。我是 Hibernate 的新手,并且在将输入保存到数据库并检索它以在有人重新打开页面时自动加载到 textarea 的过程中出错的地方有点迷失。

我只是不知道如何将输入从 textarea 传递到数据库。任何帮助都会很棒,因为我已经为此工作了很长时间并且无法弄清楚。如果您需要更多信息,请告诉我,谢谢。

4

1 回答 1

0

是的,您将需要InformationActionInformationContoller取决于您要使用的 Web 框架。您的操作或控制器需要具有description映射到文本区域值的属性。如果你使用像 Struts2 或 Spring MVC 这样的 web 框架,这很容易实现。

现在进入休眠部分。您的操作需要具有Session可以读取和写入数据库值的休眠对象。然后,您可以使用从前端获得的构造Information对象,然后在会话中调用方法。descriptionsaveOrUpdate()

代码将是这样的

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);
   }
}

这将在您的信息表中保存一行。

于 2012-08-14T02:46:28.253 回答