一个简单的Struts Hibernate入门例子(下)
2008-07-17 23:06
726 查看
hibernate.cfg.xml文件内容:
<?xml version='1.0' encoding="GBK"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Oracle connection settings
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@192.168.0.199:1521:portal2</property>
<property name="connection.username">portal</property>
<property name="connection.password">portal</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
-->
<!-- MySql connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=gbk</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- Add your mapping resource(.hbm.xml) here -->
<mapping resource="com/student/entity/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
Student.hbm.xml文件内容:
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.student.entity">
<class name="Student" table="prj_student">
<id name="id" column="id" type="integer">
<generator class="native" />
</id>
<!--
<id name="id" column="id" type="integer">
<generator class="sequence">
<param name="sequence">prj_student_seq</param>
</generator>
</id>-->
<property name="name" column="name" type="string" />
<property name="birthday" column="birthday" type="date" />
</class>
</hibernate-mapping>
struts-config.xml文件内容:
<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<action-mappings>
<action path="/service" type="com.student.action.StudentAction" parameter="method">
<forward name="tolist" path="/service.do?method=find" redirect="true"/>
<forward name="modify" path="/modify.jsp" />
<forward name="list" path="/list.jsp" />
<forward name="add" path="/add.jsp" />
<forward name="error" path="/error.jsp" />
</action>
</action-mappings>
</struts-config>
web.xml文件内容:
<?xml version="1.0" encoding="GBK"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
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"
version="2.4">
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>
com.student.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>ActionServlet</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ActionServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
SQL脚本(Oracle)
create table prj_student
(
id number(7),
name varchar2(20) not null,
birthday date,
primary key(id)
)
create sequence prj_student_seq;
JSP文件(共四个)如下:
1,index.jsp
<jsp:forward page="/service.do?method=find" />
2,list.jsp
<%@page contentType="text/html;charset=gbk" %>
<%@page import="java.util.Iterator,com.student.entity.Student"%>
<jsp:useBean id="students" type="java.util.Collection" scope="request" />
<html>
<head>
<title>list</title>
</head>
<body>
<h3 align="center">Student List</h3>
<hr>
<table align="center" border="1" width="650" cellpadding="5" cellspacing="0">
<tr>
<th>序号</th>
<th>姓名</th>
<th>生日</th>
<th>操作</th>
</tr>
<%
Iterator iter = students.iterator();
int no = 0;
while(iter.hasNext())
{
Student student = (Student)iter.next();
%>
<tr>
<td align="center"><%= ++no%></td>
<td align="center"><%= student.getName()%></td>
<td align="center"><%= student.getBirthday()%></td>
<td align="center">
<a href="<%= request.getContextPath()%>/service.do?method=tomodify&id=<%= student.getId()%>">修改</a> |
<a href="<%= request.getContextPath()%>/service.do?method=remove&id=<%= student.getId()%>" onclick="return confirm('真的要删除该学生吗?')">删除</a>
</td>
</tr>
<%
}
if(no == 0)
{
%>
<tr>
<td height="50" align="center" colspan="4"><font color="red">没有符合条件的学生</font></td>
</tr>
<%
}
%>
</table>
<br>
<center>
<input type="button" value="add" onclick="window.location='<%= request.getContextPath()%>/service.do?method=toadd'">
</center>
</body>
</html>
3,add.jsp
<%@page contentType="text/html;charset=gbk" %>
<html>
<head>
<title>add</title>
</head>
<body>
<h3 align="center">Student Add</h3>
<hr>
<form method="get" action="<%= request.getContextPath()%>/service.do">
<input type="hidden" name="method" value="add" >
<table align="center" border="1" width="450" cellpadding="5" cellspacing="0">
<tr>
<td>姓名</td>
<td>
<input type="text" name="name" size="15">
</td>
<td>生日</td>
<td>
<input type="text" name="birthday" size="10">
</td>
</tr>
</table>
<br>
<center>
<input type="submit" value="add">
</center>
</form>
</body>
</html>
4,modify.jsp
<%@page contentType="text/html;charset=gbk" %>
<jsp:useBean id="student" type="com.student.entity.Student" scope="request" />
<html>
<head>
<title>modify</title>
</head>
<body>
<h3 align="center">Student Modify</h3>
<hr>
<form method="post" action="<%= request.getContextPath()%>/service.do">
<input type="hidden" name="method" value="modify" >
<input type="hidden" name="id" value="<%= student.getId()%>">
<table align="center" border="1" width="450" cellpadding="5" cellspacing="0">
<tr>
<td>姓名</td>
<td>
<input type="text" name="name" size="15" value="<%= student.getName()%>">
</td>
<td>生日</td>
<td>
<input type="text" name="birthday" size="10" value="<%= student.getBirthday()%>">
</td>
</tr>
</table>
<br>
<center>
<input type="submit" value="save">
</center>
</form>
</body>
</html>
<?xml version='1.0' encoding="GBK"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Oracle connection settings
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@192.168.0.199:1521:portal2</property>
<property name="connection.username">portal</property>
<property name="connection.password">portal</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
-->
<!-- MySql connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=gbk</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- Add your mapping resource(.hbm.xml) here -->
<mapping resource="com/student/entity/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
Student.hbm.xml文件内容:
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.student.entity">
<class name="Student" table="prj_student">
<id name="id" column="id" type="integer">
<generator class="native" />
</id>
<!--
<id name="id" column="id" type="integer">
<generator class="sequence">
<param name="sequence">prj_student_seq</param>
</generator>
</id>-->
<property name="name" column="name" type="string" />
<property name="birthday" column="birthday" type="date" />
</class>
</hibernate-mapping>
struts-config.xml文件内容:
<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<action-mappings>
<action path="/service" type="com.student.action.StudentAction" parameter="method">
<forward name="tolist" path="/service.do?method=find" redirect="true"/>
<forward name="modify" path="/modify.jsp" />
<forward name="list" path="/list.jsp" />
<forward name="add" path="/add.jsp" />
<forward name="error" path="/error.jsp" />
</action>
</action-mappings>
</struts-config>
web.xml文件内容:
<?xml version="1.0" encoding="GBK"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
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"
version="2.4">
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>
com.student.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>ActionServlet</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ActionServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
SQL脚本(Oracle)
create table prj_student
(
id number(7),
name varchar2(20) not null,
birthday date,
primary key(id)
)
create sequence prj_student_seq;
JSP文件(共四个)如下:
1,index.jsp
<jsp:forward page="/service.do?method=find" />
2,list.jsp
<%@page contentType="text/html;charset=gbk" %>
<%@page import="java.util.Iterator,com.student.entity.Student"%>
<jsp:useBean id="students" type="java.util.Collection" scope="request" />
<html>
<head>
<title>list</title>
</head>
<body>
<h3 align="center">Student List</h3>
<hr>
<table align="center" border="1" width="650" cellpadding="5" cellspacing="0">
<tr>
<th>序号</th>
<th>姓名</th>
<th>生日</th>
<th>操作</th>
</tr>
<%
Iterator iter = students.iterator();
int no = 0;
while(iter.hasNext())
{
Student student = (Student)iter.next();
%>
<tr>
<td align="center"><%= ++no%></td>
<td align="center"><%= student.getName()%></td>
<td align="center"><%= student.getBirthday()%></td>
<td align="center">
<a href="<%= request.getContextPath()%>/service.do?method=tomodify&id=<%= student.getId()%>">修改</a> |
<a href="<%= request.getContextPath()%>/service.do?method=remove&id=<%= student.getId()%>" onclick="return confirm('真的要删除该学生吗?')">删除</a>
</td>
</tr>
<%
}
if(no == 0)
{
%>
<tr>
<td height="50" align="center" colspan="4"><font color="red">没有符合条件的学生</font></td>
</tr>
<%
}
%>
</table>
<br>
<center>
<input type="button" value="add" onclick="window.location='<%= request.getContextPath()%>/service.do?method=toadd'">
</center>
</body>
</html>
3,add.jsp
<%@page contentType="text/html;charset=gbk" %>
<html>
<head>
<title>add</title>
</head>
<body>
<h3 align="center">Student Add</h3>
<hr>
<form method="get" action="<%= request.getContextPath()%>/service.do">
<input type="hidden" name="method" value="add" >
<table align="center" border="1" width="450" cellpadding="5" cellspacing="0">
<tr>
<td>姓名</td>
<td>
<input type="text" name="name" size="15">
</td>
<td>生日</td>
<td>
<input type="text" name="birthday" size="10">
</td>
</tr>
</table>
<br>
<center>
<input type="submit" value="add">
</center>
</form>
</body>
</html>
4,modify.jsp
<%@page contentType="text/html;charset=gbk" %>
<jsp:useBean id="student" type="com.student.entity.Student" scope="request" />
<html>
<head>
<title>modify</title>
</head>
<body>
<h3 align="center">Student Modify</h3>
<hr>
<form method="post" action="<%= request.getContextPath()%>/service.do">
<input type="hidden" name="method" value="modify" >
<input type="hidden" name="id" value="<%= student.getId()%>">
<table align="center" border="1" width="450" cellpadding="5" cellspacing="0">
<tr>
<td>姓名</td>
<td>
<input type="text" name="name" size="15" value="<%= student.getName()%>">
</td>
<td>生日</td>
<td>
<input type="text" name="birthday" size="10" value="<%= student.getBirthday()%>">
</td>
</tr>
</table>
<br>
<center>
<input type="submit" value="save">
</center>
</form>
</body>
</html>
相关文章推荐
- 一个简单的Struts Hibernate入门例子(上)
- struts+hibernate+spring一个入门的实用例子
- struts+hibernate+spring一个入门的实用例子(转)
- struts+hibernate+spring一个入门的实用例子
- struts2入门-第一个例子==》第一个struts2实例——HelloWorld==》Struts2.0搭建和简单实例==>写一个Steuts2的经典入门案例,做了一个登陆程序作为入门例子
- struts+hibernate+spring一个入门的实用例子(上)
- struts+hibernate+spring一个入门的实用例子(下)
- Struts入门的一个例子
- 一个简单的Hibernate例子!
- WCF 一步一步从入门到精通(一)建立一个简单入门的例子(适合一点不会的朋友阅读)
- Spring入门-一个简单的小例子
- Hibernate4.2.4入门(一)——环境搭建和简单例子
- hibernate级联操 同时向2张表插入数据(一对多的关联,一个简单的例子说明)
- hibernate级联操 同时向2张表插入数据(一对多的关联,一个简单的例子说明)
- 一个简单的例子入门Vue.js
- 菜鸟学习Hibernate——简单的一个例子
- 一个简单的Hibernate例子的心得
- (3.1.4.1)一个简单的struts例子
- 一个简单的iBatis入门例子
- Spring+struts2+hibernate框架的Java Web项目利用POI导出Excel简单例子