Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合

最新版Struts2+Hibernate+Spring整合

目前为止三大框架最新版本是:

struts2.3.16.1

hibernate4.3.4

spring4.0.2

其中struts2和hibernate的下载方式比较简单,但是spring下载有点麻烦,可以直接复制下面链接下载最新版spring


http://repo.springsource.org/libs-release-local/org/springframework/spring/4.0.2.RELEASE/spring-framework-4.0.2.RELEASE-dist.zip

一. 所需的jar包(其中aopaliance-1.0.jar,是spring所依赖的jar,直接复制粘贴到谷歌百度就有的下载)

框架

版本

所需jar包

Struts2

2.3.16.1

Hibernate

4.3.4

spring

4.0.2

其它


二. 创建一张表

CREATE TABLE `user` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`user_name` varchar(20) DEFAULT NULL,

`password` varchar(20) DEFAULT NULL,

`address` varchar(100) DEFAULT NULL,

`phone_number` varchar(20) DEFAULT NULL,

`create_time` datetime DEFAULT NULL,

`update_time` datetime DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULTCHARSET=utf8;

并插入一条数据

INSERT INTO `user` VALUES ('1', 'test','test', 'test', 'test', '2014-03-29 00:48:14', '2014-03-29 00:48:17');

三. 先看下myeclipse的目录结构


四. 配置文件

1. web.xml

[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <web-appversion="3.0"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  7. <display-name></display-name>
  8. <!--添加对spring的支持-->
  9. <context-param>
  10. <param-name>contextConfigLocation</param-name>
  11. <param-value>classpath:applicationContext.xml</param-value>
  12. </context-param>
  13. <listener>
  14. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  15. </listener>
  16. <!--添加对struts2的支持-->
  17. <filter>
  18. <filter-name>struts2</filter-name>
  19. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  20. </filter>
  21. <!--当hibernate+spring配合使用的时候,如果设置了lazy=true,那么在读取数据的时候,当读取了父数据后,
  22. hibernate会自动关闭session,这样,当要使用子数据的时候,系统会抛出lazyinit的错误,
  23. 这时就需要使用spring提供的OpenSessionInViewFilter,OpenSessionInViewFilter主要是保持Session状态
  24. 知道request将全部页面发送到客户端,这样就可以解决延迟加载带来的问题-->
  25. <filter>
  26. <filter-name>openSessionInViewFilter</filter-name>
  27. <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
  28. <init-param>
  29. <param-name>singleSession</param-name>
  30. <param-value>true</param-value>
  31. </init-param>
  32. </filter>
  33. <filter-mapping>
  34. <filter-name>struts2</filter-name>
  35. <url-pattern>/*</url-pattern>
  36. </filter-mapping>
  37. <filter-mapping>
  38. <filter-name>openSessionInViewFilter</filter-name>
  39. <url-pattern>*.do,*.action</url-pattern>
  40. </filter-mapping>
  41. <welcome-file-list>
  42. <welcome-file>index.jsp</welcome-file>
  43. </welcome-file-list>
  44. </web-app>

2. applicationContext.xml

[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xmlns:jee="http://www.springframework.org/schema/jee"
  8. xmlns:tx="http://www.springframework.org/schema/tx"
  9. xsi:schemaLocation="
  10. http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  11. http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  12. http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd
  13. http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-4.0.xsd
  14. http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
  15. <!--加载数据库属性配置文件-->
  16. <context:property-placeholderlocation="classpath:db.properties"/>
  17. <!--数据库连接池c3p0配置-->
  18. <beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"
  19. destroy-method="close">
  20. <propertyname="jdbcUrl"value="${db.url}"></property>
  21. <propertyname="driverClass"value="${db.driverClassName}"></property>
  22. <propertyname="user"value="${db.username}"></property>
  23. <propertyname="password"value="${db.password}"></property>
  24. <propertyname="maxPoolSize"value="40"></property>
  25. <propertyname="minPoolSize"value="1"></property>
  26. <propertyname="initialPoolSize"value="1"></property>
  27. <propertyname="maxIdleTime"value="20"></property>
  28. </bean>
  29. <!--session工厂-->
  30. <beanid="sessionFactory"
  31. class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  32. <propertyname="dataSource">
  33. <refbean="dataSource"/>
  34. </property>
  35. <propertyname="configLocation"value="classpath:hibernate.cfg.xml"/>
  36. <!--自动扫描注解方式配置的hibernate类文件-->
  37. <propertyname="packagesToScan">
  38. <list>
  39. <value>com.bufoon.entity</value>
  40. </list>
  41. </property>
  42. </bean>
  43. <!--配置事务管理器-->
  44. <beanid="transactionManager"
  45. class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  46. <propertyname="sessionFactory"ref="sessionFactory"/>
  47. </bean>
  48. <!--配置事务通知属性-->
  49. <tx:adviceid="txAdvice"transaction-manager="transactionManager">
  50. <!--定义事务传播属性-->
  51. <tx:attributes>
  52. <tx:methodname="insert*"propagation="REQUIRED"/>
  53. <tx:methodname="update*"propagation="REQUIRED"/>
  54. <tx:methodname="edit*"propagation="REQUIRED"/>
  55. <tx:methodname="save*"propagation="REQUIRED"/>
  56. <tx:methodname="add*"propagation="REQUIRED"/>
  57. <tx:methodname="new*"propagation="REQUIRED"/>
  58. <tx:methodname="set*"propagation="REQUIRED"/>
  59. <tx:methodname="remove*"propagation="REQUIRED"/>
  60. <tx:methodname="delete*"propagation="REQUIRED"/>
  61. <tx:methodname="change*"propagation="REQUIRED"/>
  62. <tx:methodname="get*"propagation="REQUIRED"read-only="true"/>
  63. <tx:methodname="find*"propagation="REQUIRED"read-only="true"/>
  64. <tx:methodname="load*"propagation="REQUIRED"read-only="true"/>
  65. <tx:methodname="*"propagation="REQUIRED"read-only="true"/>
  66. </tx:attributes>
  67. </tx:advice>
  68. <!--应用普通类获取bean
  69. <beanid="appContext"class="com.soanl.util.tool.ApplicationUtil"/>-->
  70. <!--配置事务切面-->
  71. <aop:config>
  72. <aop:pointcutid="serviceOperation"
  73. expression="execution(*com.bufoon.service..*.*(..))"/>
  74. <aop:advisoradvice-ref="txAdvice"pointcut-ref="serviceOperation"/>
  75. </aop:config>
  76. <!--自动加载构建bean-->
  77. <context:component-scanbase-package="com.bufoon"/>
  78. </beans>

3. db.properties

[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. db.driverClassName=com.mysql.jdbc.Driver
  2. db.url=jdbc:mysql://localhost:3306/test
  3. db.username=root
  4. db.password=root

4. hibernate.cfg.xml

[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xmlversion='1.0'encoding='UTF-8'?>
  2. <!DOCTYPEhibernate-configurationPUBLIC
  3. "-//Hibernate/HibernateConfigurationDTD3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <propertyname="dialect">org.hibernate.dialect.MySQLDialect</property>
  8. <propertyname="jdbc.batch_size">20</property>
  9. <propertyname="connection.autocommit">true</property>
  10. <!--显示sql语句-->
  11. <propertyname="show_sql">true</property>
  12. <propertyname="connection.useUnicode">true</property>
  13. <propertyname="connection.characterEncoding">UTF-8</property>
  14. <!--缓存设置-->
  15. <propertyname="cache.provider_configuration_file_resource_path">/ehcache.xml</property>
  16. <propertyname="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
  17. <propertyname="cache.use_query_cache">true</property>
  18. </session-factory>
  19. </hibernate-configuration>

5. struts.xml

[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPEstrutsPUBLIC
  3. "-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <constantname="struts.i18n.encoding"value="UTF-8"/>
  7. <constantname="struts.action.extension"value="action"/>
  8. <constantname="struts.serve.static.browserCache"value="false"/>
  9. <packagename="s2sh"namespace="/user"extends="struts-default">
  10. <actionname="login"method="login"class="com.bufoon.action.LoginAction">
  11. <resultname="success">/success.jsp</result>
  12. <resultname="error">/login.jsp</result>
  13. </action>
  14. </package>
  15. </struts>


6. ehcache.xml (可以到下载的hibernate文件目录(hibernate-release-4.3.4.Final\hibernate-release-4.3.4.Final\project\etc)下找

五. JAVA类

1.BaseDAO.java(网上找的一个)

[java] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. packagecom.bufoon.dao;
  2. importjava.io.Serializable;
  3. importjava.util.List;
  4. /**
  5. *基础数据库操作类
  6. *
  7. *@authorss
  8. *
  9. */
  10. publicinterfaceBaseDAO<T>{
  11. /**
  12. *保存一个对象
  13. *
  14. *@paramo
  15. *@return
  16. */
  17. publicSerializablesave(To);
  18. /**
  19. *删除一个对象
  20. *
  21. *@paramo
  22. */
  23. publicvoiddelete(To);
  24. /**
  25. *更新一个对象
  26. *
  27. *@paramo
  28. */
  29. publicvoidupdate(To);
  30. /**
  31. *保存或更新对象
  32. *
  33. *@paramo
  34. */
  35. publicvoidsaveOrUpdate(To);
  36. /**
  37. *查询
  38. *
  39. *@paramhql
  40. *@return
  41. */
  42. publicList<T>find(Stringhql);
  43. /**
  44. *查询集合
  45. *
  46. *@paramhql
  47. *@paramparam
  48. *@return
  49. */
  50. publicList<T>find(Stringhql,Object[]param);
  51. /**
  52. *查询集合
  53. *
  54. *@paramhql
  55. *@paramparam
  56. *@return
  57. */
  58. publicList<T>find(Stringhql,List<Object>param);
  59. /**
  60. *查询集合(带分页)
  61. *
  62. *@paramhql
  63. *@paramparam
  64. *@parampage
  65. *查询第几页
  66. *@paramrows
  67. *每页显示几条记录
  68. *@return
  69. */
  70. publicList<T>find(Stringhql,Object[]param,Integerpage,Integerrows);
  71. /**
  72. *查询集合(带分页)
  73. *
  74. *@paramhql
  75. *@paramparam
  76. *@parampage
  77. *@paramrows
  78. *@return
  79. */
  80. publicList<T>find(Stringhql,List<Object>param,Integerpage,Integerrows);
  81. /**
  82. *获得一个对象
  83. *
  84. *@paramc
  85. *对象类型
  86. *@paramid
  87. *@returnObject
  88. */
  89. publicTget(Class<T>c,Serializableid);
  90. /**
  91. *获得一个对象
  92. *
  93. *@paramhql
  94. *@paramparam
  95. *@returnObject
  96. */
  97. publicTget(Stringhql,Object[]param);
  98. /**
  99. *获得一个对象
  100. *
  101. *@paramhql
  102. *@paramparam
  103. *@return
  104. */
  105. publicTget(Stringhql,List<Object>param);
  106. /**
  107. *selectcount(*)from类
  108. *
  109. *@paramhql
  110. *@return
  111. */
  112. publicLongcount(Stringhql);
  113. /**
  114. *selectcount(*)from类
  115. *
  116. *@paramhql
  117. *@paramparam
  118. *@return
  119. */
  120. publicLongcount(Stringhql,Object[]param);
  121. /**
  122. *selectcount(*)from类
  123. *
  124. *@paramhql
  125. *@paramparam
  126. *@return
  127. */
  128. publicLongcount(Stringhql,List<Object>param);
  129. /**
  130. *执行HQL语句
  131. *
  132. *@paramhql
  133. *@return响应数目
  134. */
  135. publicIntegerexecuteHql(Stringhql);
  136. /**
  137. *执行HQL语句
  138. *
  139. *@paramhql
  140. *@paramparam
  141. *@return响应数目
  142. */
  143. publicIntegerexecuteHql(Stringhql,Object[]param);
  144. /**
  145. *执行HQL语句
  146. *
  147. *@paramhql
  148. *@paramparam
  149. *@return
  150. */
  151. publicIntegerexecuteHql(Stringhql,List<Object>param);
  152. }

2. BaseDAOImpl.java

[java] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. packagecom.bufoon.dao.impl;
  2. importjava.io.Serializable;
  3. importjava.util.List;
  4. importorg.hibernate.Query;
  5. importorg.hibernate.Session;
  6. importorg.hibernate.SessionFactory;
  7. importorg.springframework.beans.factory.annotation.Autowired;
  8. importorg.springframework.stereotype.Repository;
  9. importcom.bufoon.dao.BaseDAO;
  10. @Repository("baseDAO")
  11. @SuppressWarnings("all")
  12. publicclassBaseDAOImpl<T>implementsBaseDAO<T>{
  13. privateSessionFactorysessionFactory;
  14. publicSessionFactorygetSessionFactory(){
  15. returnsessionFactory;
  16. }
  17. @Autowired
  18. publicvoidsetSessionFactory(SessionFactorysessionFactory){
  19. this.sessionFactory=sessionFactory;
  20. }
  21. privateSessiongetCurrentSession(){
  22. returnsessionFactory.getCurrentSession();
  23. }
  24. publicSerializablesave(To){
  25. returnthis.getCurrentSession().save(o);
  26. }
  27. publicvoiddelete(To){
  28. this.getCurrentSession().delete(o);
  29. }
  30. publicvoidupdate(To){
  31. this.getCurrentSession().update(o);
  32. }
  33. publicvoidsaveOrUpdate(To){
  34. this.getCurrentSession().saveOrUpdate(o);
  35. }
  36. publicList<T>find(Stringhql){
  37. returnthis.getCurrentSession().createQuery(hql).list();
  38. }
  39. publicList<T>find(Stringhql,Object[]param){
  40. Queryq=this.getCurrentSession().createQuery(hql);
  41. if(param!=null&&param.length>0){
  42. for(inti=0;i<param.length;i++){
  43. q.setParameter(i,param[i]);
  44. }
  45. }
  46. returnq.list();
  47. }
  48. publicList<T>find(Stringhql,List<Object>param){
  49. Queryq=this.getCurrentSession().createQuery(hql);
  50. if(param!=null&&param.size()>0){
  51. for(inti=0;i<param.size();i++){
  52. q.setParameter(i,param.get(i));
  53. }
  54. }
  55. returnq.list();
  56. }
  57. publicList<T>find(Stringhql,Object[]param,Integerpage,Integerrows){
  58. if(page==null||page<1){
  59. page=1;
  60. }
  61. if(rows==null||rows<1){
  62. rows=10;
  63. }
  64. Queryq=this.getCurrentSession().createQuery(hql);
  65. if(param!=null&&param.length>0){
  66. for(inti=0;i<param.length;i++){
  67. q.setParameter(i,param[i]);
  68. }
  69. }
  70. returnq.setFirstResult((page-1)*rows).setMaxResults(rows).list();
  71. }
  72. publicList<T>find(Stringhql,List<Object>param,Integerpage,Integerrows){
  73. if(page==null||page<1){
  74. page=1;
  75. }
  76. if(rows==null||rows<1){
  77. rows=10;
  78. }
  79. Queryq=this.getCurrentSession().createQuery(hql);
  80. if(param!=null&&param.size()>0){
  81. for(inti=0;i<param.size();i++){
  82. q.setParameter(i,param.get(i));
  83. }
  84. }
  85. returnq.setFirstResult((page-1)*rows).setMaxResults(rows).list();
  86. }
  87. publicTget(Class<T>c,Serializableid){
  88. return(T)this.getCurrentSession().get(c,id);
  89. }
  90. publicTget(Stringhql,Object[]param){
  91. List<T>l=this.find(hql,param);
  92. if(l!=null&&l.size()>0){
  93. returnl.get(0);
  94. }else{
  95. returnnull;
  96. }
  97. }
  98. publicTget(Stringhql,List<Object>param){
  99. List<T>l=this.find(hql,param);
  100. if(l!=null&&l.size()>0){
  101. returnl.get(0);
  102. }else{
  103. returnnull;
  104. }
  105. }
  106. publicLongcount(Stringhql){
  107. return(Long)this.getCurrentSession().createQuery(hql).uniqueResult();
  108. }
  109. publicLongcount(Stringhql,Object[]param){
  110. Queryq=this.getCurrentSession().createQuery(hql);
  111. if(param!=null&&param.length>0){
  112. for(inti=0;i<param.length;i++){
  113. q.setParameter(i,param[i]);
  114. }
  115. }
  116. return(Long)q.uniqueResult();
  117. }
  118. publicLongcount(Stringhql,List<Object>param){
  119. Queryq=this.getCurrentSession().createQuery(hql);
  120. if(param!=null&&param.size()>0){
  121. for(inti=0;i<param.size();i++){
  122. q.setParameter(i,param.get(i));
  123. }
  124. }
  125. return(Long)q.uniqueResult();
  126. }
  127. publicIntegerexecuteHql(Stringhql){
  128. returnthis.getCurrentSession().createQuery(hql).executeUpdate();
  129. }
  130. publicIntegerexecuteHql(Stringhql,Object[]param){
  131. Queryq=this.getCurrentSession().createQuery(hql);
  132. if(param!=null&&param.length>0){
  133. for(inti=0;i<param.length;i++){
  134. q.setParameter(i,param[i]);
  135. }
  136. }
  137. returnq.executeUpdate();
  138. }
  139. publicIntegerexecuteHql(Stringhql,List<Object>param){
  140. Queryq=this.getCurrentSession().createQuery(hql);
  141. if(param!=null&&param.size()>0){
  142. for(inti=0;i<param.size();i++){
  143. q.setParameter(i,param.get(i));
  144. }
  145. }
  146. returnq.executeUpdate();
  147. }
  148. }


3. UserService.java

[java] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. packagecom.bufoon.service.user;
  2. importjava.util.List;
  3. importcom.bufoon.entity.User;
  4. publicinterfaceUserService{
  5. publicvoidsaveUser(Useruser);
  6. publicvoidupdateUser(Useruser);
  7. publicUserfindUserById(intid);
  8. publicvoiddeleteUser(Useruser);
  9. publicList<User>findAllList();
  10. publicUserfindUserByNameAndPassword(Stringusername,Stringpassword);
  11. }

4. UserServiceImpl.java

[java] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. packagecom.bufoon.service.user.impl;
  2. importjava.util.List;
  3. importjavax.annotation.Resource;
  4. importorg.springframework.stereotype.Service;
  5. importcom.bufoon.dao.BaseDAO;
  6. importcom.bufoon.entity.User;
  7. importcom.bufoon.service.user.UserService;
  8. @Service("userService")
  9. publicclassUserServiceImplimplementsUserService{
  10. @Resource
  11. privateBaseDAO<User>baseDAO;
  12. @Override
  13. publicvoidsaveUser(Useruser){
  14. baseDAO.save(user);
  15. }
  16. @Override
  17. publicvoidupdateUser(Useruser){
  18. baseDAO.update(user);
  19. }
  20. @Override
  21. publicUserfindUserById(intid){
  22. returnbaseDAO.get(User.class,id);
  23. }
  24. @Override
  25. publicvoiddeleteUser(Useruser){
  26. baseDAO.delete(user);
  27. }
  28. @Override
  29. publicList<User>findAllList(){
  30. returnbaseDAO.find("fromUseruorderbyu.createTime");
  31. }
  32. @Override
  33. publicUserfindUserByNameAndPassword(Stringusername,Stringpassword){
  34. returnbaseDAO.get("fromUseruwhereu.userName=?andu.password=?",newObject[]{username,password});
  35. }
  36. }

5. LoginAction

[java] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. packagecom.bufoon.action;
  2. importjavax.annotation.Resource;
  3. importjavax.servlet.http.HttpServletRequest;
  4. importorg.apache.struts2.ServletActionContext;
  5. importorg.springframework.stereotype.Controller;
  6. importcom.bufoon.entity.User;
  7. importcom.bufoon.service.user.UserService;
  8. importcom.opensymphony.xwork2.ActionSupport;
  9. @Controller
  10. publicclassLoginActionextendsActionSupport{
  11. privatestaticfinallongserialVersionUID=1L;
  12. @Resource
  13. privateUserServiceuserService;
  14. privateStringusername;
  15. privateStringpassword;
  16. publicStringlogin(){
  17. HttpServletRequestrequest=ServletActionContext.getRequest();
  18. Useruser=userService.findUserByNameAndPassword(username,password);
  19. if(user!=null){
  20. request.setAttribute("username",username);
  21. returnSUCCESS;
  22. }else{
  23. returnERROR;
  24. }
  25. }
  26. publicStringgetUsername(){
  27. returnusername;
  28. }
  29. publicvoidsetUsername(Stringusername){
  30. this.username=username;
  31. }
  32. publicStringgetPassword(){
  33. returnpassword;
  34. }
  35. publicvoidsetPassword(Stringpassword){
  36. this.password=password;
  37. }
  38. }

6. Util.java

[java] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. packagecom.bufoon.util;
  2. importjava.io.PrintWriter;
  3. importjava.io.StringWriter;
  4. importjava.security.MessageDigest;
  5. importorg.springframework.context.ApplicationContext;
  6. importorg.springframework.context.support.ClassPathXmlApplicationContext;
  7. importcom.bufoon.entity.User;
  8. importcom.bufoon.service.user.UserService;
  9. importsun.misc.BASE64Encoder;
  10. /**
  11. *通用工具类
  12. */
  13. publicclassUtil{
  14. /**
  15. *对字符串进行MD5加密
  16. *
  17. *@paramstr
  18. *@returnString
  19. */
  20. publicstaticStringmd5Encryption(Stringstr){
  21. StringnewStr=null;
  22. try{
  23. MessageDigestmd5=MessageDigest.getInstance("MD5");
  24. BASE64Encoderbase=newBASE64Encoder();
  25. newStr=base.encode(md5.digest(str.getBytes("UTF-8")));
  26. }catch(Exceptione){
  27. e.printStackTrace();
  28. }
  29. returnnewStr;
  30. }
  31. /**
  32. *判断字符串是否为空
  33. *
  34. *@paramstr
  35. *字符串
  36. *@returntrue:为空;false:非空
  37. */
  38. publicstaticbooleanisNull(Stringstr){
  39. if(str!=null&&!str.trim().equals("")){
  40. returnfalse;
  41. }else{
  42. returntrue;
  43. }
  44. }
  45. }
7.User.java
[java] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. packagecom.bufoon.entity;
  2. importjava.util.Date;
  3. importjavax.persistence.Column;
  4. importjavax.persistence.Entity;
  5. importjavax.persistence.GeneratedValue;
  6. importjavax.persistence.Id;
  7. importjavax.persistence.Temporal;
  8. importjavax.persistence.TemporalType;
  9. importorg.hibernate.annotations.GenericGenerator;
  10. @Entity
  11. publicclassUser{
  12. privateIntegerid;
  13. privateStringuserName;
  14. privateStringpassword;
  15. privateStringaddress;
  16. privateStringphoneNumber;
  17. privateDatecreateTime;
  18. privateDateupdateTime;
  19. @Id
  20. @GenericGenerator(name="generator",strategy="increment")
  21. @GeneratedValue(generator="generator")
  22. @Column(name="ID",length=11)
  23. publicIntegergetId(){
  24. returnid;
  25. }
  26. publicvoidsetId(Integerid){
  27. this.id=id;
  28. }
  29. @Column(name="user_name",length=20)
  30. publicStringgetUserName(){
  31. returnuserName;
  32. }
  33. publicvoidsetUserName(StringuserName){
  34. this.userName=userName;
  35. }
  36. @Column(name="password",length=20)
  37. publicStringgetPassword(){
  38. returnpassword;
  39. }
  40. publicvoidsetPassword(Stringpassword){
  41. this.password=password;
  42. }
  43. @Column(name="address",length=100)
  44. publicStringgetAddress(){
  45. returnaddress;
  46. }
  47. publicvoidsetAddress(Stringaddress){
  48. this.address=address;
  49. }
  50. @Column(name="phone_number",length=20)
  51. publicStringgetPhoneNumber(){
  52. returnphoneNumber;
  53. }
  54. publicvoidsetPhoneNumber(StringphoneNumber){
  55. this.phoneNumber=phoneNumber;
  56. }
  57. @Temporal(TemporalType.TIMESTAMP)
  58. @Column(name="create_time")
  59. publicDategetCreateTime(){
  60. returncreateTime;
  61. }
  62. publicvoidsetCreateTime(DatecreateTime){
  63. this.createTime=createTime;
  64. }
  65. @Temporal(TemporalType.TIMESTAMP)
  66. @Column(name="update_time")
  67. publicDategetUpdateTime(){
  68. returnupdateTime;
  69. }
  70. publicvoidsetUpdateTime(DateupdateTime){
  71. this.updateTime=updateTime;
  72. }
  73. }


六. JSP文件

1. login.jsp

[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
  2. <%
  3. Stringpath=request.getContextPath();
  4. StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
  7. <html>
  8. <head>
  9. <basehref="<%=basePath%>">
  10. <title>MyJSP'index.jsp'startingpage</title>
  11. <metahttp-equiv="pragma"content="no-cache">
  12. <metahttp-equiv="cache-control"content="no-cache">
  13. <metahttp-equiv="expires"content="0">
  14. <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
  15. <metahttp-equiv="description"content="Thisismypage">
  16. <!--
  17. <linkrel="stylesheet"type="text/css"href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <formaction="${pageContext.request.contextPath}/user/login.action"method="post">
  22. username:<inputtype="text"name="username"/><br/>
  23. password:<inputtype="password"name="password"/><br/>
  24. <inputtype="submit"value="login"/><inputtype="reset"value="reset"/>
  25. </form>
  26. </body>
  27. </html>

2. success.jsp

[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
  2. <%
  3. Stringpath=request.getContextPath();
  4. StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
  7. <html>
  8. <head>
  9. <basehref="<%=basePath%>">
  10. <title>MyJSP'index.jsp'startingpage</title>
  11. <metahttp-equiv="pragma"content="no-cache">
  12. <metahttp-equiv="cache-control"content="no-cache">
  13. <metahttp-equiv="expires"content="0">
  14. <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
  15. <metahttp-equiv="description"content="Thisismypage">
  16. <!--
  17. <linkrel="stylesheet"type="text/css"href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. 欢迎您:{username}!
  22. </body>
  23. </html>

附上下载地址:http://download.csdn.net/detail/soanl/7158959

================================================================ENDING========================================================

2014-03-29

布丰(bufoon)

更正(struts.xml文件)感谢u013506859提出
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值