题目是这样的 有两张表 一张为新闻类别表 有2个字段:
nid(pk) sort
有一张新闻内容表
有三个字段
cid(pk) nid(fk) title content
要求通过下拉列表框的方法选择新闻类别然后显示该类别的新闻标题(在当前页中显示) 我是用Struts2+Hibernate3.2+JPA实现的. 数据库 脚本: create database if not exists news; drop table if exists newssort; create table newssort ( nid int primary key AUTO_INCREMENT, sort varchar(50) );
drop table if exists news; create table news ( cid int primary key AUTO_INCREMENT, title varchar(50) not null, content varchar(500) not null, nid int null );
insert into newssort values(null,’娱乐’); insert into newssort values(null,’时事’);
insert into news values(null,’好事’,'好事连连哈哈’,1); insert into news values(null,’坏事’,'坏事不断’,1); insert into news values(null,’爱情是什么’,'爱情是什么啊,还没知道呢’,2); insert into news values(null,’什么啊’,'测试内容’,2);
select * from news; select * from newssort; 两个VO类: News.java: package com.vo;
import java.io.Serializable;
import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table;
@SuppressWarnings(“serial”) @Entity @Table(name=”news”) public class News implements Serializable { private Integer cid; private String title; private String content; @Id @GeneratedValue(strategy=GenerationType.AUTO) public Integer getCid() { return cid; }
public void setCid(Integer cid) { this.cid = cid; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public String getContent() { return content; }
public void setContent(String content) { this.content = content; } } Newssort.java: package com.vo;
import java.io.Serializable; import java.util.ArrayList; import java.util.List;
import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.Table;
import org.hibernate.annotations.LazyCollection; import org.hibernate.annotations.LazyCollectionOption;
@SuppressWarnings(“serial”) @Entity @Table(name = “newssort”) public class Newssort implements Serializable { private Integer nid; private String sort; private List news = new ArrayList(); @OneToMany @JoinColumn(name=”nid”) @LazyCollection(LazyCollectionOption.FALSE) public List getNews() { return news; }
public void setNews(List news) { this.news = news; }
@Id @GeneratedValue(strategy = GenerationType.AUTO) public Integer getNid() { return nid; }
public void setNid(Integer nid) { this.nid = nid; }
public String getSort() { return sort; }
public void setSort(String sort) { this.sort = sort; } }
写个测试类先测试一个持久层操作: package com.test;
import java.util.Iterator; import org.hibernate.Session; import org.hibernate.cfg.AnnotationConfiguration; import org.junit.After; import org.junit.Before;
import com.vo.News; import com.vo.Newssort; public class Test { private Session session ; @Before public void setUp() { session = new AnnotationConfiguration().configure().buildSessionFactory().openSession(); } @After public void tearDown() { session.close(); }
@SuppressWarnings(“unchecked”) @org.junit.Test public void testFind() { @SuppressWarnings(“unused”) //List newssort = session.createCriteria(Newssort.class).list(); Newssort newssort = (Newssort) session.load(Newssort.class, 2); for(Iterator i = newssort.getNews().iterator();i .hasNext();) { String title = i.next().getTitle(); System.out.println(title); } } } 好了写Action NewsAction: package com.web.action;
import java.util.List; import java.util.Map;
import org.hibernate.Session; import org.hibernate.cfg.AnnotationConfiguration;
import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.vo.News; import com.vo.Newssort;
@SuppressWarnings( { “serial”, “unchecked” }) public class NewsAction extends ActionSupport { private Session session; private Integer sortid;
public Integer getSortid() { return sortid; }
public void setSortid(Integer sortid) { this.sortid = sortid; }
public void init() { session = new AnnotationConfiguration().configure() .buildSessionFactory().openSession(); }
public String findNewssort() { this.init(); List sorts = session.createCriteria(Newssort.class).list(); Map request = (Map) ActionContext.getContext().get(“request”); request.put(“sorts”, sorts); session.close(); return SUCCESS; }
public String findNews() { this.init(); System.out.println(“findNews”); List sorts = session.createCriteria(Newssort.class).list(); Newssort newssort = (Newssort) session.load(Newssort.class, sortid); List news = newssort.getNews(); Map request = (Map) ActionContext.getContext().get(“request”); request.put(“sorts”, sorts); request.put(“news”, news); session.close(); return SUCCESS; } }
hibernate.cfg.xml: < xml version=”1.0″ encoding=”UTF-8″ > “-//Hibernate/Hibernate Configuration DTD 3.0//EN” “http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”> org.hibernate.dialect.MySQLDialect com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/news root root true
struts.xml: < xml version=”1.0″ encoding=”UTF-8″ > “-//Apache Software Foundation//DTD Struts Configuration 2.0//EN” “http://struts.apache.org/dtds/struts-2.0.dtd”> /index.jsp
/index.jsp web.xml: < xml version=”1.0″ encoding=”UTF-8″ > xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”> struts2 org.apache.struts2.dispatcher.FilterDispatcher struts2 /* prepare.jsp
前台有两个jsp: prapare.jsp: <%@ page language=”java” pageEncoding=”GB18030″%>
My JSP ‘index.jsp’ starting page <script type=”text/java script”> window.location = “findNewssort.action”; index.jsp: <%@ page language=”java” pageEncoding=”GB18030″%> <%@taglib uri=”/struts-tags” prefix=”s” %>
My JSP ‘index.jsp’ starting page <script type=”text/java script”> function findNews() { var sort = document.getElementById(“sort”); window.location = “findNews.action sortid=” + sort.value; } 请选择 ” > 好了,一切OK,打开浏览器测试一切正常.