struts2验证+拦截器+国际化+下载excle文档+struts2二级联动+ongl表达试+ssh集成\部分代码, 项目下载

struts2验证+拦截器+国际化+下载excle文档+struts2二级联动+ongl表达试+验证码+ssh集成

项目下载:

http://download.csdn.net/user/liangrui1988

 

部分代码:

 

 

[html][/html] view plaincopy

  1. <%@ page language=”java” import=”java.util.*” pageEncoding=”UTF-8″%>
  2. <%@ taglib prefix=”s” uri=”/struts-tags” %>
  3. <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
  4. <html>
  5.   <head>
  6.     <title>My JSP ‘index.jsp’ starting page</title>
  7.   </head>
  8.   <body>
  9.   <p><a href=”
  10.   change.action?request_locale=zh_CN
  11.   “>中文</a> <a href=”change.action?request_locale=zh_HK”>繁體</a>
  12.   <a href=”change.action?request_locale=en”>english</a></p>
  13.   <font color=”blue”><s:text name=”new.user.register”/></font>
  14.   <s:form action=”register_user.action”>
  15.     <s:text name=”user1.email”></s:text>
  16.     <s:textfield label=”%{getText(‘user1.name’)}” name=”user1.name”/>
  17.     <s:textfield label=”%{getText(‘user1.email’)}” name=”user1.email”/>
  18.    <s:textfield label=”%{getText(‘user1.password’)}” name=”user1.password”/>
  19.     <s:textfield label=”%{getText(‘user1.rePassword’)}” name=”user1.rePassword”/>
  20.      <s:textfield label=”%{getText(‘user1.age’)}” name=”user1.age”/>
  21.   <s:submit value=”%{getText(‘enters’)}”/>
  22.   </s:form>
  23.   </body>
  24. </html>

 

 

 

[html][/html] view plaincopy

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <!DOCTYPE validators
  3. PUBLIC “-//Apache Struts//XWork Validator 1.0.2//EN”
  4. “http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd”>
  5. <validators>
  6.  <field name=”name”>
  7.  <field-validator type=”requiredstring”>
  8.  <message key=”nameInvok”></message>
  9.  </field-validator>
  10.  </field>
  11.  <field name=”email”>
  12.    <field-validator type=”required”>
  13.        <message>You cannot leave the email address field empty.</message>
  14.    </field-validator>
  15.    <field-validator type=”email”>
  16.        <message>The email address you entered is not valid.</message>
  17.    </field-validator>
  18.  </field>
  19.  <field name=”password”>
  20.  <field-validator type=”requiredstring”>
  21.  <message key=”passwordInvok”></message>
  22.  </field-validator>
  23.  <field-validator type=”stringlength”>
  24.  <param name=”maxLength”>10</param>
  25.  <param name=”minLength”>4</param>
  26.  <message key=”passwod.length”> err to  ${maxLength} bettowe ${minLength} </message>
  27.  </field-validator>
  28.  </field>
  29.  <field name=”rePassword”>
  30.  <field-validator type=”fieldexpression”>
  31.     <param name=”expression”><![CDATA[rePassword==password]]></param>
  32.      <message key=”rePass”>xxxxaaaaa</message>
  33.  </field-validator>
  34.  </field>
  35.   <!– 非字段校验器
  36.   <validator type=”required”>
  37.   <param name=”fileName”>email</param>
  38.    <param name=”fileName”>age</param>
  39.     <param name=”fileName”>name</param>
  40.      <param name=”fileName”>password</param>
  41.       <param name=”fileName”>rePassword</param>
  42.   <message>字段不能为空</message>
  43.   </validator>
  44.  –>
  45. </validators>

spring.xml

 

 

[html][/html] view plaincopy

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <beans
  3.     xmlns=”http://www.springframework.org/schema/beans”
  4.     xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
  5.     xmlns:p=”http://www.springframework.org/schema/p”
  6.     xsi:schemaLocation=”http://www.springframework.org/schema/beans
  7.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>
  8. <bean id=”testBean” class=”accp.ssh.test.TestBean” >
  9. <property name=”name” value=”刘备”/>
  10. </bean>
  11. <!– 配制数据源  destroy-method=”close” 用完之后自动关闭–>
  12. <bean id=”dataSoruce” class=”org.apache.commons.dbcp.BasicDataSource” destroy-method=”close”>
  13. <property name=”driverClassName” value=”oracle.jdbc.driver.OracleDriver”/>
  14. <property name=”url” value=”jdbc:oracle:thin:@localhost:1521:ABC”/>
  15. <property name=”username” value=”tenement”/>
  16. <property name=”password” value=”rui”/>
  17. <property name=”maxActive” value=”100″/>
  18. <property name=”maxIdle” value=”30″/>
  19. <property name=”maxWait” value=”500″/>
  20. <!– 自动提交事务 –>
  21. <property name=”defaultAutoCommit” value=”true”/>
  22. </bean>
  23. <!– sessionFactory配制 –>
  24. <bean id=”sessionFactory” class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>
  25. <property name=”dataSource” ref=”dataSoruce”/>
  26. <property name=”hibernateProperties”>
  27. <!–
  28. <props>
  29. <prop key=””></prop>
  30. </props>
  31. –>
  32.        <value>
  33.         hibernate.dialect=org.hibernate.dialect.OracleDialect
  34.         hibernate.hbm2ddl.auto=update
  35.         hibernate.show_sql=true
  36.         </value>
  37. </property>
  38. <!– 映射文件 –>
  39. <property name=”mappingResources” >
  40. <list>
  41. <value>accp/ssh/bean/Users3.hbm.xml</value>
  42. </list>
  43. </property>
  44. </bean>
  45. <!– userdao – HibernateDaoSupport类需要一个注入配制 ||  singleton 是在IOC容器里生成唯一的一个实列–>
  46. <bean id=”userDao” class=”accp.ssh.dao.Imple.UserDaoImple” scope=”singleton”>
  47. <property name=”sessionFactory”>
  48. <ref bean=”sessionFactory”/>
  49. </property>
  50. </bean>
  51. <!– service实现层   属性UserDaoInterface dao的注入     –>
  52. <bean id=”serviceUser” class=”accp.ssh.serviceImple.UserServiceImple”>
  53. <property name=”dao” ref=”userDao”/>
  54. </bean>
  55. <!– 配制saveSerivceUser.action 保存用户   注入属性业务层的userService接口–>
  56. <bean id=”saveUsersAction” class=”accp.ssh.action.users.SaveUsers” scope=”prototype”>
  57. <property name=”userService” ref=”serviceUser”/>
  58. </bean>
  59. <!– 配制查询所有用户, 注入业务层 得到实列  id=struts.action类class–>
  60. <bean id=”listUserActiond” class=”accp.ssh.action.users.ListUser”>
  61. <property name=”userSevice” ref=”serviceUser”/>
  62. </bean>
  63. <!– 删除用户 –>
  64. <bean id=”removeUserAction” class=”accp.ssh.action.users.RemoveUser” scope=”prototype”>
  65. <property name=”serviceUser” ref=”serviceUser”/>
  66. </bean>
  67. <!– 更新用户 –>
  68. <bean id=”updatePUserAction” class=”accp.ssh.action.users.UpdatePUser” scope=”prototype”>
  69. <property name=”serviceUser” ref=”serviceUser” />
  70. </bean>
  71. <!– 确认更新用户 –>
  72. <bean id=”updateUseraction” class=”accp.ssh.action.users.UpdateUser” scope=”prototype”>
  73. <property name=”userService” ref=”serviceUser”></property>
  74. </bean>
  75. <!– 下载文件 excel–>
  76. <bean id=”dowloadUserDocAction” class=”accp.ssh.action.users.DowloadUserDoc”>
  77. <property name=”serviceUser” ref=”serviceUser”/>
  78. </bean>
  79. </beans>

struts.xml

 

 

[html][/html] view plaincopy

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <!DOCTYPE struts PUBLIC
  3.     “-//Apache Software Foundation//DTD Struts Configuration 2.3//EN”
  4.     “http://struts.apache.org/dtds/struts-2.3.dtd”>
  5. <struts>
  6. <include file=”jqueryAction.xml”/>
  7. <package name=”default” extends=”struts-default”>
  8. <!– class是spring注入的id名称 –>
  9. <action name=”saveUser” class=”saveUsersAction”>
  10. <result name=”success”  type=”redirectAction”>listUser</result>
  11. <result name=”input”>/saveUser.jsp</result>
  12. <result name=”error”>/err.jsp</result>
  13. </action>
  14. <action name=”listUser” class=”listUserActiond”>
  15. <result name=”success” >listUser.jsp</result>
  16. </action>
  17. <!– 删除用户 –>
  18. <action name=”removeUser” class=”removeUserAction”>
  19. <result name=”success” type=”redirectAction”>listUser</result>
  20. </action>
  21. <!– 更新用户  Page–>
  22. <action name=”updatePUser” class=”updatePUserAction”>
  23. <result>updateUser.jsp</result>
  24. </action>
  25. <!–确认更新用户 –>
  26. <action name=”updateUser” class=”updateUseraction”>
  27. <result name=”success” type=”redirectAction”>listUser</result>
  28. <result name=”input”>updateUser.jsp</result>
  29. <result name=”error”>err.jsp</result>
  30. </action>
  31. <!– 下载exec –>
  32. <action name=”dowloadUserDoc” class=”dowloadUserDocAction”>
  33. <result name=”success” type=”stream”>
  34. <!– 指定下载文件配制 –>
  35. <!– 配制文件类型为excel==xls –>
  36. <param name=”contentType”>application/vnd.ms-excel</param>
  37. <!– 配制文件名 –>
  38. <param name=”contentDisposition”>filename=”allUserInfo.xls”</param>
  39. <!– 文件下载的方法名字 – 在action里的操作方法–>
  40. <param name=”inputName”>downloadUserXls</param>
  41. </result>
  42. </action>
  43. </package>
  44. </struts>

update.xml

 

 

[html][/html] view plaincopy

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <!DOCTYPE validators
  3. PUBLIC “-//Apache Struts//XWork Validator 1.0.2//EN”
  4. “http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd”>
  5. <validators>
  6. <field name=”user”>
  7. <!– 访问类型  指定对另一个xml文件过行访问,对 XXX-user-validation的文件进行访问 ,并验证 –>
  8. <field-validator type=”visitor”>
  9. <!– 指定对那个对象验证 –>
  10. <param name=”context” >user</param>
  11. <!–前面统一提示信息 –>
  12. <param name=”appendPrefix”>true</param>
  13. <message>update user s’  </message>
  14. </field-validator>
  15. </field>
  16. </validators>
[html][/html] view plaincopy

 

下载为xls方档

 

[html][/html] view plaincopy

  1. package accp.ssh.serviceImple;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10. import java.util.List;
  11. import org.apache.commons.io.output.ByteArrayOutputStream;
  12. import org.apache.poi.hssf.usermodel.HSSFCell;
  13. import org.apache.poi.hssf.usermodel.HSSFRow;
  14. import org.apache.poi.hssf.usermodel.HSSFSheet;
  15. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  16. import accp.ssh.bean.Users3;
  17. import accp.ssh.dao.Imple.UserDaoImple;
  18. import accp.ssh.dao.Interface.UserDaoInterface;
  19. import accp.ssh.serviceInterface.UserServiceInteface;
  20. public class UserServiceImple implements UserServiceInteface {
  21.     private UserDaoInterface dao; //在spring里面注入
  22.     public UserDaoInterface getDao() {
  23.         return dao;
  24.     }
  25.     public void setDao(UserDaoInterface dao) {
  26.         this.dao = dao;
  27.     }
  28.     @Override
  29.     public void addUser(Users3 u) {
  30.         dao.addUser(u);
  31.     }
  32.     @Override
  33.     public List<Users3> getUserList() {
  34.         return dao.getUserList();
  35.     }
  36.     @Override
  37.     public void removeUser(Users3 u) {
  38.        dao.removeUser(u);
  39.     }
  40.     @Override
  41.     public Users3 getUser(Integer id) {
  42.         return dao.getUser(id);
  43.     }
  44.     @Override
  45.     public void updateUser(Users3 u) {
  46.        dao.updateUser(u);
  47.     }
  48.     /**
  49.      * 下载用户文档并生成exec
  50.      * HSSFWorkbook poi包对象
  51.      */
  52.     @Override
  53.     public InputStream getUserDocStream() {
  54.         HSSFWorkbook wb=new HSSFWorkbook();//创建exec对象
  55.         HSSFSheet sheet=wb.createSheet(); //创建一张工作表
  56.         HSSFRow row=sheet.createRow(0);//创建一行,从0开始
  57.         HSSFCell cell;
  58.         //0
  59.         cell=row.createCell((short)0);//创建一个单元格,并放入数据
  60.         cell.setEncoding(HSSFCell.ENCODING_UTF_16);//指定编码
  61.         cell.setCellValue(“序列”); //设值
  62.         //1
  63.         cell=row.createCell((short)1);//创建一个单元格,并放入数据
  64.         cell.setEncoding(HSSFCell.ENCODING_UTF_16);//指定编码
  65.         cell.setCellValue(“ID号”); //设值
  66.         //2
  67.         cell=row.createCell((short)2);
  68.         cell.setEncoding(cell.ENCODING_UTF_16);
  69.         cell.setCellValue(“姓”);
  70.         //2
  71.         cell=row.createCell((short)3);
  72.         cell.setEncoding(cell.ENCODING_UTF_16);
  73.         cell.setCellValue(“名”);
  74.         //2
  75.         cell=row.createCell((short)4);
  76.         cell.setEncoding(cell.ENCODING_UTF_16);
  77.         cell.setCellValue(“年龄”);
  78.         //从数据库里取数据
  79.         List<Users3> listUser=dao.getUserList();
  80.         for(int i=0;i<listUser.size();++i){
  81.             Users3 user=listUser.get(i);
  82.             row=sheet.createRow(i+1);//创建一行,从0开始
  83.             //0
  84.             cell=row.createCell((short)0);
  85.             cell.setEncoding(cell.ENCODING_UTF_16);
  86.             cell.setCellValue(i);
  87.             //1
  88.             cell=row.createCell((short)1);
  89.             cell.setEncoding(cell.ENCODING_UTF_16);
  90.             cell.setCellValue(user.getId());
  91.             //2
  92.             cell=row.createCell((short)2);
  93.             cell.setEncoding(cell.ENCODING_UTF_16);
  94.             cell.setCellValue(user.getFirstName());
  95.             //3
  96.             cell=row.createCell((short)3);
  97.             cell.setEncoding(cell.ENCODING_UTF_16);
  98.             cell.setCellValue(user.getLastName());
  99.             //4
  100.             cell=row.createCell((short)4);
  101.             cell.setEncoding(cell.ENCODING_UTF_16);
  102.             cell.setCellValue(user.getAge());
  103.         }
  104.         //方式一 以字节流的方式  不会产生监时文件,是在内存中加载
  105.         ByteArrayOutputStream os =new ByteArrayOutputStream();//字节数组输出流
  106.         try {
  107.             wb.write(os);//写入字节输出流对象是
  108.         } catch (IOException e) {
  109.             e.printStackTrace();
  110.         }
  111.         byte[] bArry=os.toByteArray();//获取字节数组
  112.         //将数组字节 写入到输入流对象
  113.         InputStream isba=new ByteArrayInputStream(bArry);
  114.         return isba;
  115.         //办能获取一部分字节流,不可行
  116.     /*  byte [] b=wb.getBytes();//获取文件字节流
  117.         //把字节数组写入到字节输入流对象
  118.         InputStream isb=new ByteArrayInputStream(b);
  119.         return isb;*/
  120.         //方式二
  121.         /*//获得文件流
  122.         final File file=new File(“userDoc.xls”);
  123.         OutputStream os;//输出流
  124.         InputStream is = null;//输入流
  125.         try {
  126.             os=new FileOutputStream(file);
  127.             wb.write(os); //将wb文档  通过输出流  写入到File文件中
  128.             os.close();
  129.         } catch (FileNotFoundException e) {
  130.             // TODO Auto-generated catch block
  131.             e.printStackTrace();
  132.         } catch (IOException e) {
  133.             // TODO Auto-generated catch block
  134.             e.printStackTrace();
  135.         }
  136.         //将file文件 ,写入输入文件流,并返回
  137.         try {
  138.             is=new FileInputStream(file);
  139.         } catch (FileNotFoundException e) {
  140.             // TODO Auto-generated catch block
  141.             e.printStackTrace();
  142.         }
  143.         //用线程删掉tomact服务器的File的临时文件
  144.         new Thread(
  145.                 new Runnable(){
  146.                     public void run() {
  147.                     //线程休眠15秒
  148.                     try {
  149.                         Thread.sleep(15000);
  150.                     } catch (InterruptedException e) {
  151.                         // TODO Auto-generated catch block
  152.                         e.printStackTrace();
  153.                     }
  154.                     file.delete();//删除临时文件
  155.                     }
  156.                 }
  157.                 ).start();
  158.         //如果服务器在此之间关闭,监时文件不会删除,永远在,这时要用一个servlet,init()去删除
  159.         return is;*/
  160.     }
  161. }

 

太多了!有兴趣可以下载项目!

标签