隨筆-124  評論-49  文章-56  trackbacks-0
           
          1 jndi
          配置數所源
          <context:property-placeholder location="jdbc.properties"/>//引入資源文件
              
          <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
                      
          <property name="driverClassName" value="org.git.mm.mysql.Driver[${driverClassName}]"/>
                      
          <property name="url" value="jdbc:mysql://localhost:3306/database?useUnicode=true &amp; characterEncoding=UTF-8[${url}]"/>
                      
          <property name="username" value="root[${username}]"/>
                      
          <property name="password" value="root[${password}]"/>
                      
          <!-- 連接池啟動時的初始值勤 -->
                      
          <property name="initialSize" value="1[${initialSize}]"/>
                      
          <!-- 連接池的最大值 -->
                      
          <property name="maxActive" value="500[${maxActive}]"/>
                      
          <!-- 最大空閑值,當經過一個高峰時間后,連接池可以慢慢將已經用不到的連接釋放一部分,一直減少到maxIdle為止 -->
                      
          <property name="maxIdle" value="2[${maxIdle}]"/>
                      
          <!-- 最小空閑值,當連接池的連接數量少于閥值時,連接池就會申請一些連接,以免洪峰時來不及申請 -->
                      
          <property name="minIdle" value="1[${minIdle}]"/>
                  
          </bean>
          2 bean
          <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
                  
          <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
                  
          <property name="url" value="jdbc:sqlserver://127.0.0.1:1433;databaseName=somken"/>
                  
          <property name="username" value="sa"/>
                  
          <property name="password" value="123456"/>
           
          </bean>
              
              
          <bean id="sf" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
                  
          <property name="dataSource" ref="datasource"/>
                  
                  
          <property name="mappingResources">
                      
          <list>
                          
          <value>org/somken/entity/UserInfo.xml</value>
                      
          </list>
                  
          </property>
                  
                  
          <property name="hibernateProperties">
                      
          <props>
                          
          <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                          
          <prop key="hibernate.show_sql">true</prop>
                      
          </props>
                  
          </property>
              
          </bean>
          3 讀取hibernate.cfg.xml
          <bean id="sf" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
                    
          <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
          </bean>
          posted @ 2009-11-30 09:45 junly 閱讀(312) | 評論 (0)編輯 收藏
               摘要: IOC控制反轉:應用本身不負責依賴對象的創建及維護,依賴對象的創建及維護是由外部容器負責的。 依賴注入:在運行期,由外部容器動態地將依賴對象注入到組件中。 ----------------------------------------------------------------------------- 第一個項目 1    spring的依賴庫 &n...  閱讀全文
          posted @ 2009-11-30 08:58 junly 閱讀(354) | 評論 (0)編輯 收藏
          AOP
               摘要: 靜態代理 通過接口實現 動態代理 1 編寫java代理類 public class SecurityHandler implements InvocationHandler {     private Object targetObject;    ...  閱讀全文
          posted @ 2009-11-30 08:36 junly 閱讀(351) | 評論 (0)編輯 收藏
          sql
          package org.epet.dao.impl;

          import java.sql.Connection;
          import java.sql.DriverManager;
          import java.sql.PreparedStatement;
          import java.sql.ResultSet;
          import java.sql.SQLException;

          import javax.naming.Context;
          import javax.naming.InitialContext;
          import javax.naming.NamingException;
          import javax.sql.DataSource;

          import com.sun.java_cup.internal.internal_error;

          public abstract class BaseDAO {
              
          private static final String DRIVER_CLASS = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
              
          private static final String DATABASE_URL = "jdbc:sqlserver://localhost:1433;DatabaseName=epet";
              
          private static final String DATABASE_USER = "sa";
              
          private static final String DATABASE_PASSWORD = "accp";
              
              
          /**
               * 返回連接
               * 
          @return
               
          */

              
          public static Connection getConnection() {
                  Connection connection
          =null;
                  
          try {
                      Class.forName(DRIVER_CLASS);
                      connection 
          = DriverManager.getConnection(DATABASE_URL,
                              DATABASE_USER, DATABASE_PASSWORD);
          //                Context tx=new InitialContext();
          //                DataSource ds=(DataSource)tx.lookup("java:comp/env/food");
          //                connection=ds.getConnection();

                  }
           catch (SQLException e) {
                      e.printStackTrace();
                  }
           catch (ClassNotFoundException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }

                  
          return connection;
              }

              
          /**
               * 查詢
               * 
          @param sql
               * 
          @return
               
          */

              
          public static ResultSet getDate(String sql){
                  Connection connection
          =getConnection();
                  ResultSet resultSet
          =null;
                  
          try {
                      PreparedStatement preparedStatement
          =connection.prepareStatement(sql);
                      resultSet
          =preparedStatement.executeQuery();
                  }
           catch (SQLException e) {
                      e.printStackTrace();
                  }

                  
          return resultSet;
              }

              
              
          public static int dele(String sql,int id){
                  
          int result=0;
                  Connection connection
          =getConnection();
                  
          try {
                      PreparedStatement preparedStatement
          =connection.prepareStatement(sql);
                      preparedStatement.setInt(
          1, id);
                      result
          =preparedStatement.executeUpdate();
                  }
           catch (SQLException e) {
                      e.printStackTrace();
                  }

                  
          return result;
              }

          }

          mysql:
          /*show databases;
          create database aa;
          use aa;
          show tables;
          select * from userinfo limit 1,2;
          -----------------------------------------
          */

          public connection getConnection () throws SQLException{
            Class.forName(
          "com.mysql.jdbc.Driver");
            String url
          ="jdbc:mysql://127.0.1:3306/somken(數據庫名)";
            
          return DriverManager.getConnection(url,"root","root");
          }
          posted @ 2009-11-30 08:21 junly 閱讀(528) | 評論 (0)編輯 收藏

          1 自定義異常類 SystemException.java

          public class SystemException extends RuntimeException{
           //自定義key
           private String key;
           //自定義參數
           private Object[] values;

           //實現父類的構造方法
           public SystemException() {
            super();
           }

           public SystemException(String message, Throwable cause) {
            super(message, cause);
           }


           public SystemException(String message) {
            super(message);
           }


           public SystemException(Throwable cause) {
            super(cause);
           }

           //自定義構造方法
           public SystemException(String message, String key) {
            super(message);
            this.key=key;
           }

           //自定義構造方法,帶一個參數
           public SystemException(String message, String key,Object value) {
            super(message);
            this.key=key;
            this.values=new Object[]{value};
           }
           
           //自定義構造方法,帶多個參數
           public SystemException(String message, String key,Object[] values) {
            super(message);
            this.key=key;
            this.values=values;
           }
           
           //相應的get方法
           public String getKey() {
            return key;
           }

           public Object[] getValues() {
            return values;
           }
          }
          2 自定義異常處理器 SystemExceptionHandler.java

          //作用:截獲SystemException,并根據SystemException中的信息動態創建ActionMessage等這些錯誤信息,
                  將其存在request中
          public class SystemExceptionHandler extends ExceptionHandler{

           /**
            * 處理SystemException異常
            */
           @Override
           public ActionForward execute(Exception ex,//拋出的異常
                   ExceptionConfig config,//struts-config.xml中的配置信息
                   ActionMapping mapping,
                    ActionForm form,
                   HttpServletRequest request,
                   HttpServletResponse response) throws ServletException {
            
            ActionForward forward=null;
            //創建ActionForward
            if(config.getPath()!=null){
             //有path屬性,則根據path創建
             forward=new ActionForward(config.getPath());
            }else {
             //沒有path屬性,則根據input屬性創建
             forward=mapping.getInputForward();
            }
            if(ex instanceof SystemException){
             SystemException se=(SystemException)ex;
             //key可有可無,所以取出key進行判斷
             String key=se.getKey();
             ActionMessage error=null;
             //如果自定義的key為空,用struts的
             if(key==null){
              //拿出error.default和message,創建ActionMessage對象
              error=new ActionMessage(config.getKey(),se.getMessage());
             }else {
              //如果自定義的key有值
              if(se.getValues()!=null){
               error=new ActionMessage(key,se.getValues());
              }else {
               //如果自定義的key有值,則根據key創建ActionMessage對象
               error=new ActionMessage(key);
              }
             }
             //將這個ActionMessage放到request中。key為自定義的,error為ActionMessage對象
             //forward是要轉到什么地方,根據path屬性創建。"request"為scope的一個,也可以
             //用config.getScope()
             this.storeException(request, key, error, forward, config.getScope());
             return forward;
            }
            return super.execute(ex, config, mapping, form, request, response);
           }
          }


          3 編寫配置文件 struts-config.xml

          <global-exceptions>
             <exception key="error.default"
                  type="java.lang.Exception"
                  scope="request"
                  path="/common/exception.jsp"
              <!-- 自定義的異常處理類 -->
                  handler="org.oa.common.SystemExceptionHandler"/>
          </global-exceptions>

          4 編寫資源文件 ApplicationResources.properties

          error.default={0}
          error.org.del=Can't Del Orgnation,id is {0}!

          5 業務代碼

          throw new org.oa.common.SystemException("存在子機構,不允許刪除!","error.org.del",org.getOname());

          posted @ 2009-11-30 08:17 junly 閱讀(505) | 評論 (0)編輯 收藏
          框架
          面向請求驅動:
          struts1.x,struts2.x,WebWork
          面向事件驅動的(JSF)
          --------------------------------------
          struts空字段測試
          <input type="text" name="username"\>
          ActionForm中有:
          private String username;
          private String password;
          頁面取得值:
          <%=form.getUserName()%>//結果:
          <%=form.getPassWord()%>//結果:null
          ${requestScope.userActionForm.username}//結果:
          ${requestScope.userActionForm.password}//結果:
          ----------------------------------------------------
          java國際化
          1 了解缺省Locale是由操作系統決定的,Locale是由語言和國家代碼組成
          2 國際化資源文件是由baseName+locale組成,一般在src目錄下就可以了,如:MessageBundle_en_US.properties
          baseName是任意合法的文件名
          3 native2ascii命令的位置和用法
            * 位置:java_home/bin
            * 使用native2ascii.exe o.properties MessagesBundle_zh_CN.properties
            * DOS
              D:\>native2ascii -encoding gb2312 aaa.txt bbb.txt
          ------------------------------------------------------------------
          struts國際化
          1 在struts-config.xml文件中加入:<message-resources parameter="MessageResources"/>
            注意:文件默認放在根src下,如入在其他目錄下.
            如:res目錄下用"."連接<message-resources parameter="res.MessageResources"/>
          2 提供不同版本的國際化文件,中文需要采用native2ascii轉換成unicode
          MessageResources_en_US.properties文件
          user.login.success={0},Login Success
          user.title=User Login
          user.name=User Name
          user.password=Password
          user.button.login=Login
          MessageResources_zh_CN.properties文件
          user.login.success={0},\u767b\u5f55\u6210\u529f
          user.title=\u7528\u6237\u767b\u5f55
          user.name=\u7528\u6237\
          user.password=\u5bc6\u7801
          user.button.login=\u767b\u5f55
          3 在jsp中采用<bean:message>標簽讀取國際化文本
          <titel><bean:message key="user.title"/></title>
          <bean:message key="user.name"/>:<input type="text" name="username"/>
          <bean:message key="user.password"/>:<input type="password" name="password"/>
          <input type="submit" value="<bean:message key="user.name"/>"/>
          4 動態設置locale
          Locale currentLocale=Locale.getDefalut();得到Locale
          currentLocale=new Loale("zh","CN");//建立Locale
          currentLocale=new Loale("en","US");//建立Locale
          request.getSession().setAttribute(Globals.LOCALE_KEY,currentLocale);
          this.setLocale(request,currentLocale);//效果同上
          5 動態設置message
            * 創建messages集合
            ActionMessages messages=new ActionMessages();
            * 創建國際化消息文本
           public ActionMessage(String key,Object value)
           ActionMessage message=new ActionMessage("user.login.success",username);//只不清一個參數
           //ActionMessage message=new ActionMessage("user.login.success",new Object[]{username});//多個參數
           messages.add("loginSuccess1",message);
           * 傳遞國際化消息文本
           this.saveMessages(request,messages);
           錯誤信息傳遞使用:this.saveErrors(request,messages);
           * 頁面輸出
           通過<html:message>標簽顯示消息(可以顯示普通消息和錯誤消息)
           通過<html:errors>標簽顯示消息(只能顯示錯誤消息)
           <html:messages id="msg" message="true">//html:messages標記與ActionMessages messages集合對應
            <bean:write name="msg"/>
           <html:messages>
           <html:messages id="msg" message="true" property="loginSuccess1">
            <bean:write name="msg"/>
           <html:messages>
          -------------------------------------------------------------------
          JSTL國際化
          <fmt:setLocale vlaue="${header['accept-language']}"/>設置locale
          <fmt:setBundle basename="res.MessageResources"/>//設置資源文件
          <fmt:message key="user.username"/>
          ---------------------------------------------------------------------
          struts的路徑與模式匹配
          posted @ 2009-11-30 08:16 junly 閱讀(358) | 評論 (0)編輯 收藏
          1 建立頁面
          <form action="" name="form1"  enctype="multipart/form-data" method="post">
          2 建ActionForm類
          private String title;
          private FormFile file;//必須用apache的FormFile類(org.apache.struts.upload.FormFile)
          3 建立Action類
          UplodaActionForm uaf=(UplodaActionForm)form;
          Systyem.out.println("title:"+uaf.getTitle());
          FormFile file=uaf.getFile();
          if(file!=null)
          {
           System.out.println("fileName:"+file.getFileName());
           FileOutputStream fos=new FileOutputStream("c:\\"+file.getFileName());
           fos.write(file.getFileData());
           fos.flush();
           fos.close();
          }
          return mapping.findForward("sessucc");
          4 配置ActionForm和Action
          <controller MaxFileSize="10M"></controller>該屬性可以配置上傳文件的大小等相關信息
          posted @ 2009-11-30 08:16 junly 閱讀(426) | 評論 (0)編輯 收藏
          1 ActionForm中的validate方法驗證
          1)重寫ActionForm中的validate方法
          public ActionErrors validate(ActionMapping mapping,HttpServletRequest request){
           ActionErrors errors=new ActionErrors ();
            if(username==null || username.length()<0){
            errors.add("unameisnull",new ActionMessgae("error.validate.unull"));
           }
           return errors;
          }
          2)資源文件ApplicationResources.properties
          error.validate.unull=usernaem is null
          3)在config.xml的<action-mappings>中<action>加入validate="true"屬性
          4)頁面標簽
          <html:errors/>
          ----------------------------------------------------
          validate驗證框架
          1 創建form繼承ValidatorForm或用DynaValidatorForm
          2 在WEB-INF下創建validator-rules.xml和validation.xml
          3 在src下創建資源文件ApplicationResources.properties
          4 要struts-config.xml中配
          <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
              <set-property property="pathnames"
                                 value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
          </plug-in>
          5 編寫validation.xml文件
          <formset>
           <form name="userForm">
             <field property="username"  depends="required">
             <arg key="lable.username"/>
             </field>
           </form>
          </formset>
          6 頁面使用<html:errors/>標簽
          posted @ 2009-11-30 08:15 junly 閱讀(488) | 評論 (0)編輯 收藏

          struts標簽的使用和配置
          配置:
          1 在struts-config.xml文件中加入(可以到示例中的struts-config.xml文件中復制)
            <message-resources parameter="MessageResources" />
          2 在示例的src下拷貝MessageResources.properties文件到項目src下
          3 在頁面引入就可使用
            <%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
          說明:第1,2步為struts的國際化資源束文件引入,第3為標簽引入
          -----------------------------------------------------------------------------
          Bean標記
          bean:define
          從已有的變量或者變量的屬性來定義一個新的變量。
          <bean:define id="新定義的變量名" scope="原變量的scope" name="原變量名" toScope="新定義變量的scope"></bean:define>
          <bean:define id="新定義的變量名" scope="原變量的scope" name="原變量名" property="原變量的屬性名" toScope="新定義變量的scope"></bean:define>
          bean:write
          <bean:write scope="變量的scope" name="變量名" property="變量的屬性名" filter="是否按html格式輸出(默認true為文本輸出)" format="數字(###,###.0000)日期(yyyy-MM-dd HH:mm:ss)"/>
          結構數據中多重屬性可用"."作導航取出來
          bean:message
          相當于jstl中<fmt:message>(國際化標簽)
          1 定義資源文件
          com.itcast.ApplicationResources.properties
          com.itcast.ApplicationResources_zh_CN.properties
          2 在struts-config中添加:
          <message-resources parameter="ApplicationResources" key="myKey" />
          3 頁面中使用
          <bean:message bundle="myKey" key="userName"/>
          <bean:message bundle="myKey" key="password"/>
          bean:size標簽
          --------------------------------------------------------------
          logic標記
          logic:empty/login:notEmpty 判斷對象是否為空
          <logic:empty name="對象名" property="屬性名" scope="對象的scope">
           為空<br>
          </logic:empty>
          logic:notEmpty 判斷對象是否不為空
          <logic:notEmpty name="對象名" property="屬性名" scope="對象的scope">
           不為空<br>
          </logic:notEmpty>
          logic:present 判斷對象是否存在(用方同上)
          logic:notPresent
          ----------------------------------------------||
          例子:
          request.setAtrribute("attr1",null);
          request.setAtrribute("attr2","");
          request.setAtrribute("attr3",new ArrayList());
          <logic:empty name="attr1">
           11<br>
          </logic:empty>
          <logic:notEmpty name="attr1">
           12<br>
          </logic:notEmpty>
          <logic:present name="attr1">
           13<br>
          </logic:present>
          <logic:notPresent name="attr1">
           14<br>
          </logic:notPresent>

          <logic:empty name="attr2">
           21<br>
          </logic:empty>
          <logic:notEmpty name="attr2">
           22<br>
          </logic:notEmpty>
          <logic:present name="attr2">
           23<br>
          </logic:present>
          <logic:notPresent name="attr2">
           24<br>
          </logic:notPresent>

          <logic:empty name="attr3">
           31<br>
          </logic:empty>
          <logic:notEmpty name="attr3">
           32<br>
          </logic:notEmpty>
          <logic:present name="attr3">
           33<br>
          </logic:present>
          <logic:notPresent name="attr3">
           34<br>
          </logic:notPresent>
          結果:
          11空
          14不存在

          21空
          23存在

          31空
          33存在
          -----------------------------------------||
          html:equal/html:notEqual
          html:greaterEqual大于等于
          html:greaterThan大于
          html:lessEqual小于等于
          html:lessThan小于
          -----------------------------------------||
          logic:iterate(循環)
          name:對應的bean,是一個集合類型
          property:對應的集合類型的屬性
          scope:變量的scope
          id:臨時變量名
          offset:循環起始位置
          indexId:集合中當前無素的下標
          length:控制長度
          單循環
          <logic:iterate id="username" scope="request" name="對應的bean,是一個集合類型">
               output every username:${username }<br>
          </logic:iterate>
          雙循環
          <logic:iterate id="user" scope="request" name="list" offset="2" indexId="i">
               ${user.username }<br>
               <logic:iterate id="love" name="user" property="loves">
                ${love }
               </logic:iterate><br>
          </logic:iterate><br>

          logic:
          tiles標記

          ----------------------------------------------------------------
          html標簽
          <html:form action="/login" method="post">
          username:<html:text property="username" value="123"/>
          password:<html:password property="password"/>
          sex:<html:radio property="sex" value="0" />男
              <html:radio property="sex" value="1" />女
          likes:<html:checkbox property="0" value="0">吃飯</html:checkbox>
                <html:checkbox property="0" value="1">吃飯</html:checkbox>
          xueli:<html:select property="xueli">
                   <html:option value="0">小學</html:option>
                   <html:option value="1">小學</html:option>
            <html:optionsCollection property="qxlist" label="qx" value="qxid"/>
            //<html:optionsCollection name="qxlist" label="qx" value="qxid"/>

                   1.
                </html:select>
          <html:submit value="提交"/>
          </html:form>

          posted @ 2009-11-30 08:14 junly 閱讀(682) | 評論 (0)編輯 收藏
          struts validator驗證框架
          1 配置:
              * 加入國際化配置在struts-config.xml文件中,如:
              <message-resources parameter="MessageResources"/>
              * 提供國際化資源文件
              * 引入validator插件在struts-config.xml文件中,如:
              <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
                <set=property
                 property="pathnames"
                 value="WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"
                />
              </plug-in>
              * 提供validator.xml和validator_rules.xml文件,將此文件拷貝到WEB-INF下
          2 validator服務器驗證
              * 配置validation.xml文件
          3 validator客戶驗證(javascript)
              * 配置validation.xml文件
              * 在jsp頁面中包含<html:javascript>
              * 對需要驗證的表單定義onsubmit事件,其中事件名稱為validate+ActionForm的名稱,如:validateLogin
          posted @ 2009-11-30 08:13 junly 閱讀(311) | 評論 (0)編輯 收藏
          僅列出標題
          共18頁: First 上一頁 2 3 4 5 6 7 8 9 10 下一頁 Last 
          主站蜘蛛池模板: 桐梓县| 永善县| 满城县| 安阳市| 英德市| 阳东县| 邓州市| 合阳县| 汝阳县| 晴隆县| 平利县| 庆云县| 盐池县| 宜川县| 秦安县| 山西省| 和静县| 蒙自县| 刚察县| 同心县| 甘泉县| 金溪县| 安远县| 方山县| 德令哈市| 无极县| 绵竹市| 淄博市| 玛纳斯县| 神木县| 辽中县| 山阳县| 丽水市| 江安县| 阿克苏市| 井冈山市| 邛崃市| 南岸区| 长寿区| 甘洛县| 彝良县|