站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题其它技术和Hibernate-JSP2.0

其它技术和Hibernate-JSP2.0

摘要: Hibernate JSP2 JSTL EL

其它技术和Hibernate-JSP2.0

使用Hibernate,如果再加上JSP2.0,设计Web应用就更方便了,本文从使用JSP2.0(主要用到JSTL和EL)的一个小例子,体现了Hibernate和JSP2.0结合使用的好处。

使用JSP2.0的EL,可以对Java 中的Collections方便操作,其中一个要求是Collections是一个Java Bean形式的,既对于一个Property,要有get()和set().因为Hibernate在设计类时也是这种get() set()形式,所以用Hibernate进行设计,在JSP中就可以方便的显示出来。下面是一个应用中的User类的例子。其中不重要的部分用//......注释掉了,主要关系为,一个User,可要多个Position。

使用Hibernate 的User:

User 类:用XDoclet可以方便的从类生成Hibernate的map file,这里省略。

User.java

package bean;

import java.util.*;  

import net.sf.hibernate.Validatable;

import net.sf.hibernate.ValidationFailure; 

public class User implements Validatable{

   protected int id;

   private String name;  

   private String email;

   //......

   private Set positions=new TreeSet();  

   public int getId() {

     return id;

   }

   public void setId(int id) {

     this.id = id;

   }

   public String getEmail(){

     return email;

   }

   public void setEmail(String value){

     email=value;

   }    

   public String getName(){

     return name;

   }

   public void setName(String value){

     name=value;

   }

   public Set getPositions(){

     return positions;

   }

   public void setPositions(Set value){

     positions=value;

   }

   //......

}

对User操作的类:

Users.java:

package test;
import bean.User;
import bean.Position;
import java.util.*;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.*;
public class Users extends DAO{
public static List getUsers(){
return dao.find("from User user order by user.name");
}
public static String addUser(Map params){
//......
}
public static User getUser(String id){
return (User)dao.findById(User.class,id);
}
public static boolean hasUser(String name){
User u=getUserByName(name);
if(u!=null) return true;
return false;
}
public static User getUserByName(String name){
if(name==null) return null;
List l=dao.find("from User as user where user.name = ?",name,Hibernate.STRING);
Iterator iter=l.iterator();
if(iter.hasNext()) return (User)iter.next();
return null;
}
public static String delUser(String id){
User u=(User)dao.findById(User.class,id);
return delUser(u);
}
public static String delUser(User user){
try{
if(user!=null) dao.remove(user);
}catch(HibernateException e){
e.printStackTrace();
return "Hibernate Error";
}
return null;
}
public static String modUser(User u,Map params){
//......
try{
dao.update(u);
}catch(HibernateException e){
return "Hibernate Error";
}
return null;
} //......
}

用JSP2.0的User 的JSP:

user.jsp

   <%@ page contentType="text/html; charset=GBK"%>

   <%@ page import="bean.User"%>

   <%@ page import="test.*"%>

   <%@ page import="java.util.*"%>

   <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"    %>

   <%

   pageContext.setAttribute("users",Users.getUsers());

   %>

   

   <head>

   

   <link> href="../css/anderson.css" rel="stylesheet" type="text/css">

   User

   </head>

   

User


返回

<table width='50%'> <tr><td class='td-title' colspan='2'>用户</td></tr> <tr> <td>${user.name}</td> <td>删除</td> </tr> </table>

新增

</body>

user.jsp注:

通过JSTL的forEach,就可以方便的对users进行操作,不需要<% for(.....){} while(......){} %>,其中EL${user.id}其实就是if(user!=null) user.getId();

user_view.jsp:

   <%@ page contentType="text/html; charset=GBK"%>

   <%@ page import="java.util.*"%>

   <%@ page import="test.*"%>

   <%@ page import="bean.*"%>

   <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"    %>

   <%

   String id=request.getParameter("id");

   User user=Users.getUser(id);

   if(user==null){

   response.sendRedirect("user.jsp");

   return;

   }

   pageContext.setAttribute("user",user);

   %>

   

   <head>

   

   <link> href="./css/anderson.css" rel="stylesheet" type="text/css">

   User

   </head>

   

<table class='table-normal' cellpadding='6' cellspacing='0'> <tr><td class='td-subject'>用户-查看</tr> </table>
<table class='table-normal' cellpadding='2' cellspacing='1'> <tr><td width='40%'>姓名</td><td>${user.name}</td></tr> <tr><td>性别</td><td>${user.sex}</td></tr> <tr><td>Email </td><td>${user.email}</td></tr> <tr><td>电话</td><td>${user.hometel}</td></tr> <tr><td>手机</td><td>${user.mobile}</td></tr> <tr><td class='td-add' colspan='2'> 修改&nbsp; 删除&nbsp; </td></tr> </table>
<table class='table-normal' cellpadding='2' cellspacing='1'> <tr><td class='td-title' colspan='2'>职位</td></tr> <tr><td class='td-title-sub'>组织</td><td class='td-title-sub'>名称</td></tr> <tr><td width='40%'>${v.org.name}</td><td>${v.name}</td></tr> <tr><td class='td-add' colspan='2'> 修改&nbsp; </td></tr> </table>
<table class='table-normal' cellpadding='6' cellspacing='0'> <tr><td class='td-end' align='center'> 返回 </td></tr> </table> </body>

user_view.jsp注:

其中${user.positions} 调用user.getPositions(),因为Hibernate会把相关的对象取出,写程序就不需要SQL的join 几个表了,用在JSP中写的代码很少。

小结:

JSP2.0的JSTL有许多,如等等。Tomcat 和 Resin现在都支持JSP2.0 和Servlet 2.4。

使用JSP2.0和Hibernate,不用EJB等也可以方便开发Web应用。

参考:

Hibernate 主站:www.hibernate.org

Hibernate Xdoclet:http://www.chinaunix.net/jh/26/92019.html

XDoclet 主站:http://xdoclet.sourceforge.net/

一个JSTL介绍:http://www.javapassion.com/j2eeadvanced/JSTL_speakernoted.pdf



↑返回目录
前一篇: JSP中tomcat的SQL Server2000数据库连接池的配置
后一篇: Tomcat 5 servlet/JSP 容器 JNDI 资源使用说明 (一)