【转载】Ssh整合开发介绍和简单的登入案例实现
Ssh整合开发介绍和简单的登入案例实现
- 一 介绍:
- Ssh是strtus2-2.3.1.2+ spring-2.5.6+hibernate-3.6.8整合的开发,这是目前我的整合开发的使用技术和版本,使用的数据库为mySql。使用的开发工具是eclipse,eplipse的版本为Indigo Service Release 2
- 二 搭建环境
- 1. 首先要先引入struts2和sping和hibernate所需要的包
- (1)struts2的包为:
- struts-2.3.1.2\lib\ struts2-core-2.3.1.2.jar
- struts-2.3.1.2\lib\ognl-3.0.4.jar
- struts-2.3.1.2\lib\ xwork-core-2.3.1.2.jar
- struts-2.3.1.2\lib\ freemarker-2.3.18.jar
- struts-2.3.1.2\lib\commons-fileupload-1.2.2.jar
- struts-2.3.1.2\lib\ commons-io-2.0.1.jar
- struts-2.3.1.2\lib\commons-lang-2.5.jar
- struts-2.3.1.2\lib\commons-logging-1.1.1.jar
- struts-2.3.1.2\lib \struts2-json-plugin-2.3.1.2.jar
- struts-2.3.1.2\lib \struts2-spring-plugin-2.3.1.2.jar
- (2)引入hibernate的包
- hibernate-distribution-3.6.8.Final\lib\required\*.jar 所有的jar包
- hibernate-distribution-3.6.8.Final\lib\jpa\hibernate-jpa-2.0-api-1.0.1.Final.jar
- hibernate-distribution-3.6.8.Final\ hibernate3.jar
- (3)spring的包为:
- spring-framework-2.5.6\dist\ spring.jar
- spring-framework-2.5.6\lib\c3p0\c3p0-0.9.1.2.jar
- spring-framework-2.5.6\lib\aspectj\*.jar
- spring-framework-2.5.6\lib\j2ee\common-annotations.jar
- spring-framework-2.5.6\lib\log4j\log4j-1.2.15.jar
- spring-framework-2.5.6\lib\jakarta-commons\ commons-logging.jar
- spring-framework-2.5.6\lib\cglib\cglib-nodep-2.1_3.jar
- spring-framework-2.5.6\lib\dom4j\dom4j-1.6.1.jar
- 2. 配置spring下所需要的文件
- (1)首先配置spring所需要的xml文件
- 我们在class下,即在src下创建一个applicationContext.xml的xml文件,文件的头文件因为要用到各种标签,所以我们在引入头文件的时候尽量引的比较多点,代码为:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- </beans>
- (2)在xml中配置和数据库相关联,并用c3p0来配置数据库连接池
- <!-- 数据库的连接池 -->
- <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="1234" />
- <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
- <property name="initialPoolSize" value="1" />
- <!--连接池中保留的最小连接数。 -->
- <property name="minPoolSize" value="1" />
- <!--连接池中保留的最大连接数。Default: 15 -->
- <property name="maxPoolSize" value="300" />
- <!--最大空闲时间,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>
- 当然,这样所写的可以在一个properties中读取。读取外部文件在xml中的使用为:
- <!-- 读取外部的文件 -->
- <context:property-placeholder location="jdbc.properties" />
- (3)配置sessionFactory工厂,相当于是hibernate.cfg.xml里面的配置
- <!-- sessionFactory工厂 -->
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <!-- 注入数据源 -->
- <property name="dataSource" ref="dataSource"></property>
- <!-- hibernate映射文件的引入 -->
- <property name="mappingResources">
- <list>
- <value>cn/csdn/hr/s2sh/domain/Admin.hbm.xml</value>
- </list>
- </property>
- <!-- 配置hibernate属性的设置 -->
- <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>
- 3.配置struts2.xml中需要的内容
- (1)配置基本的struts2.xml
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd">
- <struts>
- <!--使用spring创建管理struts2的action操作 -->
- <constant name="struts.objectFactory" value="spring"/>
- <include file="struts-admin.xml"></include>
- </struts>
- 注:其中include是引入的文件,一下的语句是非常重要的:
- <constant name="struts.objectFactory" value="spring"/>,它是struts2和spring的连接的关键
- (2)引入的struts-admin.xml文件,意在检测并作出一个简单的登入的实现
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd">
- <struts>
- <package name="admin" 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>
- 4. Hibernate.cfg.xml中的内容可以省略,它已经在spring中的xml中配置了
- 5.搭建层之间的关系
- (1)首先是domain,包为cn.csdn.hr.s2sh.domain,我们以admin为例
- 属性为id和name和pass
- 封装的实体类为:
- package cn.csdn.hr.s2sh.domain;
- import java.io.Serializable;
- public class Admin implements Serializable {
- private static final long serialVersionUID = 1L;
- private Integer id;
- private String name;
- private String pass;
- public Admin() {
- super();
- // TODO Auto-generated constructor stub
- }
- public Admin(Integer id, String name, String pass) {
- super();
- this.id = id;
- 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 + "]";
- }
- }
- (2)在同一个包下的实体类的映射文件
- <?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.hr.s2sh.domain">
- <class name="Admin" table="admin">
- <id name="id" column="id">
- <generator class="native"></generator>
- </id>
- <property name="name" column="name"></property>
- <property name="pass" column="pass"></property>
- </class>
- </hibernate-mapping>
- (3)创建dao层,创建的包名为cn.csdn.hr.s2sh.dao,创建的接口名为AdminDao,类名为AdminDaoImpl
- 下面是创建的接口 AdminDao.java
- package cn.csdn.hr.s2sh.dao;
- import java.util.List;
- import cn.csdn.hr.s2sh.domain.Admin;
- public interface AdminDao {
- public Admin login(final String name,final String pass);
- }
- 创建的类为AdminDaoImpl.java
- package cn.csdn.hr.s2sh.dao;
- import java.sql.SQLException;
- import java.util.List;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.springframework.orm.hibernate3.HibernateCallback;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import cn.csdn.hr.s2sh.domain.Admin;
- @SuppressWarnings("unchecked")
- public class AdminDaoImpl extends HibernateDaoSupport implements AdminDao {
- public Admin login(final String name, final String pass) {
- return (Admin) this.getHibernateTemplate().execute(
- new HibernateCallback() {
- public Object doInHibernate(Session session)
- throws HibernateException, SQLException {
- // TODO Auto-generated method stub
- Object obj = session
- .createQuery(
- "from Admin where name=:name and pass=:pass")
- .setString("name", name)
- .setString("pass", pass).uniqueResult();
- return obj;
- }
- });
- }
- }
- (4)创建service层,创建的包名为cn.csdn.hr.s2sh.service
- 创建的AdminService.java接口
- package cn.csdn.hr.s2sh.service;
- import cn.csdn.hr.s2sh.dao.AdminDao;
- public interface AdminService extends AdminDao{
- }
- 创建的AdminServiceImpl.java
- package cn.csdn.hr.s2sh.service;
- import java.util.List;
- import org.springframework.transaction.TransactionStatus;
- import org.springframework.transaction.support.TransactionCallback;
- import org.springframework.transaction.support.TransactionTemplate;
- import cn.csdn.hr.s2sh.dao.AdminDao;
- import cn.csdn.hr.s2sh.domain.Admin;
- public class AdminServiceImpl implements AdminService {
- private AdminDao adminDao;
- public void setAdminDao(AdminDao adminDao) {
- this.adminDao = adminDao;
- }
- public Admin login(String name, String pass) {
- // TODO Auto-generated method stub
- return adminDao.login(name, pass);
- }
- }
- 6.创建访问struts2的时候的action,我们把action放到一个bean-action.xml中
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <!-- 创建struts2中的action -->
- <!-- 配置admin的action -->
- <bean id="adminAction" class="cn.csdn.hr.s2sh.action.AdminAction"
- scope="prototype">
- <property name="adminService" ref="adminServiceImpl"></property>
- </bean>
- </beans>
- 而ref的adminServiceImpl是在bean-service.xml中
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-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.hr.s2sh.service.AdminServiceImpl">
- <property name="adminDao" ref="adminDaoImpl"></property>
- </bean>
- </beans>
- 上面的ref是adminDaoImpl,在beans-dao.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <!-- hibernatedao的模板类 HibernateDaoSuppor 抽象类不可以实例化 加上abstract="true" -->
- <bean id="hibernateDaoSupport"
- class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"
- abstract="true">
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
- <!-- admin的dao对象 -->
- <bean id="adminDaoImpl" class="cn.csdn.hr.s2sh.dao.AdminDaoImpl"
- parent="hibernateDaoSupport" />
- </beans>
- 然后我们把上面的三个beans.xml导入到applicationContext.xml中
- <!-- 导入其他的配置文件 -->
- <import resource="beans-dao.xml" />
- <import resource="beans-service.xml" />
- <import resource="beans-action.xml" />
- 7.配置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_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>
- 8.我们来做一个简单的登入,使用的jsp页面为:
- <body>
- <div>
- <form action="/s2sh/csdn/loginAdmin.action" method="get">
- 用户名:<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>
- 访问到的action的处理为:
- package cn.csdn.hr.s2sh.action;
- import cn.csdn.hr.s2sh.domain.Admin;
- import cn.csdn.hr.s2sh.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;
- }
- // 注入
- public void setAdminService(AdminService adminService) {
- Ssh整合开发介绍和简单的登入案例实现
- 一 介绍:
- Ssh是strtus2-2.3.1.2+ spring-2.5.6+hibernate-3.6.8整合的开发,这是目前我的整合开发的使用技术和版本,使用的数据库为mySql。使用的开发工具是eclipse,eplipse的版本为Indigo Service Release 2
- 二 搭建环境
- 1. 首先要先引入struts2和sping和hibernate所需要的包
- (1)struts2的包为:
- struts-2.3.1.2\lib\ struts2-core-2.3.1.2.jar
- struts-2.3.1.2\lib\ognl-3.0.4.jar
- struts-2.3.1.2\lib\ xwork-core-2.3.1.2.jar
- struts-2.3.1.2\lib\ freemarker-2.3.18.jar
- struts-2.3.1.2\lib\commons-fileupload-1.2.2.jar
- struts-2.3.1.2\lib\ commons-io-2.0.1.jar
- struts-2.3.1.2\lib\commons-lang-2.5.jar
- struts-2.3.1.2\lib\commons-logging-1.1.1.jar
- struts-2.3.1.2\lib \struts2-json-plugin-2.3.1.2.jar
- struts-2.3.1.2\lib \struts2-spring-plugin-2.3.1.2.jar
- (2)引入hibernate的包
- hibernate-distribution-3.6.8.Final\lib\required\*.jar 所有的jar包
- hibernate-distribution-3.6.8.Final\lib\jpa\hibernate-jpa-2.0-api-1.0.1.Final.jar
- hibernate-distribution-3.6.8.Final\ hibernate3.jar
- (3)spring的包为:
- spring-framework-2.5.6\dist\ spring.jar
- spring-framework-2.5.6\lib\c3p0\c3p0-0.9.1.2.jar
- spring-framework-2.5.6\lib\aspectj\*.jar
- spring-framework-2.5.6\lib\j2ee\common-annotations.jar
- spring-framework-2.5.6\lib\log4j\log4j-1.2.15.jar
- spring-framework-2.5.6\lib\jakarta-commons\ commons-logging.jar
- spring-framework-2.5.6\lib\cglib\cglib-nodep-2.1_3.jar
- spring-framework-2.5.6\lib\dom4j\dom4j-1.6.1.jar
- 2. 配置spring下所需要的文件
- (1)首先配置spring所需要的xml文件
- 我们在class下,即在src下创建一个applicationContext.xml的xml文件,文件的头文件因为要用到各种标签,所以我们在引入头文件的时候尽量引的比较多点,代码为:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- </beans>
- (2)在xml中配置和数据库相关联,并用c3p0来配置数据库连接池
- <!-- 数据库的连接池 -->
- <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="1234" />
- <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
- <property name="initialPoolSize" value="1" />
- <!--连接池中保留的最小连接数。 -->
- <property name="minPoolSize" value="1" />
- <!--连接池中保留的最大连接数。Default: 15 -->
- <property name="maxPoolSize" value="300" />
- <!--最大空闲时间,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>
- 当然,这样所写的可以在一个properties中读取。读取外部文件在xml中的使用为:
- <!-- 读取外部的文件 -->
- <context:property-placeholder location="jdbc.properties" />
- (3)配置sessionFactory工厂,相当于是hibernate.cfg.xml里面的配置
- <!-- sessionFactory工厂 -->
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <!-- 注入数据源 -->
- <property name="dataSource" ref="dataSource"></property>
- <!-- hibernate映射文件的引入 -->
- <property name="mappingResources">
- <list>
- <value>cn/csdn/hr/s2sh/domain/Admin.hbm.xml</value>
- </list>
- </property>
- <!-- 配置hibernate属性的设置 -->
- <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>
- 3.配置struts2.xml中需要的内容
- (1)配置基本的struts2.xml
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd">
- <struts>
- <!--使用spring创建管理struts2的action操作 -->
- <constant name="struts.objectFactory" value="spring"/>
- <include file="struts-admin.xml"></include>
- </struts>
- 注:其中include是引入的文件,一下的语句是非常重要的:
- <constant name="struts.objectFactory" value="spring"/>,它是struts2和spring的连接的关键
- (2)引入的struts-admin.xml文件,意在检测并作出一个简单的登入的实现
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd">
- <struts>
- <package name="admin" 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>
- 4. Hibernate.cfg.xml中的内容可以省略,它已经在spring中的xml中配置了
- 5.搭建层之间的关系
- (1)首先是domain,包为cn.csdn.hr.s2sh.domain,我们以admin为例
- 属性为id和name和pass
- 封装的实体类为:
- package cn.csdn.hr.s2sh.domain;
- import java.io.Serializable;
- public class Admin implements Serializable {
- private static final long serialVersionUID = 1L;
- private Integer id;
- private String name;
- private String pass;
- public Admin() {
- super();
- // TODO Auto-generated constructor stub
- }
- public Admin(Integer id, String name, String pass) {
- super();
- this.id = id;
- 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 + "]";
- }
- }
- (2)在同一个包下的实体类的映射文件
- <?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.hr.s2sh.domain">
- <class name="Admin" table="admin">
- <id name="id" column="id">
- <generator class="native"></generator>
- </id>
- <property name="name" column="name"></property>
- <property name="pass" column="pass"></property>
- </class>
- </hibernate-mapping>
- (3)创建dao层,创建的包名为cn.csdn.hr.s2sh.dao,创建的接口名为AdminDao,类名为AdminDaoImpl
- 下面是创建的接口 AdminDao.java
- package cn.csdn.hr.s2sh.dao;
- import java.util.List;
- import cn.csdn.hr.s2sh.domain.Admin;
- public interface AdminDao {
- public Admin login(final String name,final String pass);
- }
- 创建的类为AdminDaoImpl.java
- package cn.csdn.hr.s2sh.dao;
- import java.sql.SQLException;
- import java.util.List;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.springframework.orm.hibernate3.HibernateCallback;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import cn.csdn.hr.s2sh.domain.Admin;
- @SuppressWarnings("unchecked")
- public class AdminDaoImpl extends HibernateDaoSupport implements AdminDao {
- public Admin login(final String name, final String pass) {
- return (Admin) this.getHibernateTemplate().execute(
- new HibernateCallback() {
- public Object doInHibernate(Session session)
- throws HibernateException, SQLException {
- // TODO Auto-generated method stub
- Object obj = session
- .createQuery(
- "from Admin where name=:name and pass=:pass")
- .setString("name", name)
- .setString("pass", pass).uniqueResult();
- return obj;
- }
- });
- }
- }
- (4)创建service层,创建的包名为cn.csdn.hr.s2sh.service
- 创建的AdminService.java接口
- package cn.csdn.hr.s2sh.service;
- import cn.csdn.hr.s2sh.dao.AdminDao;
- public interface AdminService extends AdminDao{
- }
- 创建的AdminServiceImpl.java
- package cn.csdn.hr.s2sh.service;
- import java.util.List;
- import org.springframework.transaction.TransactionStatus;
- import org.springframework.transaction.support.TransactionCallback;
- import org.springframework.transaction.support.TransactionTemplate;
- import cn.csdn.hr.s2sh.dao.AdminDao;
- import cn.csdn.hr.s2sh.domain.Admin;
- public class AdminServiceImpl implements AdminService {
- private AdminDao adminDao;
- public void setAdminDao(AdminDao adminDao) {
- this.adminDao = adminDao;
- }
- public Admin login(String name, String pass) {
- // TODO Auto-generated method stub
- return adminDao.login(name, pass);
- }
- }
- 6.创建访问struts2的时候的action,我们把action放到一个bean-action.xml中
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <!-- 创建struts2中的action -->
- <!-- 配置admin的action -->
- <bean id="adminAction" class="cn.csdn.hr.s2sh.action.AdminAction"
- scope="prototype">
- <property name="adminService" ref="adminServiceImpl"></property>
- </bean>
- </beans>
- 而ref的adminServiceImpl是在bean-service.xml中
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-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.hr.s2sh.service.AdminServiceImpl">
- <property name="adminDao" ref="adminDaoImpl"></property>
- </bean>
- </beans>
- 上面的ref是adminDaoImpl,在beans-dao.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <!-- hibernatedao的模板类 HibernateDaoSuppor 抽象类不可以实例化 加上abstract="true" -->
- <bean id="hibernateDaoSupport"
- class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"
- abstract="true">
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
- <!-- admin的dao对象 -->
- <bean id="adminDaoImpl" class="cn.csdn.hr.s2sh.dao.AdminDaoImpl"
- parent="hibernateDaoSupport" />
- </beans>
- 然后我们把上面的三个beans.xml导入到applicationContext.xml中
- <!-- 导入其他的配置文件 -->
- <import resource="beans-dao.xml" />
- <import resource="beans-service.xml" />
- <import resource="beans-action.xml" />
- 7.配置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_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>
- 8.我们来做一个简单的登入,使用的jsp页面为:
- <body>
- <div>
- <form action="/s2sh/csdn/loginAdmin.action" method="get">
- 用户名:<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>
- 访问到的action的处理为:
- package cn.csdn.hr.s2sh.action;
- import cn.csdn.hr.s2sh.domain.Admin;
- import cn.csdn.hr.s2sh.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;
- }
- // 注入
- public void setAdminService(AdminService adminService) {
- this.adminService = adminService;
- }
- public String login() {
- System.out.println("用户名:" + admin.getName());
- admin = adminService.login(admin.getName(), admin.getPass());
- return SUCCESS;
- }
- }
- Ssh整合开发介绍和简单的登入案例实现
- 一 介绍:
- Ssh是strtus2-2.3.1.2+ spring-2.5.6+hibernate-3.6.8整合的开发,这是目前我的整合开发的使用技术和版本,使用的数据库为mySql。使用的开发工具是eclipse,eplipse的版本为Indigo Service Release 2
- 二 搭建环境
- 1. 首先要先引入struts2和sping和hibernate所需要的包
- (1)struts2的包为:
- struts-2.3.1.2\lib\ struts2-core-2.3.1.2.jar
- struts-2.3.1.2\lib\ognl-3.0.4.jar
- struts-2.3.1.2\lib\ xwork-core-2.3.1.2.jar
- struts-2.3.1.2\lib\ freemarker-2.3.18.jar
- struts-2.3.1.2\lib\commons-fileupload-1.2.2.jar
- struts-2.3.1.2\lib\ commons-io-2.0.1.jar
- struts-2.3.1.2\lib\commons-lang-2.5.jar
- struts-2.3.1.2\lib\commons-logging-1.1.1.jar
- struts-2.3.1.2\lib \struts2-json-plugin-2.3.1.2.jar
- struts-2.3.1.2\lib \struts2-spring-plugin-2.3.1.2.jar
- (2)引入hibernate的包
- hibernate-distribution-3.6.8.Final\lib\required\*.jar 所有的jar包
- hibernate-distribution-3.6.8.Final\lib\jpa\hibernate-jpa-2.0-api-1.0.1.Final.jar
- hibernate-distribution-3.6.8.Final\ hibernate3.jar
- (3)spring的包为:
- spring-framework-2.5.6\dist\ spring.jar
- spring-framework-2.5.6\lib\c3p0\c3p0-0.9.1.2.jar
- spring-framework-2.5.6\lib\aspectj\*.jar
- spring-framework-2.5.6\lib\j2ee\common-annotations.jar
- spring-framework-2.5.6\lib\log4j\log4j-1.2.15.jar
- spring-framework-2.5.6\lib\jakarta-commons\ commons-logging.jar
- spring-framework-2.5.6\lib\cglib\cglib-nodep-2.1_3.jar
- spring-framework-2.5.6\lib\dom4j\dom4j-1.6.1.jar
- 2. 配置spring下所需要的文件
- (1)首先配置spring所需要的xml文件
- 我们在class下,即在src下创建一个applicationContext.xml的xml文件,文件的头文件因为要用到各种标签,所以我们在引入头文件的时候尽量引的比较多点,代码为:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- </beans>
- (2)在xml中配置和数据库相关联,并用c3p0来配置数据库连接池
- <!-- 数据库的连接池 -->
- <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="1234" />
- <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
- <property name="initialPoolSize" value="1" />
- <!--连接池中保留的最小连接数。 -->
- <property name="minPoolSize" value="1" />
- <!--连接池中保留的最大连接数。Default: 15 -->
- <property name="maxPoolSize" value="300" />
- <!--最大空闲时间,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>
- 当然,这样所写的可以在一个properties中读取。读取外部文件在xml中的使用为:
- <!-- 读取外部的文件 -->
- <context:property-placeholder location="jdbc.properties" />
- (3)配置sessionFactory工厂,相当于是hibernate.cfg.xml里面的配置
- <!-- sessionFactory工厂 -->
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <!-- 注入数据源 -->
- <property name="dataSource" ref="dataSource"></property>
- <!-- hibernate映射文件的引入 -->
- <property name="mappingResources">
- <list>
- <value>cn/csdn/hr/s2sh/domain/Admin.hbm.xml</value>
- </list>
- </property>
- <!-- 配置hibernate属性的设置 -->
- <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>
- 3.配置struts2.xml中需要的内容
- (1)配置基本的struts2.xml
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd">
- <struts>
- <!--使用spring创建管理struts2的action操作 -->
- <constant name="struts.objectFactory" value="spring"/>
- <include file="struts-admin.xml"></include>
- </struts>
- 注:其中include是引入的文件,一下的语句是非常重要的:
- <constant name="struts.objectFactory" value="spring"/>,它是struts2和spring的连接的关键
- (2)引入的struts-admin.xml文件,意在检测并作出一个简单的登入的实现
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd">
- <struts>
- <package name="admin" 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>
- 4. Hibernate.cfg.xml中的内容可以省略,它已经在spring中的xml中配置了
- 5.搭建层之间的关系
- (1)首先是domain,包为cn.csdn.hr.s2sh.domain,我们以admin为例
- 属性为id和name和pass
- 封装的实体类为:
- package cn.csdn.hr.s2sh.domain;
- import java.io.Serializable;
- public class Admin implements Serializable {
- private static final long serialVersionUID = 1L;
- private Integer id;
- private String name;
- private String pass;
- public Admin() {
- super();
- // TODO Auto-generated constructor stub
- }
- public Admin(Integer id, String name, String pass) {
- super();
- this.id = id;
- 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 + "]";
- }
- }
- (2)在同一个包下的实体类的映射文件
- <?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.hr.s2sh.domain">
- <class name="Admin" table="admin">
- <id name="id" column="id">
- <generator class="native"></generator>
- </id>
- <property name="name" column="name"></property>
- <property name="pass" column="pass"></property>
- </class>
- </hibernate-mapping>
- (3)创建dao层,创建的包名为cn.csdn.hr.s2sh.dao,创建的接口名为AdminDao,类名为AdminDaoImpl
- 下面是创建的接口 AdminDao.java
- package cn.csdn.hr.s2sh.dao;
- import java.util.List;
- import cn.csdn.hr.s2sh.domain.Admin;
- public interface AdminDao {
- public Admin login(final String name,final String pass);
- }
- 创建的类为AdminDaoImpl.java
- package cn.csdn.hr.s2sh.dao;
- import java.sql.SQLException;
- import java.util.List;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.springframework.orm.hibernate3.HibernateCallback;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import cn.csdn.hr.s2sh.domain.Admin;
- @SuppressWarnings("unchecked")
- public class AdminDaoImpl extends HibernateDaoSupport implements AdminDao {
- public Admin login(final String name, final String pass) {
- return (Admin) this.getHibernateTemplate().execute(
- new HibernateCallback() {
- public Object doInHibernate(Session session)
- throws HibernateException, SQLException {
- // TODO Auto-generated method stub
- Object obj = session
- .createQuery(
- "from Admin where name=:name and pass=:pass")
- .setString("name", name)
- .setString("pass", pass).uniqueResult();
- return obj;
- }
- });
- }
- }
- (4)创建service层,创建的包名为cn.csdn.hr.s2sh.service
- 创建的AdminService.java接口
- package cn.csdn.hr.s2sh.service;
- import cn.csdn.hr.s2sh.dao.AdminDao;
- public interface AdminService extends AdminDao{
- }
- 创建的AdminServiceImpl.java
- package cn.csdn.hr.s2sh.service;
- import java.util.List;
- import org.springframework.transaction.TransactionStatus;
- import org.springframework.transaction.support.TransactionCallback;
- import org.springframework.transaction.support.TransactionTemplate;
- import cn.csdn.hr.s2sh.dao.AdminDao;
- import cn.csdn.hr.s2sh.domain.Admin;
- public class AdminServiceImpl implements AdminService {
- private AdminDao adminDao;
- public void setAdminDao(AdminDao adminDao) {
- this.adminDao = adminDao;
- }
- public Admin login(String name, String pass) {
- // TODO Auto-generated method stub
- return adminDao.login(name, pass);
- }
- }
- 6.创建访问struts2的时候的action,我们把action放到一个bean-action.xml中
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <!-- 创建struts2中的action -->
- <!-- 配置admin的action -->
- <bean id="adminAction" class="cn.csdn.hr.s2sh.action.AdminAction"
- scope="prototype">
- <property name="adminService" ref="adminServiceImpl"></property>
- </bean>
- </beans>
- 而ref的adminServiceImpl是在bean-service.xml中
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-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.hr.s2sh.service.AdminServiceImpl">
- <property name="adminDao" ref="adminDaoImpl"></property>
- </bean>
- </beans>
- 上面的ref是adminDaoImpl,在beans-dao.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <!-- hibernatedao的模板类 HibernateDaoSuppor 抽象类不可以实例化 加上abstract="true" -->
- <bean id="hibernateDaoSupport"
- class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"
- abstract="true">
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
- <!-- admin的dao对象 -->
- <bean id="adminDaoImpl" class="cn.csdn.hr.s2sh.dao.AdminDaoImpl"
- parent="hibernateDaoSupport" />
- </beans>
- 然后我们把上面的三个beans.xml导入到applicationContext.xml中
- <!-- 导入其他的配置文件 -->
- <import resource="beans-dao.xml" />
- <import resource="beans-service.xml" />
- <import resource="beans-action.xml" />
- 7.配置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_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>
- 8.我们来做一个简单的登入,使用的jsp页面为:
- <body>
- <div>
- <form action="/s2sh/csdn/loginAdmin.action" method="get">
- 用户名:<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>
- 访问到的action的处理为:
- package cn.csdn.hr.s2sh.action;
- import cn.csdn.hr.s2sh.domain.Admin;
- import cn.csdn.hr.s2sh.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;
- }
- // 注入
- public void setAdminService(AdminService adminService) {
- this.adminService = adminService;
- }
- public String login() {
- System.out.println("用户名:" + admin.getName());
- admin = adminService.login(admin.getName(), admin.getPass());
- return SUCCESS;
- }
- }
- Ssh整合开发介绍和简单的登入案例实现
- 一 介绍:
- Ssh是strtus2-2.3.1.2+ spring-2.5.6+hibernate-3.6.8整合的开发,这是目前我的整合开发的使用技术和版本,使用的数据库为mySql。使用的开发工具是eclipse,eplipse的版本为Indigo Service Release 2
- 二 搭建环境
- 1. 首先要先引入struts2和sping和hibernate所需要的包
- (1)struts2的包为:
- struts-2.3.1.2\lib\ struts2-core-2.3.1.2.jar
- struts-2.3.1.2\lib\ognl-3.0.4.jar
- struts-2.3.1.2\lib\ xwork-core-2.3.1.2.jar
- struts-2.3.1.2\lib\ freemarker-2.3.18.jar
- struts-2.3.1.2\lib\commons-fileupload-1.2.2.jar
- struts-2.3.1.2\lib\ commons-io-2.0.1.jar
- struts-2.3.1.2\lib\commons-lang-2.5.jar
- struts-2.3.1.2\lib\commons-logging-1.1.1.jar
- struts-2.3.1.2\lib \struts2-json-plugin-2.3.1.2.jar
- struts-2.3.1.2\lib \struts2-spring-plugin-2.3.1.2.jar
- (2)引入hibernate的包
- hibernate-distribution-3.6.8.Final\lib\required\*.jar 所有的jar包
- hibernate-distribution-3.6.8.Final\lib\jpa\hibernate-jpa-2.0-api-1.0.1.Final.jar
- hibernate-distribution-3.6.8.Final\ hibernate3.jar
- (3)spring的包为:
- spring-framework-2.5.6\dist\ spring.jar
- spring-framework-2.5.6\lib\c3p0\c3p0-0.9.1.2.jar
- spring-framework-2.5.6\lib\aspectj\*.jar
- spring-framework-2.5.6\lib\j2ee\common-annotations.jar
- spring-framework-2.5.6\lib\log4j\log4j-1.2.15.jar
- spring-framework-2.5.6\lib\jakarta-commons\ commons-logging.jar
- spring-framework-2.5.6\lib\cglib\cglib-nodep-2.1_3.jar
- spring-framework-2.5.6\lib\dom4j\dom4j-1.6.1.jar
- 2. 配置spring下所需要的文件
- (1)首先配置spring所需要的xml文件
- 我们在class下,即在src下创建一个applicationContext.xml的xml文件,文件的头文件因为要用到各种标签,所以我们在引入头文件的时候尽量引的比较多点,代码为:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- </beans>
- (2)在xml中配置和数据库相关联,并用c3p0来配置数据库连接池
- <!-- 数据库的连接池 -->
- <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="1234" />
- <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
- <property name="initialPoolSize" value="1" />
- <!--连接池中保留的最小连接数。 -->
- <property name="minPoolSize" value="1" />
- <!--连接池中保留的最大连接数。Default: 15 -->
- <property name="maxPoolSize" value="300" />
- <!--最大空闲时间,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>
- 当然,这样所写的可以在一个properties中读取。读取外部文件在xml中的使用为:
- <!-- 读取外部的文件 -->
- <context:property-placeholder location="jdbc.properties" />
- (3)配置sessionFactory工厂,相当于是hibernate.cfg.xml里面的配置
- <!-- sessionFactory工厂 -->
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <!-- 注入数据源 -->
- <property name="dataSource" ref="dataSource"></property>
- <!-- hibernate映射文件的引入 -->
- <property name="mappingResources">
- <list>
- <value>cn/csdn/hr/s2sh/domain/Admin.hbm.xml</value>
- </list>
- </property>
- <!-- 配置hibernate属性的设置 -->
- <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>
- 3.配置struts2.xml中需要的内容
- (1)配置基本的struts2.xml
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd">
- <struts>
- <!--使用spring创建管理struts2的action操作 -->
- <constant name="struts.objectFactory" value="spring"/>
- <include file="struts-admin.xml"></include>
- </struts>
- 注:其中include是引入的文件,一下的语句是非常重要的:
- <constant name="struts.objectFactory" value="spring"/>,它是struts2和spring的连接的关键
- (2)引入的struts-admin.xml文件,意在检测并作出一个简单的登入的实现
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd">
- <struts>
- <package name="admin" 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>
- 4. Hibernate.cfg.xml中的内容可以省略,它已经在spring中的xml中配置了
- 5.搭建层之间的关系
- (1)首先是domain,包为cn.csdn.hr.s2sh.domain,我们以admin为例
- 属性为id和name和pass
- 封装的实体类为:
- package cn.csdn.hr.s2sh.domain;
- import java.io.Serializable;
- public class Admin implements Serializable {
- private static final long serialVersionUID = 1L;
- private Integer id;
- private String name;
- private String pass;
- public Admin() {
- super();
- // TODO Auto-generated constructor stub
- }
- public Admin(Integer id, String name, String pass) {
- super();
- this.id = id;
- 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 + "]";
- }
- }
- (2)在同一个包下的实体类的映射文件
- <?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.hr.s2sh.domain">
- <class name="Admin" table="admin">
- <id name="id" column="id">
- <generator class="native"></generator>
- </id>
- <property name="name" column="name"></property>
- <property name="pass" column="pass"></property>
- </class>
- </hibernate-mapping>
- (3)创建dao层,创建的包名为cn.csdn.hr.s2sh.dao,创建的接口名为AdminDao,类名为AdminDaoImpl
- 下面是创建的接口 AdminDao.java
- package cn.csdn.hr.s2sh.dao;
- import java.util.List;
- import cn.csdn.hr.s2sh.domain.Admin;
- public interface AdminDao {
- public Admin login(final String name,final String pass);
- }
- 创建的类为AdminDaoImpl.java
- package cn.csdn.hr.s2sh.dao;
- import java.sql.SQLException;
- import java.util.List;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.springframework.orm.hibernate3.HibernateCallback;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import cn.csdn.hr.s2sh.domain.Admin;
- @SuppressWarnings("unchecked")
- public class AdminDaoImpl extends HibernateDaoSupport implements AdminDao {
- public Admin login(final String name, final String pass) {
- return (Admin) this.getHibernateTemplate().execute(
- new HibernateCallback() {
- public Object doInHibernate(Session session)
- throws HibernateException, SQLException {
- // TODO Auto-generated method stub
- Object obj = session
- .createQuery(
- "from Admin where name=:name and pass=:pass")
- .setString("name", name)
- .setString("pass", pass).uniqueResult();
- return obj;
- }
- });
- }
- }
- (4)创建service层,创建的包名为cn.csdn.hr.s2sh.service
- 创建的AdminService.java接口
- package cn.csdn.hr.s2sh.service;
- import cn.csdn.hr.s2sh.dao.AdminDao;
- public interface AdminService extends AdminDao{
- }
- 创建的AdminServiceImpl.java
- package cn.csdn.hr.s2sh.service;
- import java.util.List;
- import org.springframework.transaction.TransactionStatus;
- import org.springframework.transaction.support.TransactionCallback;
- import org.springframework.transaction.support.TransactionTemplate;
- import cn.csdn.hr.s2sh.dao.AdminDao;
- import cn.csdn.hr.s2sh.domain.Admin;
- public class AdminServiceImpl implements AdminService {
- private AdminDao adminDao;
- public void setAdminDao(AdminDao adminDao) {
- this.adminDao = adminDao;
- }
- public Admin login(String name, String pass) {
- // TODO Auto-generated method stub
- return adminDao.login(name, pass);
- }
- }
- 6.创建访问struts2的时候的action,我们把action放到一个bean-action.xml中
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <!-- 创建struts2中的action -->
- <!-- 配置admin的action -->
- <bean id="adminAction" class="cn.csdn.hr.s2sh.action.AdminAction"
- scope="prototype">
- <property name="adminService" ref="adminServiceImpl"></property>
- </bean>
- </beans>
- 而ref的adminServiceImpl是在bean-service.xml中
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-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.hr.s2sh.service.AdminServiceImpl">
- <property name="adminDao" ref="adminDaoImpl"></property>
- </bean>
- </beans>
- 上面的ref是adminDaoImpl,在beans-dao.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <!-- hibernatedao的模板类 HibernateDaoSuppor 抽象类不可以实例化 加上abstract="true" -->
- <bean id="hibernateDaoSupport"
- class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"
- abstract="true">
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
- <!-- admin的dao对象 -->
- <bean id="adminDaoImpl" class="cn.csdn.hr.s2sh.dao.AdminDaoImpl"
- parent="hibernateDaoSupport" />
- </beans>
- 然后我们把上面的三个beans.xml导入到applicationContext.xml中
- <!-- 导入其他的配置文件 -->
- <import resource="beans-dao.xml" />
- <import resource="beans-service.xml" />
- <import resource="beans-action.xml" />
- 7.配置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_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>
- 8.我们来做一个简单的登入,使用的jsp页面为:
- <body>
- <div>
- <form action="/s2sh/csdn/loginAdmin.action" method="get">
- 用户名:<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>
- 访问到的action的处理为:
- package cn.csdn.hr.s2sh.action;
- import cn.csdn.hr.s2sh.domain.Admin;
- import cn.csdn.hr.s2sh.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;
- }
- // 注入
- public void setAdminService(AdminService adminService) {
- this.adminService = adminService;
- }
- public String login() {
- System.out.println("用户名:" + admin.getName());
- admin = adminService.login(admin.getName(), admin.getPass());
- return SUCCESS;
- }
- }
this.adminService = adminService;
- }
- public String login() {
- System.out.println("用户名:" + admin.getName());
- admin = adminService.login(admin.getName(), admin.getPass());
- return SUCCESS;
- }
- }
【转载】Ssh整合开发介绍和简单的登入案例实现的更多相关文章
- 从MVC和三层架构说到SSH整合开发
相信很多人都认同JavaWeb开发是遵从MVC开发模式的,遵从三层架构进行开发的,是的,大家都这么认同.但是相信大家都会有过这样一个疑问,if(MVC三层模式==三层架构思想)out.println( ...
- SSH整合开发时Scope为默认时现象与原理
1.前提知识 1)scope默认值 进行SSH整合开发时,Struts2的action须要用spring容器进行管理,仅仅要涉及到类以bean的形式入到spring容器中.无论是xml配置还是使用注解 ...
- 从MVC和三层架构说到ssh整合开发-下
这章主要讲整合开发,直接从实战讲起,对与ssh的单方面了解,请继续等待我的兴许文章. 解说不到位的地方欢迎大家指正:联系方式rlovep.com 具体请看源码凝视: 全部代码下载(csdn):链接 G ...
- spring和mybatis的整合开发(基于MapperFactoryBean的整合开发(方便简单不复杂))
MapperFactoryBean是mybati-spring团队提供的一个用于根据mapper接口生成mapper对象的类. 在spring配置文件中可以配置以下参数: 1.mapperInterf ...
- ssh整合开发
ssh整合思想 ssh整合 第一步:导入ssh相关jar包 第二步:搭建struts环境 (1)创建 action struts.xml配置文件, 配置action struts.xml约束 & ...
- SpringBoot整合SpringSecurity简单实现登入登出从零搭建
技术栈 : SpringBoot + SpringSecurity + jpa + freemark ,完整项目地址 : https://github.com/EalenXie/spring-secu ...
- SSH整合开发的web.xml配置
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" ...
- 手游聚合SDK开发之远程开关---渠道登入白名单
白名单有啥好说的呢?无非就是筛选登入,大家第一眼看到就是这个印象,白名单也是有文章的,弄的时机不同会给你带来很不错的收益,注意是收益.还是举例来说,游戏上线前渠道都会做一个预下载,一般提前1-2天,这 ...
- Java窗体简单登入案例(附带源码)
运行截图 源代码下载地址 https://pan.baidu.com/s/1i82Z_onKdOdPFdfGce5e8Q
随机推荐
- windows phone 8 开发系列(二)Hello Wp8!
上篇我们了解了WP8的环境搭建,从今天开始,我们就正式进入WP8的设计,开发阶段. 一. 项目模板介绍 打开vs,选择Windows Phone的项目模板,我们发现如下有很多模板,那么我们就从认识这些 ...
- 常用HTML meta 标签属性(网站兼容与优化需要),meta标签
常用HTML meta 标签属性(网站兼容与优化需要),meta标签 标签提供关于HTML文档的元数据.元数据不会显示在页面上,但是对于机器是可读的.它可用于浏览器(如何显示内容或重新加载页面),搜索 ...
- JQ 选择器大全
一.基本选择器 选择器 描 述 返回 示例 #id 根据给定id匹配一个元素 单个元素 $("#test") 选取id为test的元素 .class 根据给定类名匹配一个元素 集合 ...
- centos问题集锦
一. 为什么新装的centos系统无法使用xshell,putty等工具连接? 原因:sshd服务没有启动. 解决: 1)使用命令rpm -qa | grep ssh查看是否已经安装了ssh 2)使用 ...
- python: 实现sha1小工具
File1: sha1.py File2: sha1.bat ------------------ File1: sha1.py import hashlib import os,sys def Ca ...
- C# CacheHepler Class
internal class CacheHelper { /// <summary> /// Insert value into the cache using /// appropria ...
- sql2012安装过程中出现个一个问题
最近安装了一次SQLSERVER2012,遇到了一个小问题,截图如下: 就是上图中状态为失败的项,点开之后,会弹出下面的一个框: 在网上搜了之后,有了这样的答案: http://www.cnblogs ...
- pyinstall 使用笔记
1.下载pyinstaller 目前pyinstaller支持的python版本为2.3-2.7,可以到http://www.pyinstaller.org/官网下载. 2.安装 下载完成后,解压即可 ...
- 命令行参数的处理函数getopt
命令参数 在linux下, shell命令的参数分两种情况: a.参数需要附加信息, 如"wget http://www.abc.com/1.zip -o 1.zip" b.参数不 ...
- 金融系列5《AUTH过程》
INITIALIZE UPDATE: 在安全通道的显式发起期间,INITIALIZEUPDATE命令用于在卡和主机之间传送卡和会话数据.这个命令开始一个安全通道会话的发起. CPURESET() // ...