其中使用到得包有
第一个ssh的实例详细说明
今天,我们来看一下第一个ssh的项目详细说明,个人总结,这个是我自己的想法仅供参考---下面以一个实例说明
第一步,按照一定的顺序写出每一个包,如:
1.>cn.csdn.domain 就是一个实体类包
2.>cn.csdn.dao就是DAO—封装了真实的方法和其中的说明包
3.>cn.csdn.service就是Service---封转了DAO中的实现 和其中的说明(我觉得就是隐藏源代码)。
4.>cn.csdn.action就是action---也就是那个struts2中的一些动作,也就是需要的方法
第二步,按照这每一个包写出其中的类
首先我们看一下这个cn.csdn.domain中的类----Admin.java和一个hibernate配置文件
首先看一下这个Admin.java
package cn.csdn.domain;
import java.io.Serializable;
//创建了一个实体bean
public class Admin implements Serializable{
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String pass;
public Admin() { //这个方法就是为了在action中初始化的时候不出现空指针异常
super();
// TODO Auto-generatedconstructor stub
}
public Admin(String name, String pass) {
super();
this.name = name;
this.pass = pass;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
@Override
public String toString() {
return "Admin[id=" + id + ",name=" + name + ",pass=" + pass + "]";
}
}
配置文件Admin.hbm.xml
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.csdn.domain">
<class name="Admin"table="admin">
<id column="id"name="id" type="integer">
<generator class="native"/>
</id>
<property name="name"column="name" type="string"length="40" unique="true"/>
<property name="pass"column="pass" type="string"length="40"/>
</class>
</hibernate-mapping>
---------------------------------------------------------
然后看一下cn.csdn.dao中的类—adminDao.java和一个实现类adminDaoImpl
看第一个adminDao.java
package cn.csdn.dao;
import cn.csdn.domain.Admin;
public interface AdminDao {
Admin login(String name, String pass);
}
看第二个adminDaoImpl.java
package cn.csdn.dao;
import java.sql.SQLException;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.csdn.domain.Admin;
public class AdminDaoImpl extends HibernateDaoSupport implements AdminDao {
@Override
public Admin login(final String name, final String pass) {
return (Admin) this.getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Object obj = session
.createQuery(
"from Admin where name=:name and pass=:pass")
.setString("name", name)
.setString("pass",pass).uniqueResult();
return obj;
}
});
}
}
---------------------------------------------------------
然后我们看cn.csdn.service包中的类, AdminService.java和AdminServiceImpl.java
第一个我们来AdminService.java
package cn.csdn.service;
import cn.csdn.domain.Admin;
public interface AdminService {
Admin login(String name, String pass);
}
第二个AdminServiceImpl.java
package cn.csdn.service;
import cn.csdn.dao.AdminDao;
import cn.csdn.domain.Admin;
public class AdminServiceImpl implements AdminService {
//用adminDao注入
AdminDao adminDao;
public void setAdminDao(AdminDao adminDao) {
this.adminDao = adminDao;
}
@Override
public Admin login(String name, String pass) {
return adminDao.login(name, pass);
}
}
最后我们来看一下cn.csdn.action其中我们有一个AdminAction.java
看一下其中内容
package cn.csdn.action;
import cn.csdn.domain.Admin;
import cn.csdn.service.AdminService;
import com.opensymphony.xwork2.ActionSupport;
public class AdminAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private AdminService adminService;
private Admin admin;
public Admin getAdmin() {
return admin;
}
public void setAdmin(Admin admin) {
this.admin = admin;
}
// 注入业务bean
public void setAdminService(AdminService adminService) {
this.adminService = adminService;
}
public String login() {
System.out.println("==========================================");
admin = adminService.login(admin.getName(), admin.getPass());
System.out.println(admin.toString());
if (admin != null) {
return SUCCESS;
} else {
return INPUT;
}
}
}
第三步.就是写配置文件了
上面所说的就我们的MVC结构中的DAO层
我们来看一下配置文件吧!只是其中一个难点.这个配置文件的理解要基于我所学的struts2和spring配置文件的结合内容,很不好理解.还是我个人的理解啊!这个都在src根目录下面
首先,我们还是配置一下这个struts2中的配置文件struts.xml
看其中的内容
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD StrutsConfiguration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 常量的配置 -->
<!-- 国际化 -->
<constant name="struts.custom.i18n.resources" value="buy360"></constant>
<!-- 开发模式 -->
<constant name="struts.devMode" value="true"></constant>
<!-- 编码 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!-- 访问的后缀名称 -->
<constant name="struts.action.extension" value="action"></constant>
<!-- 文件上传的大小设置 10*1048576M =10M-->
<constant name="struts.multipart.maxSize" value="10485760"></constant>
<!-- 是用spring创建管理struts2的action操作 -->
<constant name="struts.objectFactory"value="spring"/>
<!-- 包含其它的配置文件 -->
<include file="struts-admin.xml"/>
</struts>
理解一下其实就是包含了一个配置文件,和配置了一些变量
我们再来看这个包含文件
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD StrutsConfiguration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name=""extends="struts-default" namespace="/csdn">
<!-- class的名称等于spring中action配置文件中的 id名称 -->
<action name="loginAdmin" class="adminAction" method="login">
<result name="success">../index.jsp</result>
</action>
</package>
</struts>
哦 这里看到了不容就是这个action 中的class为什么是一个adminAction不是一个类呢?
我们在来看一下下面的配置文件,这样我们就能解释了.
ApplicationContext.xml配置文件
<?xml version="1.0"encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 配置数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass"value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=UTF-8"/>
<property name="user"value="root" />
<property name="password"value="qiaoyu." />
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize"value="1" />
<!--连接池中保留的最小连接数。 -->
<property name="minPoolSize"value="1" />
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize"value="20" />
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime"value="60" />
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement"value="5" />
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod"value="60" />
</bean>
<!-- sessionFactory工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"ref="dataSource" />
<property name="mappingResources">
<list>
<value>cn/csdn/domain/Admin.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<!-- hiberante事务管理器 -->
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"ref="sessionFactory" />
</bean>
<!-- 事务的通知 -->
<tx:advice id="txadvice" transaction-manager="hibernateTransactionManager">
<tx:attributes>
<tx:method name="add*"isolation="DEFAULT" propagation="REQUIRED"/>
<tx:method name="insert*"isolation="DEFAULT" propagation="REQUIRED"/>
<tx:method name="find*"isolation="DEFAULT" propagation="REQUIRED"/>
<tx:method name="update*"isolation="DEFAULT" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 给事务配置一个切面 -->
<aop:config>
<!-- advisor 切入点和通知组合体 -->
<aop:pointcut expression="execution(*cn.csdn.service.*.*(..))"
id="txPointcut" />
<aop:advisor advice-ref="txadvice"pointcut-ref="txPointcut" />
</aop:config>
<!-- 导入其它的配置文件 -->
<import resource="beans-dao.xml"/>
<import resource="beans-service.xml"/>
<import resource="beans-action.xml"/>
</beans>
这个配置文件有长了,看一些其中的内容,
首先,配置一个数据库连接池--à配置一个sessionFactory工厂---à配置一个hibernate事务管理器------à事物的通知---à事务的切面--à最后导入别的配置文件
这下面我们从一个service层中的bean对象开始讲起beans-service.xml
<?xml version="1.0"encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--admin的业务操作 -->
<bean id="adminServiceImpl"class="cn.csdn.service.AdminServiceImpl">
<property name="adminDao" ref="adminDaoImpl"/>
</bean>
</beans>
这里我们知道就是配置了一个bean对象其中因为在service层中的AdminServiceImpl要使用到这个adminDao类中的一些方法,所以我们要在这里注入.我们注入的对象名字是:adminDao.其中我们引用的对象为adminDaoImpl(这个在下面)
我们再来看下一个配置文件beans-dao.xml
<?xml version="1.0"encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--HibernateDaoSupport -->
<bean id="hibernateDaoSupport"class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"abstract="true">
<property name="sessionFactory"ref="sessionFactory"/>
</bean>
<!--admin的Dao对象 -->
<bean id="adminDaoImpl"class="cn.csdn.dao.AdminDaoImpl" parent="hibernateDaoSupport"/>
</beans>
这里有一个hibernateDaoSupport这个类,我们看到其中的那个DaoImpl中也集成了那个hibernateDaoSupport这个类
其实这个类中就是使用了一个模板对象,如果想理解可以看一下源代码,下面的adminDaoImpl就是我们在上面引用的了! 这里我们就全部明白了然后就可以运行了,自己写一个页面测试一下即可,如:我们这个实例里面写的是:
这里就是第四部写一个网页:
<%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 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 titlehere</title>
</head>
<body>
<div>
<form action="/s2sh-this/csdn/loginAdmin.action"method="post">
用户名:<input type="text"name="admin.name"><br/>
密 码:<input type="password"name="admin.pass"><br />
<input type="submit"value="登陆"> <input type="reset"
value="重置">
</form>
</div>
<div>登陆成功的用户名${admin.name}</div>
</body>
</html>
第五步:我们要在这里更改这里的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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>s2sh</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>
<!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applic*.xml</param-value>
</context-param>
<!-- 对Spring容器进行实例化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- struts2的配置 -->
<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>
<!-- 配置使用spring解决hibernate因session关闭导致的延迟加载例外问题 -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<!-- 指定org.springframework.orm.hibernate3.LocalSessionFactoryBean在spring配置文件中的名称,默认值为sessionFactory.如果LocalSessionFactoryBean在spring中的名称不是sessionFactory,该参数一定要指定,否则会出现找不到sessionFactory的例外 -->
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 使用spring解决struts2中文乱码的问题 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
这里面的内容没有什么好解释的,一般都这样写