jsp頁面如何遍歷數據庫的表

本人爲初學者,如下是本菜鳥在實際運用時的記錄,大神能夠跳過,固然也歡迎指點一二

  • 從JSP頁面開始講起,在此用的是
<s:iterator var="word" value="#wordList>//其中wordList是與Action中context.put("wordList", wordList);裏的list集合相對應的,list裝的是數據庫東西,word只是個變量名
    <s:property value="#word.details"/>//用<s:property取出wordList元素中的details屬性,  即數據庫中的details字段                      
</s:iterator>
  • 如今來到action

private String time;
private String details;


public String getTime() {
  return time;
}java

public void setTime(String time) {
  this.time = time;
}

public String getDetails() {
  return details;
}mysql

public void setDetails(String details) {
  this.details = details;
}sql

 

public String showWordList()throws Exception{
  ActionContext context=ActionContext.getContext();

  List<Word> wordList=WordDao.getWordList();//List集合接收的是從Dao層傳來的數據庫內容
  context.put("wordList", wordList);

  return "word";
}數據庫

  • 而後就是Dao層
public static List<Word> getWordList()
    {
        Session session=HibernateSessionFactory.getSession();
        try {
            
            Criteria criteria=session.createCriteria(Word.class);
            
            List<Word> word=criteria.list();//獲取數據庫裏的表裝到List集合中
            
            session.close();
            return word;//返回list集合
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
  • 接着就是model層
public class Word implements java.io.Serializable{

    private static final long serialVersionUID = 1L;
    
    private Integer id;
    
    private String time;
    
    private String details;
    
    public Word() {
    }
    //重載構造方法
    public Word(String time, String details) {
        this.time = time;
        this.details = details;
    }
...下面省略各個成員的set,get方法
}

如今是hbm.xml,與hibernate.cfg.xml相關文件的配置session

//如下是hbm.xml
<hibernate-mapping>
    <!-- 映射數據庫的word表 -->
    <class name="com.model.Word" table="word" catalog="se">
        <!-- 映射id字段 -->
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <!-- 映射name字段 -->
        <property name="time" type="java.lang.String">
            <column name="time" length="30" not-null="true" />
        </property>
        <!-- 映射pwd字段 -->
        <property name="details" type="java.lang.String">
            <column name="details" length="2000" not-null="true" />
        </property>
    </class>
</hibernate-mapping>
//如下是cfg.xml
<hibernate-configuration>
<session-factory>
    <property name="dialect">
        org.hibernate.dialect.MySQLDialect
    </property>
    <!-- 連接地址 -->
    <property name="connection.url">
        jdbc:mysql://localhost:3306/se?useUnicode=true&amp;characterEncoding=UTF-8
    </property>
    <!-- 數據庫user -->
    <property name="connection.username">root</property>
    <!-- 數據庫user密碼 -->
    <property name="connection.password">root</property>
    <!-- 鏈接driver -->
    <property name="connection.driver_class">
        com.mysql.jdbc.Driver
    </property>
    <property name="myeclipse.connection.profile">
        com.mysql.jdbc.Driver
    </property>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <!-- 映射文件 -->
    <mapping resource="com/model/Word.hbm.xml" />
</session-factory>
</hibernate-configuration>
  • 接着是struts.xml的配置
<action name="wordpage" class="com.action.WordAction">
            <result name="word">/user/word.jsp</result>
</action>
相關文章
相關標籤/搜索