struts2驗證+攔截器+國際化+下載excle文件+struts2二級聯動+OGNL表達試+ssh整合\部分程式碼, 專案下載、

java的爪哇發表於2013-08-06

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>

spring.xml

<?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>

struts.xml

<?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>
	

update.xml

<?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;*/
	}

}


太多了!有興趣可以下載專案!

相關文章