1.这里用的是struts2+spring2+hibernate3.1。用到user表
2.请确定jar包不要重复。
3.用到了struts2的validation框架。spring的配置文件applicationContext.xml,如果启动fwq发现无法加载此配置,请放置到WEB-INF下。hibernate通过映射关联表格,和字段。如果出现不能为空的错误,请在user.hbm.xml中检查字段属性和配置属性有否冲突。比如:
- <property name="registime" type="timestamp">
- <column name="registime" length="0" not-null="false" />
- </property>
- <property name="registime" type="timestamp">
- <column name="registime" length="0" not-null="false" />
- </property>
时间輚不能为空的。
从数据库开始说起。
一,设计方式接口——实现类(这里用的是老紫竹留言本的数据库部分代码)
接口实现如干方法,实现类实现接口中的所有方法。
二,
- <hibernate-configuration>
- <session-factory>
- <property name="show_sql">true</property>
- <property name="myeclipse.connection.profile">Myway</property>
- <property name="connection.url">
- jdbc:mysql://localhost:3306/myway
- </property>
- <property name="connection.username">root</property>
- <property name="connection.password">1</property>
- <property name="connection.driver_class">
- com.mysql.jdbc.Driver
- </property>
- <property name="dialect">
- org.hibernate.dialect.MySQLDialect
- </property>
- <property name="current_session_context_class">thread</property>
- <mapping resource="myway/po/User.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
- <hibernate-configuration>
- <session-factory>
- <property name="show_sql">true</property>
- <property name="myeclipse.connection.profile">Myway</property>
- <property name="connection.url">
- jdbc:mysql://localhost:3306/myway
- </property>
- <property name="connection.username">root</property>
- <property name="connection.password">1</property>
- <property name="connection.driver_class">
- com.mysql.jdbc.Driver
- </property>
- <property name="dialect">
- org.hibernate.dialect.MySQLDialect
- </property>
- <property name="current_session_context_class">thread</property>
- <mapping resource="myway/po/User.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
以上是hibernate.cfg.xml的配置
- <property name="current_session_context_class">thread</property>
- <property name="current_session_context_class">thread</property>
这句必须有。不然会报错。如果不加上对应的是实现类中的错误。
三,表的映射关系
类似javabean的一个类文件,属性对应表中的字段,一个实例对应一条记录。
- <hibernate-mapping>
- <class name="myway.po.User" table="user" catalog="myway">
- <id name="id" type="integer">
- <column name="id" />
- <generator class="assigned"></generator>
- </id>
- <property name="userid" type="string">
- <column name="userid" length="50" not-null="true" />
- </property>
- <property name="userpwd" type="string">
- <column name="userpwd" length="50" />
- </property>
- <property name="userques" type="string">
- <column name="userques" length="50" />
- </property>
- <property name="userans" type="string">
- <column name="userans" length="50" />
- </property>
- <property name="usermail" type="string">
- <column name="usermail" length="50" />
- </property>
- <property name="integral" type="integer">
- <column name="integral" />
- </property>
- <property name="grade" type="integer">
- <column name="grade" />
- </property>
- <property name="sex" type="string">
- <column name="sex" length="8" />
- </property>
- <property name="schoolname" type="string">
- <column name="schoolname" length="50" />
- </property>
- <property name="birthday" type="string">
- <column name="birthday" length="50" />
- </property>
- <property name="realname" type="string">
- <column name="realname" length="50" />
- </property>
- <property name="nicheng" type="string">
- <column name="nicheng" length="50" />
- </property>
- <property name="personalhobby" type="string">
- <column name="personalhobby" length="50" />
- </property>
- <property name="otherinfo" type="string">
- <column name="otherinfo" length="200" />
- </property>
- <property name="registime" type="timestamp">
- <column name="registime" length="0" not-null="false" />
- </property>
- </class>
- </hibernate-mapping>
- <hibernate-mapping>
- <class name="myway.po.User" table="user" catalog="myway">
- <id name="id" type="integer">
- <column name="id" />
- <generator class="assigned"></generator>
- </id>
- <property name="userid" type="string">
- <column name="userid" length="50" not-null="true" />
- </property>
- <property name="userpwd" type="string">
- <column name="userpwd" length="50" />
- </property>
- <property name="userques" type="string">
- <column name="userques" length="50" />
- </property>
- <property name="userans" type="string">
- <column name="userans" length="50" />
- </property>
- <property name="usermail" type="string">
- <column name="usermail" length="50" />
- </property>
- <property name="integral" type="integer">
- <column name="integral" />
- </property>
- <property name="grade" type="integer">
- <column name="grade" />
- </property>
- <property name="sex" type="string">
- <column name="sex" length="8" />
- </property>
- <property name="schoolname" type="string">
- <column name="schoolname" length="50" />
- </property>
- <property name="birthday" type="string">
- <column name="birthday" length="50" />
- </property>
- <property name="realname" type="string">
- <column name="realname" length="50" />
- </property>
- <property name="nicheng" type="string">
- <column name="nicheng" length="50" />
- </property>
- <property name="personalhobby" type="string">
- <column name="personalhobby" length="50" />
- </property>
- <property name="otherinfo" type="string">
- <column name="otherinfo" length="200" />
- </property>
- <property name="registime" type="timestamp">
- <column name="registime" length="0" not-null="false" />
- </property>
- </class>
- </hibernate-mapping>
以上是表的xml文件
(对数据库的操作通过po来实现)
下面是struts2部分。
一,
- <struts>
- <constant name="struts.custom.i18n.resources" value="globalMessages"/>
- <constant name="struts.i18n.encodeing" value="UTF-8"/>
- <package name="Myway" extends="struts-default">
- <action name="zjreg" class="zjregisterAction">
- <result name="success" type="redirect" >/jsp/zjregisterinfo.jsp</result>
- <result name="input">/jsp/zjregister.jsp</result>
- </action>
- <action name="login" class="loginAction">
- <result name="success" type="redirect">/my_foot.jsp</result>
- <result name="input" type="redirect">/jsp/login.jsp</result>
- </action>
- </struts>
- <struts>
- <constant name="struts.custom.i18n.resources" value="globalMessages"/>
- <constant name="struts.i18n.encodeing" value="UTF-8"/>
- <package name="Myway" extends="struts-default">
- <action name="zjreg" class="zjregisterAction">
- <result name="success" type="redirect" >/jsp/zjregisterinfo.jsp</result>
- <result name="input">/jsp/zjregister.jsp</result>
- </action>
- <action name="login" class="loginAction">
- <result name="success" type="redirect">/my_foot.jsp</result>
- <result name="input" type="redirect">/jsp/login.jsp</result>
- </action>
- </struts>
以上是struts.xml的部分配置(有于整合的spring本来action 后面的class是逻辑处理类的class文件,现在只是一个表示,这个表示用于spring识别这个action)
二,在action中通过提供一个上面的数据库操作接口的get方法,让spring调用的
- private UserService userservice;
- public void setUserservice(UserService userservice) {
- this.userservice = userservice;
- }
- private UserService userservice;
- public void setUserservice(UserService userservice) {
- this.userservice = userservice;
- }
类似这个样子。
action负责通过接口对象调用实现类完成登陆和注册功能,返回表示success,和input来转到不通的视图。
三,validation.xml更具不通的action类名字,来提供效验文件,这里提供的是LoginAction-validation.xml和ZjregisterAction-validation.xml
- <validators>
- <field name="username">
- <field-validator type="requiredstring">
- <param name="trim">true</param>
- <message>username should be write</message>
- </field-validator>
- </field>
- <field name="username">
- <field-validator type="regex">
- <param name="trim">ture</param>
- <param name="expression"><![CDATA[(/w{6,15})]]></param>
- <message>username must between 6 and 15 and also </message>
- </field-validator>
- </field>
- <field name="password">
- <field-validator type="requiredstring">
- <param name="trim">true</param>
- <message>password should be write</message>
- </field-validator>
- </field>
- <field name="password">
- <field-validator type="regex">
- <param name="trim">ture</param>
- <param name="expression"><![CDATA[(/w{6,15})]]></param>
- <message>password must between 6 and 15 and also </message>
- </field-validator>
- </field>
- </validators>
- <validators>
- <field name="username">
- <field-validator type="requiredstring">
- <param name="trim">true</param>
- <message>username should be write</message>
- </field-validator>
- </field>
- <field name="username">
- <field-validator type="regex">
- <param name="trim">ture</param>
- <param name="expression"><![CDATA[(/w{6,15})]]></param>
- <message>username must between 6 and 15 and also </message>
- </field-validator>
- </field>
- <field name="password">
- <field-validator type="requiredstring">
- <param name="trim">true</param>
- <message>password should be write</message>
- </field-validator>
- </field>
- <field name="password">
- <field-validator type="regex">
- <param name="trim">ture</param>
- <param name="expression"><![CDATA[(/w{6,15})]]></param>
- <message>password must between 6 and 15 and also </message>
- </field-validator>
- </field>
- </validators>
部分效验文件,虽然有这个文件,但strtus2还是会去访问action中的validate方法的。
spring部分
一,
- <bean id="userservice" class="myway.userinterface.imp.UserServiceHbimp"></bean>
- <bean id="loginAction" class="myway.action.LoginAction" scope="prototype">
- <property name="userservice">
- <ref local="userservice"/>
- </property>
- </bean>
- <bean id="zjregisterAction" class="myway.action.ZjregAction" scope="prototype">
- <property name="userservice">
- <ref local="userservice"/>
- </property>
- </bean>
- </beans>
- <bean id="userservice" class="myway.userinterface.imp.UserServiceHbimp"></bean>
- <bean id="loginAction" class="myway.action.LoginAction" scope="prototype">
- <property name="userservice">
- <ref local="userservice"/>
- </property>
- </bean>
- <bean id="zjregisterAction" class="myway.action.ZjregAction" scope="prototype">
- <property name="userservice">
- <ref local="userservice"/>
- </property>
- </bean>
- </beans>
就这些。