昨天用了一天整合了SSH,下面是具体步骤:
1.下载所需jar包,百度云地址http://pan.baidu.com/s/1i5mDgA5 提取码jdjh
2.安装好数据库,测试版本是SQLServer2008R2,其他版本都可以,只要可以连接上;
3.安装好myeclipse,测试版本是myeclipse10
4.整合
第一步:新建一个web工程,并将工程编码改为utf-8;
第二步:将所需要的jar包导入WebRoot-web-inf-lib文件夹下
第三步:在src下建一个com.pojo包,com.pojo包新建Users类,具体代码为:
public class Users {
private long uid;
private String username;
private String password;
public long getUid() {
return uid;
}
public void setUid(long uid) {
this.uid = uid;
}
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;
}
}
第四步:在com.pojo包下新建Users.hbm.xml,通过hibernate映射进行持久化,具体代码为:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 用来描述一个持久化类 name 类的全名 table 表名,可以不写,默认值和类名一样 catalog 数据库名称,一般不写 -->
<class name="com.pojo.Users" table="student" lazy="true">
<!-- 标示属性,和数据库主键对应 ,将Users类的pid映射为表中的主键pid column 列名称 length 数据库的字段长度,默认数据库最高的长度
type 类型 -->
<id name="uid" length="5" type="java.lang.Long">
<column name="id" />
<generator class="native"></generator>
</id>
<property name="username" type="java.lang.String">
<column name="username" length="56" />
</property>
<property name="password" type="java.lang.String">
<column name="password" length="56" />
</property>
</class>
</hibernate-mapping>
第五步:在src根目录下添加hibernate.cfg.xm文件,进行hibernate配置.具体代码为:
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://127.0.0.1:1435;DatabaseName=ssh</property>
<property name="connection.username">sa</property>
<property name="connection.password">admin123</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</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.internal.NoCacheProvider</property>-->
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com.pojo/Users.hbml.xml" />
</session-factory>
</hibernate-configuration>
其中数据库连接信息,根据实际情况做改变
第六步:先整合spring+hibernate,方便测试,在src根目录下添加applicationContext.xml文件,具体代码为:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- 0,配置bean的自动扫描与装配 -->
<context:component-scan base-package="src"></context:component-scan>
<!-- 1,配置数据源 -->
<!-- 1.1,导入 jdbc.properties 配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 1.2,配置数据源(c3p0) -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据库连接信息 -->
<property name="jdbcUrl" value="jdbc:sqlserver://127.0.0.1:1435;DatabaseName=ssh"></property>
<property name="user" value="sa"></property>
<property name="password" value="admin123"></property>
<property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
<!-- 其他配置 -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements" value="8"></property>
<!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection" value="5"></property>
<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>
<!-- 2,配置SessionFactory(整合Hibernate) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<!-- 3,配置声明式事务 -->
<!-- 3.1,配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 3.2,配置基于注解的事务支持-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
第七步:测试spring与hibernate整合是否成功,发布工程,运行,如果生成映射的数据库,则表示成功;
第八步:整合spring+Struts,在src目录下新建com.action包,在com.action包下新建类UsersAction类,具体代码为:
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
/**
* @author Likai
*/
private String username;
private String password;
public String login(){
if(password.equals("admin")){
return SUCCESS;
}
return ERROR;
}
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;
}
}
第九步:在src根目录下添加struts.xml文件,具体代码为:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encodeing" value="utf-8"/>
<!-- 把主题设为simple -->
<constant name="struts.ui.theme" value="simple" />
<package name="default" namespace="/" extends="struts-default">
<!-- 配置roleAction -->
<action name="login" class="com.action.UserAction" method="{1}">
<result name="success">WEB-INF/jsp/init.jsp</result>
</action>
</package>
</struts>
第十步:配置web.xml,来管理spring,hibernate,struts;具体代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
第十一步:编写jsp测试ssh
在web-inf下新建文件夹jsp,新建init.jsp,作为index.jsp请求成功后通过struts配置响应的jsp页面
web-root根目录下index.jsp的部分代码为:
<form action="login" method="post">
用户名<input type="text" name="username"><br>
密码<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
如果遇到中文乱码问题,可以参考上一篇关于解决JavaWeb乱码问题,不足之处,请多指教!!!