create table elements (id integer(3) identity,
name char(30),
number char(30),
mass char(30),
symbol char(2));
create unique index ui_elements_pk on elements (symbol)
insert into elements ( name, number, mass, symbol) values ('manganese','25','55','mn');
insert into elements ( name, number, mass, symbol) values ('zinc','30','65','zn');
insert into elements ( name, number, mass, symbol) values ('thulium','69','169','tm');
insert into elements ( name, number, mass, symbol) values ('californium','98','251','cf');
insert into elements ( name, number, mass, symbol) values ('gold','79','197','au');
insert into elements ( name, number, mass, symbol) values ('ytterbium','70','173','yb');
insert into elements ( name, number, mass, symbol) values ('molybdenum','42','96','mo');
insert into elements ( name, number, mass, symbol) values ('palladium','46','106','pd');
package com.strutsrecipes.hibernate.beans;
public class element {
private string name;
private string symbol;
private string number;
private string mass;
private int id;
public element() {
super();
}
public element(string name, string symbol, string number, string mass) {
this.name = name;
this.symbol = symbol;
this.number = number;
this.mass = mass;
}
public int getid() {
return id;
}
public void setid(int id) {
this.id = id;
}
public string getmass() {
return mass;
}
public string getname() {
return name;
}
public string getnumber() {
return number;
}
public string getsymbol() {
return symbol;
}
public void setmass(string mass) {
this.mass = mass;
}
public void setname(string name) {
this.name = name;
}
public void setnumber(string number) {
this.number = number;
}
public void setsymbol(string symbol) {
this.symbol = symbol;
}
}
<?xml version="1.0"?>
<!doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd//en"
"http://hibernate.sf.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.strutsrecipes.hibernate.beans.element" table="elements">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
<property name="number" column="number"/>
<property name="mass" column="mass"/>
<property name="symbol" column="symbol"/>
</class>
</hibernate-mapping>
<?xml version='1.0' encoding='utf-8'?>
<!doctype hibernate-configuration public "-//hibernate/hibernate configuration dtd//en"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">.net.sf.hibernate.dialect.hsqldialect</property>
<property name="connection.driver_class">org.hsqldb.jdbcdriver</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="connection.url">jdbc:hsqldb:hsql://127.0.0.1</property>
<property name="show_sql"> </property>
<property name="">true</property>
<mapping resource="/com/strutscookbook/hibernate/beans/element.hbm.xml"/>
</session-factory>
</hibernate-configuration>
package com.strutsrecipes.hibernate.plugin;
import java.net.url;
import javax.servlet.servletexception;
import.net.sf.hibernate.hibernateexception;
import.net.sf.hibernate.mappingexception;
import.net.sf.hibernate.sessionfactory;
import.net.sf.hibernate.cfg.configuration;
import org.apache.commons.logging.log;
import org.apache.commons.logging.logfactory;
import org.apache.struts.action.actionservlet;
import org.apache.struts.action.plugin;
import org.apache.struts.config.moduleconfig;
public class hibernateplugin implements plugin {
private configuration config;
private sessionfactory factory;
private string path = "/hibernate.cfg.xml";
private static class clazz = hibernateplugin.class;
public static final string key_name = clazz.getname();
private static log log = logfactory.getlog(clazz);
public void setpath(string path) {
this.path = path;
}
public void init(actionservlet servlet, moduleconfig modconfig)
throws servletexception {
try {
url url = hibernateplugin.class.getresource(path);
config = new configuration().configure(url);
factory = config.buildsessionfactory();
servlet.getservletcontext().setattribute(key_name, factory);
} catch (mappingexception e) {
log.error("mapping error", e);
throw new servletexception();
} catch (hibernateexception e) {
log.error("hibernate error", e);
throw new servletexception();
}
}
public void destroy() {
try {
factory.close();
} catch (hibernateexception e) {
log.error("unable to close factory", e);
}
}
}
<?xml version="1.0" encoding="iso-8859-1" ?>
<!doctype struts-config public
"-//apache software foundation//dtd struts configuration 1.1//en"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="searchform"type="com.strutsrecipes.hibernate.forms.searchform"/>
</form-beans>
<global-forwards>
<forward name="search" path="/search.jsp"/>
<forward name="searchsubmit" path="/searchsubmit.do"/>
</global-forwards>
<action-mappings>
<action path="/searchsubmit"
type="com.strutsrecipes.hibernate.actions.searchaction"
name="searchform"
scope="request"
input="/search.jsp">
<forward name="success" path="/element.jsp"/>
</action>
</action-mappings>
<plug-in classname="com.strutsrecipes.hibernate.plugin.hibernateplugin">
<set-property property="path" value="/hibernate.cfg.xml"/>
</plug-in>
</struts-config>
package com.strutsrecipes.hibernate.forms;
import org.apache.struts.action.actionform;
public class searchform extends actionform {
string symbol;
public string getsymbol() {
return symbol;
}
public void setsymbol(string symbol) {
this.symbol = symbol;
}
}
package com.strutsrecipes.hibernate.actions;
import java.util.list;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.apache.commons.logging.log;
import org.apache.commons.logging.logfactory;
import.net.sf.hibernate.hibernate;
import.net.sf.hibernate.hibernateexception;
import.net.sf.hibernate.session;
import.net.sf.hibernate.sessionfactory;
import org.apache.struts.action.action;
import org.apache.struts.action.actionerror;
import org.apache.struts.action.actionerrors;
import org.apache.struts.action.actionform;
import org.apache.struts.action.actionforward;
import org.apache.struts.action.actionmapping;
import com.strutsrecipes.hibernate.beans.element;
import com.strutsrecipes.hibernate.forms.searchform;
import com.strutsrecipes.hibernate.plugin.hibernateplugin;
public class searchaction extends action {
private static log log = logfactory.getlog(searchaction.class);
final public static string hql_find_element =
"from com.strutsrecipes.hibernate.beans.element as e where e.symbol = ?";
public actionforward execute(
actionmapping mapping,
actionform form,
httpservletrequest request,
httpservletresponse response)
throws exception {
searchform searchform = (searchform) form;
element element = null;
list elements = null;
sessionfactory factory = null;
session session = null;
try {
factory =
(sessionfactory) servlet.getservletcontext()
.getattribute(hibernateplugin.key_name);
session = factory.opensession();
elements = session.find(hql_find_element,searchform.getsymbol(),
hibernate.string);
if (!elements.isempty()) {
element = (element) elements.get(0);
}
} catch (hibernateexception e) {
log.error("hibernate error", e);
} finally {
log.error("hibernate exception encountered");
session.close();
}
if (element != null) {
request.setattribute("element", element);
return mapping.findforward("success");
}
actionerrors errors = new actionerrors();
errors.add(actionerrors.global_error,
new actionerror("error.notfound"));
saveerrors(request, errors);
return mapping.getinputforward();
}
}
<%@ page language="java" %>
<%@ taglib uri="/web-inf/struts-html.tld" prefix="html" %>
<html:html>
<body>
<h1>search for an element</h1>
<html:form action="/searchsubmit.do">
symbol <form:text property="symbol"/>
<html:submit value="search"/>
</html:form>
<html:errors/>
</body>
</html:html>
listing 10. element.jsp
<%@ page language="java" %>
<%@ taglib uri="/web-inf/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/web-inf/struts-html.tld" prefix="html" %>
<html:html>
<h1>periodic element</h1>
name: <bean:write name="element" property="name"/><br>
symbol: <bean:write name="element" property="symbol"/><br>
number: <bean:write name="element" property="number"/><br>
mass: <bean:write name="element" property="mass"/><br>
<html:link forward="search">search</html:link><p>
</html:html>
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器