struts2验证+拦截器+国际化+下载excle文档+struts2二级联动+OGNL表达试+验证码+ssh集成
项目下载:
http://download.csdn.net/detail/liangrui1988/5875807
部分代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> <p><a href=" change.action?request_locale=zh_CN ">中文</a> <a href="change.action?request_locale=zh_HK">繁體</a> <a href="change.action?request_locale=en">english</a></p> <font color="blue"><s:text name="new.user.register"/></font> <s:form action="register_user.action"> <s:text name="user1.email"></s:text> <s:textfield label="%{getText('user1.name')}" name="user1.name"/> <s:textfield label="%{getText('user1.email')}" name="user1.email"/> <s:textfield label="%{getText('user1.password')}" name="user1.password"/> <s:textfield label="%{getText('user1.rePassword')}" name="user1.rePassword"/> <s:textfield label="%{getText('user1.age')}" name="user1.age"/> <s:submit value="%{getText('enters')}"/> </s:form> </body> </html>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> <validators> <field name="name"> <field-validator type="requiredstring"> <message key="nameInvok"></message> </field-validator> </field> <field name="email"> <field-validator type="required"> <message>You cannot leave the email address field empty.</message> </field-validator> <field-validator type="email"> <message>The email address you entered is not valid.</message> </field-validator> </field> <field name="password"> <field-validator type="requiredstring"> <message key="passwordInvok"></message> </field-validator> <field-validator type="stringlength"> <param name="maxLength">10</param> <param name="minLength">4</param> <message key="passwod.length"> err to ${maxLength} bettowe ${minLength} </message> </field-validator> </field> <field name="rePassword"> <field-validator type="fieldexpression"> <param name="expression"><![CDATA[rePassword==password]]></param> <message key="rePass">xxxxaaaaa</message> </field-validator> </field> <!-- 非字段校验器 <validator type="required"> <param name="fileName">email</param> <param name="fileName">age</param> <param name="fileName">name</param> <param name="fileName">password</param> <param name="fileName">rePassword</param> <message>字段不能为空</message> </validator> --> </validators>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="testBean" class="accp.ssh.test.TestBean" > <property name="name" value="刘备"/> </bean> <!-- 配制数据源 destroy-method="close" 用完之后自动关闭--> <bean id="dataSoruce" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@localhost:1521:ABC"/> <property name="username" value="tenement"/> <property name="password" value="rui"/> <property name="maxActive" value="100"/> <property name="maxIdle" value="30"/> <property name="maxWait" value="500"/> <!-- 自动提交事务 --> <property name="defaultAutoCommit" value="true"/> </bean> <!-- sessionFactory配制 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSoruce"/> <property name="hibernateProperties"> <!-- <props> <prop key=""></prop> </props> --> <value> hibernate.dialect=org.hibernate.dialect.OracleDialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true </value> </property> <!-- 映射文件 --> <property name="mappingResources" > <list> <value>accp/ssh/bean/Users3.hbm.xml</value> </list> </property> </bean> <!-- userdao - HibernateDaoSupport类需要一个注入配制 || singleton 是在IOC容器里生成唯一的一个实列--> <bean id="userDao" class="accp.ssh.dao.Imple.UserDaoImple" scope="singleton"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- service实现层 属性UserDaoInterface dao的注入 --> <bean id="serviceUser" class="accp.ssh.serviceImple.UserServiceImple"> <property name="dao" ref="userDao"/> </bean> <!-- 配制saveSerivceUser.action 保存用户 注入属性业务层的userService接口--> <bean id="saveUsersAction" class="accp.ssh.action.users.SaveUsers" scope="prototype"> <property name="userService" ref="serviceUser"/> </bean> <!-- 配制查询所有用户, 注入业务层 得到实列 id=struts.action类class--> <bean id="listUserActiond" class="accp.ssh.action.users.ListUser"> <property name="userSevice" ref="serviceUser"/> </bean> <!-- 删除用户 --> <bean id="removeUserAction" class="accp.ssh.action.users.RemoveUser" scope="prototype"> <property name="serviceUser" ref="serviceUser"/> </bean> <!-- 更新用户 --> <bean id="updatePUserAction" class="accp.ssh.action.users.UpdatePUser" scope="prototype"> <property name="serviceUser" ref="serviceUser" /> </bean> <!-- 确认更新用户 --> <bean id="updateUseraction" class="accp.ssh.action.users.UpdateUser" scope="prototype"> <property name="userService" ref="serviceUser"></property> </bean> <!-- 下载文件 excel--> <bean id="dowloadUserDocAction" class="accp.ssh.action.users.DowloadUserDoc"> <property name="serviceUser" ref="serviceUser"/> </bean> </beans>
<?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> <include file="jqueryAction.xml"/> <package name="default" extends="struts-default"> <!-- class是spring注入的id名称 --> <action name="saveUser" class="saveUsersAction"> <result name="success" type="redirectAction">listUser</result> <result name="input">/saveUser.jsp</result> <result name="error">/err.jsp</result> </action> <action name="listUser" class="listUserActiond"> <result name="success" >listUser.jsp</result> </action> <!-- 删除用户 --> <action name="removeUser" class="removeUserAction"> <result name="success" type="redirectAction">listUser</result> </action> <!-- 更新用户 Page--> <action name="updatePUser" class="updatePUserAction"> <result>updateUser.jsp</result> </action> <!--确认更新用户 --> <action name="updateUser" class="updateUseraction"> <result name="success" type="redirectAction">listUser</result> <result name="input">updateUser.jsp</result> <result name="error">err.jsp</result> </action> <!-- 下载exec --> <action name="dowloadUserDoc" class="dowloadUserDocAction"> <result name="success" type="stream"> <!-- 指定下载文件配制 --> <!-- 配制文件类型为excel==xls --> <param name="contentType">application/vnd.ms-excel</param> <!-- 配制文件名 --> <param name="contentDisposition">filename="allUserInfo.xls"</param> <!-- 文件下载的方法名字 - 在action里的操作方法--> <param name="inputName">downloadUserXls</param> </result> </action> </package> </struts>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> <validators> <field name="user"> <!-- 访问类型 指定对另一个xml文件过行访问,对 XXX-user-validation的文件进行访问 ,并验证 --> <field-validator type="visitor"> <!-- 指定对那个对象验证 --> <param name="context" >user</param> <!--前面统一提示信息 --> <param name="appendPrefix">true</param> <message>update user s' </message> </field-validator> </field> </validators>
下载为xls方档
package accp.ssh.serviceImple; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.List; import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import accp.ssh.bean.Users3; import accp.ssh.dao.Imple.UserDaoImple; import accp.ssh.dao.Interface.UserDaoInterface; import accp.ssh.serviceInterface.UserServiceInteface; public class UserServiceImple implements UserServiceInteface { private UserDaoInterface dao; //在spring里面注入 public UserDaoInterface getDao() { return dao; } public void setDao(UserDaoInterface dao) { this.dao = dao; } @Override public void addUser(Users3 u) { dao.addUser(u); } @Override public List<Users3> getUserList() { return dao.getUserList(); } @Override public void removeUser(Users3 u) { dao.removeUser(u); } @Override public Users3 getUser(Integer id) { return dao.getUser(id); } @Override public void updateUser(Users3 u) { dao.updateUser(u); } /** * 下载用户文档并生成exec * HSSFWorkbook poi包对象 */ @Override public InputStream getUserDocStream() { HSSFWorkbook wb=new HSSFWorkbook();//创建exec对象 HSSFSheet sheet=wb.createSheet(); //创建一张工作表 HSSFRow row=sheet.createRow(0);//创建一行,从0开始 HSSFCell cell; //0 cell=row.createCell((short)0);//创建一个单元格,并放入数据 cell.setEncoding(HSSFCell.ENCODING_UTF_16);//指定编码 cell.setCellValue("序列"); //设值 //1 cell=row.createCell((short)1);//创建一个单元格,并放入数据 cell.setEncoding(HSSFCell.ENCODING_UTF_16);//指定编码 cell.setCellValue("ID号"); //设值 //2 cell=row.createCell((short)2); cell.setEncoding(cell.ENCODING_UTF_16); cell.setCellValue("姓"); //2 cell=row.createCell((short)3); cell.setEncoding(cell.ENCODING_UTF_16); cell.setCellValue("名"); //2 cell=row.createCell((short)4); cell.setEncoding(cell.ENCODING_UTF_16); cell.setCellValue("年龄"); //从数据库里取数据 List<Users3> listUser=dao.getUserList(); for(int i=0;i<listUser.size();++i){ Users3 user=listUser.get(i); row=sheet.createRow(i+1);//创建一行,从0开始 //0 cell=row.createCell((short)0); cell.setEncoding(cell.ENCODING_UTF_16); cell.setCellValue(i); //1 cell=row.createCell((short)1); cell.setEncoding(cell.ENCODING_UTF_16); cell.setCellValue(user.getId()); //2 cell=row.createCell((short)2); cell.setEncoding(cell.ENCODING_UTF_16); cell.setCellValue(user.getFirstName()); //3 cell=row.createCell((short)3); cell.setEncoding(cell.ENCODING_UTF_16); cell.setCellValue(user.getLastName()); //4 cell=row.createCell((short)4); cell.setEncoding(cell.ENCODING_UTF_16); cell.setCellValue(user.getAge()); } //方式一 以字节流的方式 不会产生监时文件,是在内存中加载 ByteArrayOutputStream os =new ByteArrayOutputStream();//字节数组输出流 try { wb.write(os);//写入字节输出流对象是 } catch (IOException e) { e.printStackTrace(); } byte[] bArry=os.toByteArray();//获取字节数组 //将数组字节 写入到输入流对象 InputStream isba=new ByteArrayInputStream(bArry); return isba; //办能获取一部分字节流,不可行 /* byte [] b=wb.getBytes();//获取文件字节流 //把字节数组写入到字节输入流对象 InputStream isb=new ByteArrayInputStream(b); return isb;*/ //方式二 /*//获得文件流 final File file=new File("userDoc.xls"); OutputStream os;//输出流 InputStream is = null;//输入流 try { os=new FileOutputStream(file); wb.write(os); //将wb文档 通过输出流 写入到File文件中 os.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //将file文件 ,写入输入文件流,并返回 try { is=new FileInputStream(file); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } //用线程删掉tomact服务器的File的临时文件 new Thread( new Runnable(){ public void run() { //线程休眠15秒 try { Thread.sleep(15000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } file.delete();//删除临时文件 } } ).start(); //如果服务器在此之间关闭,监时文件不会删除,永远在,这时要用一个servlet,init()去删除 return is;*/ } }