首页 > 代码库 > 用户登录注册之搭建ssh框架
用户登录注册之搭建ssh框架
用户登录注册之搭建ssh框架
第一步 导入jar包
第二步 搭建struts2环境
(1)创建action(继承actionnsuport),创建struts.xml配置文件,配置action
public class UserAction extends ActionSupport{}
<?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> <package name="User_test" extends="struts-default" namespace="/"> <action name="user_*" class="com.my.action.UserAction" method="{1}"> </action> </package> </struts>
(2)配置struts2的过滤器,在web.xml中
<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>
第三步 搭建hibernate环境
(1)创建实体类,User
public class User { private Integer uid; private String username; private String password; private String email; private String code; private Boolean state; //再生成set/get方法 }
实体类编写规则
1 实体类里面属性私有的
2 私有属性使用公开的set和get方法操作
3 要求实体类有属性作为唯一值(一般使用id值)
4 实体类属性建议不使用基本数据类型,使用基本数据类型对应的包装类 (1)八个基本数据类型对应的包装类 - int – Integer - char—Character、 - 其他的都是首字母大写 比如 double – Double (2)比如 表示学生的分数,假如 int score; - 比如学生得了0分 ,int score = 0; - 如果表示学生没有参加考试,int score = 0;不能准确表示学生是否参加考试 l 解决:使用包装类可以了, Integer score = 0,表示学生得了0分, l 表示学生没有参加考试,Integer score = null; |
(2)配置实体类和数据库表映射关系,User.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> <class name="com.my.entity.User" table="t_user"> <id name="uid" column="uid"> <generator class="native"></generator> </id> <property name="username" column="username"></property> <property name="password" column="password"></property> <property name="email" column="email"></property> <property name="code" column="code"></property> <property name="state" column="state"></property> </class> </hibernate-mapping>
(3)创建hibernate核心配置文件,hibernate.cfg.xml
- 引入映射配置文件
(配置文件需要引入约束)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 这里没有配置数据库,之后创建spring的时候配置 --> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <mapping resource="com/my/entity/User.hbm.xml"/> </session-factory> </hibernate-configuration>
第四步 搭建spring环境
(1)创建spring核心配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"<!-- IOC相关 --> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<!-- IOC相关 --> xmlns:p="http://www.springframework.org/schema/p"<!-- P名称空间 --> xmlns:context="http://www.springframework.org/schema/context"<!-- IOC注解相关 --> xmlns:aop="http://www.springframework.org/schema/aop"<!-- aop相关 --> xmlns:tx="http://www.springframework.org/schema/tx"<!-- 事务相关 --> xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> </beans>
(2)让spring配置文件在服务器启动时候加载,在web.xml中
- 配置监听器
- 指定spring配置文件位置
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:bean.xml</param-value> </context-param> ...... <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
第五步 struts2和spring整合
(1)把action在spring配置(action多实例的)
(2)在struts.xml中action标签class属性里面写 bean的id值
bean.xml <bean id="userAction" class="com.my.action.UserAction" scope="prototype"> </bean>
struts.xml <action name="user_*" class="userAction" method="{1}">
第六步 spring和hibernate整合
(1)把hibernate核心配置文件中数据库配置,在spring里面配置
(2)把hibernate的sessionFactory在spring配置
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="http://www.mamicode.com/com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="http://www.mamicode.com/jdbc:mysql:///my"></property> <property name="user" value="http://www.mamicode.com/root"></property> <property name="password" value="http://www.mamicode.com/123"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocations" value="http://www.mamicode.com/classpath:hibernate.cfg.xml"></property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:annotation-driven transaction-manager="transactionManager"/>
第七步 完成互相注入关系(在dao里面使用hibernateTemplate)
(1)在dao注入hibernateTemplate对象
(2)在hibernateTemplate对象中注入sessionFactory
public class UserAction extends ActionSupport{ private UserService userService; public void setUserService(UserService userService) { this.userService = userService; } }
@Transactional public class UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } } /** 问题:如果在service类上面没有添加注解,出现异常 问题:只读模式下(FlushMode.NEVER/MANUAL)写操作不被允许:把你的Session改成FlushMode.COMMIT/AUTO或者清除事务定义中的readOnly标记。 错误原因: OpenSessionInViewFilter在getSession的时候,会把获取回来的session的flush mode 设为FlushMode.NEVER。 然后把该sessionFactory绑定到TransactionSynchronizationManager,使request的整个过程都使用同一个session,在请求过后再接除该sessionFactory的绑定, 最后closeSessionIfNecessary根据该session是否已和transaction绑定来决定是否关闭session。 在这个过程中,若HibernateTemplate 发现自当前session有不是readOnly的transaction,就会获取到FlushMode.AUTO Session,使方法拥有写权限。 也即是,如果有不是readOnly的transaction就可以由Flush.NEVER转为Flush.AUTO,拥有insert,update,delete操作权限,如果没有transaction,并且没有另外人为地设flush model的话,则doFilter的整个过程都是Flush.NEVER。 所以受transaction(声明式的事务)保护的方法有写权限,没受保护的则没有。 */
public interface UserDao {}
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {}
<bean id="userAction" class="com.my.action.UserAction" scope="prototype"> <property name="userService" ref="userService"></property> </bean> <bean id="userService" class="com.my.service.UserService"> <property name="userDao" ref="userDaoImpl"></property> </bean> <bean id="userDaoImpl" class="com.my.dao.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 这一部分可以另外创建一个xml文件(比如user.xml),再引入bean.xml中的约束, 最后在bean.xml中引入这个文件<import resource="classpath:user.xml"/> 适合多人开发。 -->
最后结构图:
测试:启动服务器(我试过tomcat和resin),如果没有报错,并且数据库中存在数据表,则说明成功。
注意:数据库必须有,表可以没有;log4j.properties是一个可以显示详细日志信息的配置文件。
补:
在web.xml中在配置如下,原因是防止"no session"问题
<filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
本文出自 “qb的博客” 博客,谢绝转载!
用户登录注册之搭建ssh框架