SSH框架---简单整合登录注册

1.这里用的是struts2+spring2+hibernate3.1。用到user表 
2.请确定jar包不要重复。 
3.用到了struts2的validation框架。spring的配置文件applicationContext.xml,如果启动fwq发现无法加载此配置,请放置到WEB-INF下。hibernate通过映射关联表格,和字段。如果出现不能为空的错误,请在user.hbm.xml中检查字段属性和配置属性有否冲突。比如:
  1. <property name="registime" type="timestamp">  
  2.             <column name="registime" length="0" not-null="false" />  
  3.         </property>  
[XML]  view plain copy
  1. <property name="registime" type="timestamp">  
  2.             <column name="registime" length="0" not-null="false" />  
  3.         </property>  
[XML]  view plain  copy
  1. <property name="registime" type="timestamp">  
  2.             <column name="registime" length="0" not-null="false" />  
  3.         </property>  

时间輚不能为空的。 
从数据库开始说起。 
一,设计方式接口——实现类(这里用的是老紫竹留言本的数据库部分代码) 
接口实现如干方法,实现类实现接口中的所有方法。 
二,
  1. <hibernate-configuration>  
  2.   
  3. <session-factory>  
  4.     <property name="show_sql">true</property>  
  5.     <property name="myeclipse.connection.profile">Myway</property>  
  6.     <property name="connection.url">  
  7.         jdbc:mysql://localhost:3306/myway   
  8.     </property>  
  9.     <property name="connection.username">root</property>  
  10.     <property name="connection.password">1</property>  
  11.     <property name="connection.driver_class">  
  12.         com.mysql.jdbc.Driver   
  13.     </property>  
  14.     <property name="dialect">  
  15.         org.hibernate.dialect.MySQLDialect   
  16.     </property>  
  17.     <property name="current_session_context_class">thread</property>  
  18.     <mapping resource="myway/po/User.hbm.xml" />  
  19.   
  20. </session-factory>  
  21.   
  22. </hibernate-configuration>  
[XML]  view plain copy
  1. <hibernate-configuration>  
  2.   
  3. <session-factory>  
  4.     <property name="show_sql">true</property>  
  5.     <property name="myeclipse.connection.profile">Myway</property>  
  6.     <property name="connection.url">  
  7.         jdbc:mysql://localhost:3306/myway  
  8.     </property>  
  9.     <property name="connection.username">root</property>  
  10.     <property name="connection.password">1</property>  
  11.     <property name="connection.driver_class">  
  12.         com.mysql.jdbc.Driver  
  13.     </property>  
  14.     <property name="dialect">  
  15.         org.hibernate.dialect.MySQLDialect  
  16.     </property>  
  17.     <property name="current_session_context_class">thread</property>  
  18.     <mapping resource="myway/po/User.hbm.xml" />  
  19.   
  20. </session-factory>  
  21.   
  22. </hibernate-configuration>  
[XML]  view plain  copy
  1. <hibernate-configuration>  
  2.   
  3. <session-factory>  
  4.     <property name="show_sql">true</property>  
  5.     <property name="myeclipse.connection.profile">Myway</property>  
  6.     <property name="connection.url">  
  7.         jdbc:mysql://localhost:3306/myway  
  8.     </property>  
  9.     <property name="connection.username">root</property>  
  10.     <property name="connection.password">1</property>  
  11.     <property name="connection.driver_class">  
  12.         com.mysql.jdbc.Driver  
  13.     </property>  
  14.     <property name="dialect">  
  15.         org.hibernate.dialect.MySQLDialect  
  16.     </property>  
  17.     <property name="current_session_context_class">thread</property>  
  18.     <mapping resource="myway/po/User.hbm.xml" />  
  19.   
  20. </session-factory>  
  21.   
  22. </hibernate-configuration>  

以上是hibernate.cfg.xml的配置 
  1. <property name="current_session_context_class">thread</property>  
[XML]  view plain copy
  1. <property name="current_session_context_class">thread</property>  
[XML]  view plain  copy
  1. <property name="current_session_context_class">thread</property>  

这句必须有。不然会报错。如果不加上对应的是实现类中的错误。 
三,表的映射关系 
类似javabean的一个类文件,属性对应表中的字段,一个实例对应一条记录。 

  1. <hibernate-mapping>  
  2.     <class name="myway.po.User" table="user" catalog="myway">  
  3.         <id name="id" type="integer">  
  4.             <column name="id" />  
  5.             <generator class="assigned"></generator>  
  6.         </id>  
  7.         <property name="userid" type="string">  
  8.             <column name="userid" length="50" not-null="true" />  
  9.         </property>  
  10.         <property name="userpwd" type="string">  
  11.             <column name="userpwd" length="50" />  
  12.         </property>  
  13.         <property name="userques" type="string">  
  14.             <column name="userques" length="50" />  
  15.         </property>  
  16.         <property name="userans" type="string">  
  17.             <column name="userans" length="50" />  
  18.         </property>  
  19.         <property name="usermail" type="string">  
  20.             <column name="usermail" length="50" />  
  21.         </property>  
  22.         <property name="integral" type="integer">  
  23.             <column name="integral" />  
  24.         </property>  
  25.         <property name="grade" type="integer">  
  26.             <column name="grade" />  
  27.         </property>  
  28.         <property name="sex" type="string">  
  29.             <column name="sex" length="8" />  
  30.         </property>  
  31.         <property name="schoolname" type="string">  
  32.             <column name="schoolname" length="50" />  
  33.         </property>  
  34.         <property name="birthday" type="string">  
  35.             <column name="birthday" length="50" />  
  36.         </property>  
  37.         <property name="realname" type="string">  
  38.             <column name="realname" length="50" />  
  39.         </property>  
  40.         <property name="nicheng" type="string">  
  41.             <column name="nicheng" length="50" />  
  42.         </property>  
  43.         <property name="personalhobby" type="string">  
  44.             <column name="personalhobby" length="50" />  
  45.         </property>  
  46.         <property name="otherinfo" type="string">  
  47.             <column name="otherinfo" length="200" />  
  48.         </property>  
  49.         <property name="registime" type="timestamp">  
  50.             <column name="registime" length="0" not-null="false" />  
  51.         </property>  
  52.     </class>  
  53. </hibernate-mapping>  
[XML]  view plain copy
  1. <hibernate-mapping>  
  2.     <class name="myway.po.User" table="user" catalog="myway">  
  3.         <id name="id" type="integer">  
  4.             <column name="id" />  
  5.             <generator class="assigned"></generator>  
  6.         </id>  
  7.         <property name="userid" type="string">  
  8.             <column name="userid" length="50" not-null="true" />  
  9.         </property>  
  10.         <property name="userpwd" type="string">  
  11.             <column name="userpwd" length="50" />  
  12.         </property>  
  13.         <property name="userques" type="string">  
  14.             <column name="userques" length="50" />  
  15.         </property>  
  16.         <property name="userans" type="string">  
  17.             <column name="userans" length="50" />  
  18.         </property>  
  19.         <property name="usermail" type="string">  
  20.             <column name="usermail" length="50" />  
  21.         </property>  
  22.         <property name="integral" type="integer">  
  23.             <column name="integral" />  
  24.         </property>  
  25.         <property name="grade" type="integer">  
  26.             <column name="grade" />  
  27.         </property>  
  28.         <property name="sex" type="string">  
  29.             <column name="sex" length="8" />  
  30.         </property>  
  31.         <property name="schoolname" type="string">  
  32.             <column name="schoolname" length="50" />  
  33.         </property>  
  34.         <property name="birthday" type="string">  
  35.             <column name="birthday" length="50" />  
  36.         </property>  
  37.         <property name="realname" type="string">  
  38.             <column name="realname" length="50" />  
  39.         </property>  
  40.         <property name="nicheng" type="string">  
  41.             <column name="nicheng" length="50" />  
  42.         </property>  
  43.         <property name="personalhobby" type="string">  
  44.             <column name="personalhobby" length="50" />  
  45.         </property>  
  46.         <property name="otherinfo" type="string">  
  47.             <column name="otherinfo" length="200" />  
  48.         </property>  
  49.         <property name="registime" type="timestamp">  
  50.             <column name="registime" length="0" not-null="false" />  
  51.         </property>  
  52.     </class>  
  53. </hibernate-mapping>  
[XML]  view plain  copy
  1. <hibernate-mapping>  
  2.     <class name="myway.po.User" table="user" catalog="myway">  
  3.         <id name="id" type="integer">  
  4.             <column name="id" />  
  5.             <generator class="assigned"></generator>  
  6.         </id>  
  7.         <property name="userid" type="string">  
  8.             <column name="userid" length="50" not-null="true" />  
  9.         </property>  
  10.         <property name="userpwd" type="string">  
  11.             <column name="userpwd" length="50" />  
  12.         </property>  
  13.         <property name="userques" type="string">  
  14.             <column name="userques" length="50" />  
  15.         </property>  
  16.         <property name="userans" type="string">  
  17.             <column name="userans" length="50" />  
  18.         </property>  
  19.         <property name="usermail" type="string">  
  20.             <column name="usermail" length="50" />  
  21.         </property>  
  22.         <property name="integral" type="integer">  
  23.             <column name="integral" />  
  24.         </property>  
  25.         <property name="grade" type="integer">  
  26.             <column name="grade" />  
  27.         </property>  
  28.         <property name="sex" type="string">  
  29.             <column name="sex" length="8" />  
  30.         </property>  
  31.         <property name="schoolname" type="string">  
  32.             <column name="schoolname" length="50" />  
  33.         </property>  
  34.         <property name="birthday" type="string">  
  35.             <column name="birthday" length="50" />  
  36.         </property>  
  37.         <property name="realname" type="string">  
  38.             <column name="realname" length="50" />  
  39.         </property>  
  40.         <property name="nicheng" type="string">  
  41.             <column name="nicheng" length="50" />  
  42.         </property>  
  43.         <property name="personalhobby" type="string">  
  44.             <column name="personalhobby" length="50" />  
  45.         </property>  
  46.         <property name="otherinfo" type="string">  
  47.             <column name="otherinfo" length="200" />  
  48.         </property>  
  49.         <property name="registime" type="timestamp">  
  50.             <column name="registime" length="0" not-null="false" />  
  51.         </property>  
  52.     </class>  
  53. </hibernate-mapping>  

以上是表的xml文件 
(对数据库的操作通过po来实现) 
下面是struts2部分。 
一,
  1. <struts>  
  2. <constant name="struts.custom.i18n.resources" value="globalMessages"/>  
  3.   
  4. <constant name="struts.i18n.encodeing" value="UTF-8"/>  
  5.   
  6. <package name="Myway" extends="struts-default">  
  7.     <action name="zjreg" class="zjregisterAction">  
  8.         <result name="success" type="redirect" >/jsp/zjregisterinfo.jsp</result>  
  9.         <result name="input">/jsp/zjregister.jsp</result>  
  10.     </action>  
  11.        
  12.     <action name="login" class="loginAction">  
  13.         <result name="success" type="redirect">/my_foot.jsp</result>  
  14.         <result name="input"  type="redirect">/jsp/login.jsp</result>  
  15.        
  16.     </action>  
  17. </struts>  
[XML]  view plain copy
  1. <struts>  
  2. <constant name="struts.custom.i18n.resources" value="globalMessages"/>  
  3.   
  4. <constant name="struts.i18n.encodeing" value="UTF-8"/>  
  5.   
  6. <package name="Myway" extends="struts-default">  
  7.     <action name="zjreg" class="zjregisterAction">  
  8.         <result name="success" type="redirect" >/jsp/zjregisterinfo.jsp</result>  
  9.         <result name="input">/jsp/zjregister.jsp</result>  
  10.     </action>  
  11.       
  12.     <action name="login" class="loginAction">  
  13.         <result name="success" type="redirect">/my_foot.jsp</result>  
  14.         <result name="input"  type="redirect">/jsp/login.jsp</result>  
  15.       
  16.     </action>  
  17. </struts>  
[XML]  view plain  copy
  1. <struts>  
  2. <constant name="struts.custom.i18n.resources" value="globalMessages"/>  
  3.   
  4. <constant name="struts.i18n.encodeing" value="UTF-8"/>  
  5.   
  6. <package name="Myway" extends="struts-default">  
  7.     <action name="zjreg" class="zjregisterAction">  
  8.         <result name="success" type="redirect" >/jsp/zjregisterinfo.jsp</result>  
  9.         <result name="input">/jsp/zjregister.jsp</result>  
  10.     </action>  
  11.       
  12.     <action name="login" class="loginAction">  
  13.         <result name="success" type="redirect">/my_foot.jsp</result>  
  14.         <result name="input"  type="redirect">/jsp/login.jsp</result>  
  15.       
  16.     </action>  
  17. </struts>  

以上是struts.xml的部分配置(有于整合的spring本来action 后面的class是逻辑处理类的class文件,现在只是一个表示,这个表示用于spring识别这个action) 
二,在action中通过提供一个上面的数据库操作接口的get方法,让spring调用的 
  1. private UserService userservice;   
  2.        
  3.        
  4.     public void setUserservice(UserService userservice) {   
  5.         this.userservice = userservice;   
  6.     }  
[Java]  view plain copy
  1. private UserService userservice;  
  2.       
  3.       
  4.     public void setUserservice(UserService userservice) {  
  5.         this.userservice = userservice;  
  6.     }  
[Java]  view plain  copy
  1. private UserService userservice;  
  2.       
  3.       
  4.     public void setUserservice(UserService userservice) {  
  5.         this.userservice = userservice;  
  6.     }  

类似这个样子。 

action负责通过接口对象调用实现类完成登陆和注册功能,返回表示success,和input来转到不通的视图。 

三,validation.xml更具不通的action类名字,来提供效验文件,这里提供的是LoginAction-validation.xml和ZjregisterAction-validation.xml 
  1. <validators>  
  2.     <field name="username">  
  3.         <field-validator type="requiredstring">  
  4.             <param name="trim">true</param>  
  5.             <message>username should be write</message>  
  6.         </field-validator>  
  7.     </field>  
  8.   
  9.     <field name="username">  
  10.         <field-validator type="regex">  
  11.             <param name="trim">ture</param>  
  12.             <param name="expression"><![CDATA[(/w{6,15})]]></param>  
  13.             <message>username must between 6 and 15 and also </message>  
  14.         </field-validator>  
  15.     </field>  
  16.        
  17.         <field name="password">  
  18.         <field-validator type="requiredstring">  
  19.             <param name="trim">true</param>  
  20.             <message>password should be write</message>  
  21.         </field-validator>  
  22.     </field>  
  23.        
  24.     <field name="password">  
  25.         <field-validator type="regex">  
  26.             <param name="trim">ture</param>  
  27.             <param name="expression"><![CDATA[(/w{6,15})]]></param>  
  28.             <message>password must between 6 and 15 and also </message>  
  29.         </field-validator>  
  30.     </field>  
  31. </validators>  
[XML]  view plain copy
  1. <validators>  
  2.     <field name="username">  
  3.         <field-validator type="requiredstring">  
  4.             <param name="trim">true</param>  
  5.             <message>username should be write</message>  
  6.         </field-validator>  
  7.     </field>  
  8.   
  9.     <field name="username">  
  10.         <field-validator type="regex">  
  11.             <param name="trim">ture</param>  
  12.             <param name="expression"><![CDATA[(/w{6,15})]]></param>  
  13.             <message>username must between 6 and 15 and also </message>  
  14.         </field-validator>  
  15.     </field>  
  16.       
  17.         <field name="password">  
  18.         <field-validator type="requiredstring">  
  19.             <param name="trim">true</param>  
  20.             <message>password should be write</message>  
  21.         </field-validator>  
  22.     </field>  
  23.       
  24.     <field name="password">  
  25.         <field-validator type="regex">  
  26.             <param name="trim">ture</param>  
  27.             <param name="expression"><![CDATA[(/w{6,15})]]></param>  
  28.             <message>password must between 6 and 15 and also </message>  
  29.         </field-validator>  
  30.     </field>  
  31. </validators>  
[XML]  view plain  copy
  1. <validators>  
  2.     <field name="username">  
  3.         <field-validator type="requiredstring">  
  4.             <param name="trim">true</param>  
  5.             <message>username should be write</message>  
  6.         </field-validator>  
  7.     </field>  
  8.   
  9.     <field name="username">  
  10.         <field-validator type="regex">  
  11.             <param name="trim">ture</param>  
  12.             <param name="expression"><![CDATA[(/w{6,15})]]></param>  
  13.             <message>username must between 6 and 15 and also </message>  
  14.         </field-validator>  
  15.     </field>  
  16.       
  17.         <field name="password">  
  18.         <field-validator type="requiredstring">  
  19.             <param name="trim">true</param>  
  20.             <message>password should be write</message>  
  21.         </field-validator>  
  22.     </field>  
  23.       
  24.     <field name="password">  
  25.         <field-validator type="regex">  
  26.             <param name="trim">ture</param>  
  27.             <param name="expression"><![CDATA[(/w{6,15})]]></param>  
  28.             <message>password must between 6 and 15 and also </message>  
  29.         </field-validator>  
  30.     </field>  
  31. </validators>  

部分效验文件,虽然有这个文件,但strtus2还是会去访问action中的validate方法的。 

spring部分 
一,
  1. <bean id="userservice" class="myway.userinterface.imp.UserServiceHbimp"></bean>  
  2. <bean id="loginAction" class="myway.action.LoginAction" scope="prototype">  
  3.     <property name="userservice">  
  4.         <ref local="userservice"/>  
  5.     </property>  
  6. </bean>  
  7. <bean id="zjregisterAction" class="myway.action.ZjregAction" scope="prototype">  
  8.     <property name="userservice">  
  9.         <ref local="userservice"/>  
  10.     </property>  
  11. </bean>  
  12. </beans>  
[XML]  view plain copy
  1. <bean id="userservice" class="myway.userinterface.imp.UserServiceHbimp"></bean>  
  2. <bean id="loginAction" class="myway.action.LoginAction" scope="prototype">  
  3.     <property name="userservice">  
  4.         <ref local="userservice"/>  
  5.     </property>  
  6. </bean>  
  7. <bean id="zjregisterAction" class="myway.action.ZjregAction" scope="prototype">  
  8.     <property name="userservice">  
  9.         <ref local="userservice"/>  
  10.     </property>  
  11. </bean>  
  12. </beans>  
[XML]  view plain  copy
  1. <bean id="userservice" class="myway.userinterface.imp.UserServiceHbimp"></bean>  
  2. <bean id="loginAction" class="myway.action.LoginAction" scope="prototype">  
  3.     <property name="userservice">  
  4.         <ref local="userservice"/>  
  5.     </property>  
  6. </bean>  
  7. <bean id="zjregisterAction" class="myway.action.ZjregAction" scope="prototype">  
  8.     <property name="userservice">  
  9.         <ref local="userservice"/>  
  10.     </property>  
  11. </bean>  
  12. </beans>  
1.这里用的是struts2+spring2+hibernate3.1。用到user表 
2.请确定jar包不要重复。 
3.用到了struts2的validation框架。spring的配置文件applicationContext.xml,如果启动fwq发现无法加载此配置,请放置到WEB-INF下。hibernate通过映射关联表格,和字段。如果出现不能为空的错误,请在user.hbm.xml中检查字段属性和配置属性有否冲突。比如:
  1. <property name="registime" type="timestamp">  
  2.             <column name="registime" length="0" not-null="false" />  
  3.         </property>  
[XML]  view plain copy
  1. <property name="registime" type="timestamp">  
  2.             <column name="registime" length="0" not-null="false" />  
  3.         </property>  
[XML]  view plain  copy
  1. <property name="registime" type="timestamp">  
  2.             <column name="registime" length="0" not-null="false" />  
  3.         </property>  

时间輚不能为空的。 
从数据库开始说起。 
一,设计方式接口——实现类(这里用的是老紫竹留言本的数据库部分代码) 
接口实现如干方法,实现类实现接口中的所有方法。 
二,
  1. <hibernate-configuration>  
  2.   
  3. <session-factory>  
  4.     <property name="show_sql">true</property>  
  5.     <property name="myeclipse.connection.profile">Myway</property>  
  6.     <property name="connection.url">  
  7.         jdbc:mysql://localhost:3306/myway   
  8.     </property>  
  9.     <property name="connection.username">root</property>  
  10.     <property name="connection.password">1</property>  
  11.     <property name="connection.driver_class">  
  12.         com.mysql.jdbc.Driver   
  13.     </property>  
  14.     <property name="dialect">  
  15.         org.hibernate.dialect.MySQLDialect   
  16.     </property>  
  17.     <property name="current_session_context_class">thread</property>  
  18.     <mapping resource="myway/po/User.hbm.xml" />  
  19.   
  20. </session-factory>  
  21.   
  22. </hibernate-configuration>  
[XML]  view plain copy
  1. <hibernate-configuration>  
  2.   
  3. <session-factory>  
  4.     <property name="show_sql">true</property>  
  5.     <property name="myeclipse.connection.profile">Myway</property>  
  6.     <property name="connection.url">  
  7.         jdbc:mysql://localhost:3306/myway  
  8.     </property>  
  9.     <property name="connection.username">root</property>  
  10.     <property name="connection.password">1</property>  
  11.     <property name="connection.driver_class">  
  12.         com.mysql.jdbc.Driver  
  13.     </property>  
  14.     <property name="dialect">  
  15.         org.hibernate.dialect.MySQLDialect  
  16.     </property>  
  17.     <property name="current_session_context_class">thread</property>  
  18.     <mapping resource="myway/po/User.hbm.xml" />  
  19.   
  20. </session-factory>  
  21.   
  22. </hibernate-configuration>  
[XML]  view plain  copy
  1. <hibernate-configuration>  
  2.   
  3. <session-factory>  
  4.     <property name="show_sql">true</property>  
  5.     <property name="myeclipse.connection.profile">Myway</property>  
  6.     <property name="connection.url">  
  7.         jdbc:mysql://localhost:3306/myway  
  8.     </property>  
  9.     <property name="connection.username">root</property>  
  10.     <property name="connection.password">1</property>  
  11.     <property name="connection.driver_class">  
  12.         com.mysql.jdbc.Driver  
  13.     </property>  
  14.     <property name="dialect">  
  15.         org.hibernate.dialect.MySQLDialect  
  16.     </property>  
  17.     <property name="current_session_context_class">thread</property>  
  18.     <mapping resource="myway/po/User.hbm.xml" />  
  19.   
  20. </session-factory>  
  21.   
  22. </hibernate-configuration>  

以上是hibernate.cfg.xml的配置 
  1. <property name="current_session_context_class">thread</property>  
[XML]  view plain copy
  1. <property name="current_session_context_class">thread</property>  
[XML]  view plain  copy
  1. <property name="current_session_context_class">thread</property>  

这句必须有。不然会报错。如果不加上对应的是实现类中的错误。 
三,表的映射关系 
类似javabean的一个类文件,属性对应表中的字段,一个实例对应一条记录。 

  1. <hibernate-mapping>  
  2.     <class name="myway.po.User" table="user" catalog="myway">  
  3.         <id name="id" type="integer">  
  4.             <column name="id" />  
  5.             <generator class="assigned"></generator>  
  6.         </id>  
  7.         <property name="userid" type="string">  
  8.             <column name="userid" length="50" not-null="true" />  
  9.         </property>  
  10.         <property name="userpwd" type="string">  
  11.             <column name="userpwd" length="50" />  
  12.         </property>  
  13.         <property name="userques" type="string">  
  14.             <column name="userques" length="50" />  
  15.         </property>  
  16.         <property name="userans" type="string">  
  17.             <column name="userans" length="50" />  
  18.         </property>  
  19.         <property name="usermail" type="string">  
  20.             <column name="usermail" length="50" />  
  21.         </property>  
  22.         <property name="integral" type="integer">  
  23.             <column name="integral" />  
  24.         </property>  
  25.         <property name="grade" type="integer">  
  26.             <column name="grade" />  
  27.         </property>  
  28.         <property name="sex" type="string">  
  29.             <column name="sex" length="8" />  
  30.         </property>  
  31.         <property name="schoolname" type="string">  
  32.             <column name="schoolname" length="50" />  
  33.         </property>  
  34.         <property name="birthday" type="string">  
  35.             <column name="birthday" length="50" />  
  36.         </property>  
  37.         <property name="realname" type="string">  
  38.             <column name="realname" length="50" />  
  39.         </property>  
  40.         <property name="nicheng" type="string">  
  41.             <column name="nicheng" length="50" />  
  42.         </property>  
  43.         <property name="personalhobby" type="string">  
  44.             <column name="personalhobby" length="50" />  
  45.         </property>  
  46.         <property name="otherinfo" type="string">  
  47.             <column name="otherinfo" length="200" />  
  48.         </property>  
  49.         <property name="registime" type="timestamp">  
  50.             <column name="registime" length="0" not-null="false" />  
  51.         </property>  
  52.     </class>  
  53. </hibernate-mapping>  
[XML]  view plain copy
  1. <hibernate-mapping>  
  2.     <class name="myway.po.User" table="user" catalog="myway">  
  3.         <id name="id" type="integer">  
  4.             <column name="id" />  
  5.             <generator class="assigned"></generator>  
  6.         </id>  
  7.         <property name="userid" type="string">  
  8.             <column name="userid" length="50" not-null="true" />  
  9.         </property>  
  10.         <property name="userpwd" type="string">  
  11.             <column name="userpwd" length="50" />  
  12.         </property>  
  13.         <property name="userques" type="string">  
  14.             <column name="userques" length="50" />  
  15.         </property>  
  16.         <property name="userans" type="string">  
  17.             <column name="userans" length="50" />  
  18.         </property>  
  19.         <property name="usermail" type="string">  
  20.             <column name="usermail" length="50" />  
  21.         </property>  
  22.         <property name="integral" type="integer">  
  23.             <column name="integral" />  
  24.         </property>  
  25.         <property name="grade" type="integer">  
  26.             <column name="grade" />  
  27.         </property>  
  28.         <property name="sex" type="string">  
  29.             <column name="sex" length="8" />  
  30.         </property>  
  31.         <property name="schoolname" type="string">  
  32.             <column name="schoolname" length="50" />  
  33.         </property>  
  34.         <property name="birthday" type="string">  
  35.             <column name="birthday" length="50" />  
  36.         </property>  
  37.         <property name="realname" type="string">  
  38.             <column name="realname" length="50" />  
  39.         </property>  
  40.         <property name="nicheng" type="string">  
  41.             <column name="nicheng" length="50" />  
  42.         </property>  
  43.         <property name="personalhobby" type="string">  
  44.             <column name="personalhobby" length="50" />  
  45.         </property>  
  46.         <property name="otherinfo" type="string">  
  47.             <column name="otherinfo" length="200" />  
  48.         </property>  
  49.         <property name="registime" type="timestamp">  
  50.             <column name="registime" length="0" not-null="false" />  
  51.         </property>  
  52.     </class>  
  53. </hibernate-mapping>  
[XML]  view plain  copy
  1. <hibernate-mapping>  
  2.     <class name="myway.po.User" table="user" catalog="myway">  
  3.         <id name="id" type="integer">  
  4.             <column name="id" />  
  5.             <generator class="assigned"></generator>  
  6.         </id>  
  7.         <property name="userid" type="string">  
  8.             <column name="userid" length="50" not-null="true" />  
  9.         </property>  
  10.         <property name="userpwd" type="string">  
  11.             <column name="userpwd" length="50" />  
  12.         </property>  
  13.         <property name="userques" type="string">  
  14.             <column name="userques" length="50" />  
  15.         </property>  
  16.         <property name="userans" type="string">  
  17.             <column name="userans" length="50" />  
  18.         </property>  
  19.         <property name="usermail" type="string">  
  20.             <column name="usermail" length="50" />  
  21.         </property>  
  22.         <property name="integral" type="integer">  
  23.             <column name="integral" />  
  24.         </property>  
  25.         <property name="grade" type="integer">  
  26.             <column name="grade" />  
  27.         </property>  
  28.         <property name="sex" type="string">  
  29.             <column name="sex" length="8" />  
  30.         </property>  
  31.         <property name="schoolname" type="string">  
  32.             <column name="schoolname" length="50" />  
  33.         </property>  
  34.         <property name="birthday" type="string">  
  35.             <column name="birthday" length="50" />  
  36.         </property>  
  37.         <property name="realname" type="string">  
  38.             <column name="realname" length="50" />  
  39.         </property>  
  40.         <property name="nicheng" type="string">  
  41.             <column name="nicheng" length="50" />  
  42.         </property>  
  43.         <property name="personalhobby" type="string">  
  44.             <column name="personalhobby" length="50" />  
  45.         </property>  
  46.         <property name="otherinfo" type="string">  
  47.             <column name="otherinfo" length="200" />  
  48.         </property>  
  49.         <property name="registime" type="timestamp">  
  50.             <column name="registime" length="0" not-null="false" />  
  51.         </property>  
  52.     </class>  
  53. </hibernate-mapping>  

以上是表的xml文件 
(对数据库的操作通过po来实现) 
下面是struts2部分。 
一,
  1. <struts>  
  2. <constant name="struts.custom.i18n.resources" value="globalMessages"/>  
  3.   
  4. <constant name="struts.i18n.encodeing" value="UTF-8"/>  
  5.   
  6. <package name="Myway" extends="struts-default">  
  7.     <action name="zjreg" class="zjregisterAction">  
  8.         <result name="success" type="redirect" >/jsp/zjregisterinfo.jsp</result>  
  9.         <result name="input">/jsp/zjregister.jsp</result>  
  10.     </action>  
  11.        
  12.     <action name="login" class="loginAction">  
  13.         <result name="success" type="redirect">/my_foot.jsp</result>  
  14.         <result name="input"  type="redirect">/jsp/login.jsp</result>  
  15.        
  16.     </action>  
  17. </struts>  
[XML]  view plain copy
  1. <struts>  
  2. <constant name="struts.custom.i18n.resources" value="globalMessages"/>  
  3.   
  4. <constant name="struts.i18n.encodeing" value="UTF-8"/>  
  5.   
  6. <package name="Myway" extends="struts-default">  
  7.     <action name="zjreg" class="zjregisterAction">  
  8.         <result name="success" type="redirect" >/jsp/zjregisterinfo.jsp</result>  
  9.         <result name="input">/jsp/zjregister.jsp</result>  
  10.     </action>  
  11.       
  12.     <action name="login" class="loginAction">  
  13.         <result name="success" type="redirect">/my_foot.jsp</result>  
  14.         <result name="input"  type="redirect">/jsp/login.jsp</result>  
  15.       
  16.     </action>  
  17. </struts>  
[XML]  view plain  copy
  1. <struts>  
  2. <constant name="struts.custom.i18n.resources" value="globalMessages"/>  
  3.   
  4. <constant name="struts.i18n.encodeing" value="UTF-8"/>  
  5.   
  6. <package name="Myway" extends="struts-default">  
  7.     <action name="zjreg" class="zjregisterAction">  
  8.         <result name="success" type="redirect" >/jsp/zjregisterinfo.jsp</result>  
  9.         <result name="input">/jsp/zjregister.jsp</result>  
  10.     </action>  
  11.       
  12.     <action name="login" class="loginAction">  
  13.         <result name="success" type="redirect">/my_foot.jsp</result>  
  14.         <result name="input"  type="redirect">/jsp/login.jsp</result>  
  15.       
  16.     </action>  
  17. </struts>  

以上是struts.xml的部分配置(有于整合的spring本来action 后面的class是逻辑处理类的class文件,现在只是一个表示,这个表示用于spring识别这个action) 
二,在action中通过提供一个上面的数据库操作接口的get方法,让spring调用的 
  1. private UserService userservice;   
  2.        
  3.        
  4.     public void setUserservice(UserService userservice) {   
  5.         this.userservice = userservice;   
  6.     }  
[Java]  view plain copy
  1. private UserService userservice;  
  2.       
  3.       
  4.     public void setUserservice(UserService userservice) {  
  5.         this.userservice = userservice;  
  6.     }  
[Java]  view plain  copy
  1. private UserService userservice;  
  2.       
  3.       
  4.     public void setUserservice(UserService userservice) {  
  5.         this.userservice = userservice;  
  6.     }  

类似这个样子。 

action负责通过接口对象调用实现类完成登陆和注册功能,返回表示success,和input来转到不通的视图。 

三,validation.xml更具不通的action类名字,来提供效验文件,这里提供的是LoginAction-validation.xml和ZjregisterAction-validation.xml 
  1. <validators>  
  2.     <field name="username">  
  3.         <field-validator type="requiredstring">  
  4.             <param name="trim">true</param>  
  5.             <message>username should be write</message>  
  6.         </field-validator>  
  7.     </field>  
  8.   
  9.     <field name="username">  
  10.         <field-validator type="regex">  
  11.             <param name="trim">ture</param>  
  12.             <param name="expression"><![CDATA[(/w{6,15})]]></param>  
  13.             <message>username must between 6 and 15 and also </message>  
  14.         </field-validator>  
  15.     </field>  
  16.        
  17.         <field name="password">  
  18.         <field-validator type="requiredstring">  
  19.             <param name="trim">true</param>  
  20.             <message>password should be write</message>  
  21.         </field-validator>  
  22.     </field>  
  23.        
  24.     <field name="password">  
  25.         <field-validator type="regex">  
  26.             <param name="trim">ture</param>  
  27.             <param name="expression"><![CDATA[(/w{6,15})]]></param>  
  28.             <message>password must between 6 and 15 and also </message>  
  29.         </field-validator>  
  30.     </field>  
  31. </validators>  
[XML]  view plain copy
  1. <validators>  
  2.     <field name="username">  
  3.         <field-validator type="requiredstring">  
  4.             <param name="trim">true</param>  
  5.             <message>username should be write</message>  
  6.         </field-validator>  
  7.     </field>  
  8.   
  9.     <field name="username">  
  10.         <field-validator type="regex">  
  11.             <param name="trim">ture</param>  
  12.             <param name="expression"><![CDATA[(/w{6,15})]]></param>  
  13.             <message>username must between 6 and 15 and also </message>  
  14.         </field-validator>  
  15.     </field>  
  16.       
  17.         <field name="password">  
  18.         <field-validator type="requiredstring">  
  19.             <param name="trim">true</param>  
  20.             <message>password should be write</message>  
  21.         </field-validator>  
  22.     </field>  
  23.       
  24.     <field name="password">  
  25.         <field-validator type="regex">  
  26.             <param name="trim">ture</param>  
  27.             <param name="expression"><![CDATA[(/w{6,15})]]></param>  
  28.             <message>password must between 6 and 15 and also </message>  
  29.         </field-validator>  
  30.     </field>  
  31. </validators>  
[XML]  view plain  copy
  1. <validators>  
  2.     <field name="username">  
  3.         <field-validator type="requiredstring">  
  4.             <param name="trim">true</param>  
  5.             <message>username should be write</message>  
  6.         </field-validator>  
  7.     </field>  
  8.   
  9.     <field name="username">  
  10.         <field-validator type="regex">  
  11.             <param name="trim">ture</param>  
  12.             <param name="expression"><![CDATA[(/w{6,15})]]></param>  
  13.             <message>username must between 6 and 15 and also </message>  
  14.         </field-validator>  
  15.     </field>  
  16.       
  17.         <field name="password">  
  18.         <field-validator type="requiredstring">  
  19.             <param name="trim">true</param>  
  20.             <message>password should be write</message>  
  21.         </field-validator>  
  22.     </field>  
  23.       
  24.     <field name="password">  
  25.         <field-validator type="regex">  
  26.             <param name="trim">ture</param>  
  27.             <param name="expression"><![CDATA[(/w{6,15})]]></param>  
  28.             <message>password must between 6 and 15 and also </message>  
  29.         </field-validator>  
  30.     </field>  
  31. </validators>  

部分效验文件,虽然有这个文件,但strtus2还是会去访问action中的validate方法的。 

spring部分 
一,
  1. <bean id="userservice" class="myway.userinterface.imp.UserServiceHbimp"></bean>  
  2. <bean id="loginAction" class="myway.action.LoginAction" scope="prototype">  
  3.     <property name="userservice">  
  4.         <ref local="userservice"/>  
  5.     </property>  
  6. </bean>  
  7. <bean id="zjregisterAction" class="myway.action.ZjregAction" scope="prototype">  
  8.     <property name="userservice">  
  9.         <ref local="userservice"/>  
  10.     </property>  
  11. </bean>  
  12. </beans>  
[XML]  view plain copy
  1. <bean id="userservice" class="myway.userinterface.imp.UserServiceHbimp"></bean>  
  2. <bean id="loginAction" class="myway.action.LoginAction" scope="prototype">  
  3.     <property name="userservice">  
  4.         <ref local="userservice"/>  
  5.     </property>  
  6. </bean>  
  7. <bean id="zjregisterAction" class="myway.action.ZjregAction" scope="prototype">  
  8.     <property name="userservice">  
  9.         <ref local="userservice"/>  
  10.     </property>  
  11. </bean>  
  12. </beans>  
[XML]  view plain  copy
  1. <bean id="userservice" class="myway.userinterface.imp.UserServiceHbimp"></bean>  
  2. <bean id="loginAction" class="myway.action.LoginAction" scope="prototype">  
  3.     <property name="userservice">  
  4.         <ref local="userservice"/>  
  5.     </property>  
  6. </bean>  
  7. <bean id="zjregisterAction" class="myway.action.ZjregAction" scope="prototype">  
  8.     <property name="userservice">  
  9.         <ref local="userservice"/>  
  10.     </property>  
  11. </bean>  
  12. </beans>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值