总的来说,struts2+hibernate3.0开发,就是struts2与hibernate3.0简单的叠加。为了记录,还是把它写下来
第一步
所需要的jar包
antlr-2.7.6.jar
commons-collections-3.1.jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-lang-2.6.jar
commons-logging.jar
dom4j-1.6.1.jar
freemarker-2.3.15.jar
hibernate3.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
mysql-connector-java-5.0.8-bin.jar
ognl-2.7.3.jar
slf4j-api-1.5.8.jar
struts2-core-2.1.8.jar
xwork-core-2.1.6.jar
第二步
struts.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.i18n.encoding" value="utf-8"/>
<constant name="struts.ui.theme" value="simple"/>
<constant name="struts.custom.i18n.resources" value="application"/>
<package name="default" extends="struts-default" namespace="/">
<action name="login" class="com.ppt.action.TestAction" method="login">
<result name="login">/index.jsp</result>
</action>
</package>
</struts>
第三步
web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>strutsTest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
第四步
hibernate.cfg.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="">
<!--Examda提示:数据库用户名-->
<property name="connection.url">jdbc:mysql://localhost:3306/ppt</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">True</property>
<!--Examda,是否使用数据库外连接-->
<property name="hibernate.use_out_join">True</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/ppt/domain/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
第五步
实体类User
package com.ppt.domain;
import java.io.Serializable;
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 801137863725022747L;
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
第六步
映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!--
hibernate-mapping有几个可选的属性:
schema属性指明了这个映射的表所在的schema名称。
default-cascade属性指定了默认的级联风格 可取值有 none、save、update。
auto-import属性默认让我们在查询语言中可以使用非全限定名的类名 可取值有 true、false。
package属性指定一个包前缀。
native
-->
<hibernate-mapping package="com.ppt.domain">
<class name="User" table="USER">
<id name="id" column="ID" type="int">
<generator class="assigned"/>
</id>
<property name="username" column="USERNAME" type="string" length="255" not-null="true"/>
<property name="password" column="PASSWORD" type="string" length="255" not-null="true"/>
</class>
</hibernate-mapping>
第七步
action类,testAction
package com.ppt.action;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import com.opensymphony.xwork2.ActionSupport;
import com.ppt.domain.User;
public class TestAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 3505638234491695571L;
User user = new User();
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String login() {
Session session;
// Transaction transaction;
Configuration configuration = new Configuration().configure();
session = configuration.buildSessionFactory().openSession();
User u = (User)session.get(User.class, 1);
System.out.println(u.getPassword());
System.out.println(user.getUsername());
System.out.println(user.getPassword());
return "login";
}
}
第八步
jsp文件---index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="login" method="post">
<table>
<tr>
<td>用户名</td>
<td><s:textfield name="user.username"></s:textfield> </td>
</tr>
<tr>
<td>密码 </td>
<td><s:textfield name="user.password"></s:textfield> </td>
</tr>
<tr>
<td colspan='2'><s:submit value="提交"></s:submit> <s:reset value="重置"></s:reset></td>
</tr>
</table>
</s:form>
</body>
</html>
到此,struts2+hibernate3.0开发算结束