panda

          IT高薪不是夢!!

          統計

          留言簿

          閱讀排行榜

          評論排行榜

          2009年7月16日 #

          文件上傳(FileUpload)

          1.使用JAR
          ??????jsp文件上傳主要使用了兩個jar包,commons-fileupload-1.2.1.jar和commons-io-1.4.jar
          2.代碼實現
          ???? public class UploadServlet extends HttpServlet {

          ?/**
          ? *
          ? */
          ?private static final long serialVersionUID = 1L;

          ?private ServletContext sc;

          ?private String savePath;

          ?@Override
          ?protected void doGet(HttpServletRequest request,
          ???HttpServletResponse response) throws ServletException, IOException {
          ??doPost(request, response);
          ?}

          ?@Override
          ?protected void doPost(HttpServletRequest request,
          ???HttpServletResponse response) throws ServletException, IOException {

          ??System.out.println("請求進來了..........");

          ??// 設置請求的編碼
          ??request.setCharacterEncoding("UTF-8");

          ??DiskFileItemFactory factory = new DiskFileItemFactory();//創建一個磁盤文件工廠
          ??ServletFileUpload upload = new ServletFileUpload(factory);

          ??try {
          ???List items = upload.parseRequest(request);
          ???Iterator it = items.iterator();
          ???while (it.hasNext()) {
          ????FileItem item = (FileItem) it.next();

          ????if (item.isFormField()) {
          ?????System.out.println("表單的參數名稱:" + item.getFieldName()
          ???????+ ",對應的參數值:" + item.getString("UTF-8"));
          ????} else {
          ?????// 獲取文件擴展名
          ?????String strtype = item.getName().substring(
          ???????item.getName().length() - 3,
          ???????item.getName().length());
          ?????strtype = strtype.toLowerCase();

          ?????if (strtype == "jpg" || strtype == "gif"
          ???????|| strtype == "txt") {
          ??????if (item.getName() != null
          ????????&& !item.getName().equals("")) {
          ???????System.out.println("上傳文件的大小:" + item.getSize());
          ???????System.out.println("上傳文件的類型:"
          ?????????+ item.getContentType());
          ???????System.out.println("上傳文件的名稱:" + item.getName());

          ???????System.out.println("文件的擴展名" + strtype);
          ???????File tempFile = new File(item.getName());
          ???????File file = new File(
          ?????????sc.getRealPath("/") + savePath, tempFile
          ???????????.getName());
          ???????item.write(file);

          ???????request.setAttribute("upload.message", "上傳文件成功!");

          ??????} else {
          ???????request.setAttribute("upload.message",
          ?????????"沒有選擇上傳文件獲取格式不支持");
          ??????}
          ?????} else {
          ??????request.setAttribute("upload.message", "上傳文件格式不支持");
          ?????}
          ????}
          ???}
          ??} catch (Exception e) {
          ???e.printStackTrace();
          ???request.setAttribute("upload.message", "上傳文件不成功!");
          ??}
          ??// 轉發
          ??request.getRequestDispatcher("/uploadResult.jsp").forward(request,
          ????response);
          ?}

          ?@Override
          ?public void init(ServletConfig config) throws ServletException {

          ??savePath = config.getInitParameter("savePath");
          ??sc = config.getServletContext();
          ?}

          posted @ 2009-11-08 16:30 IT追求者 閱讀(170) | 評論 (0)編輯 收藏

          Hibernate的多對一關聯映射

          1.關聯映射的本質:就是將關聯關系映射到數據庫中,關聯關系指對象模型中的一個或多個引用.

          2.下面列舉多對一的示例:用戶和組(多個用戶屬于一個組)多對一關聯映射是最常用的一種關聯映射

          ?? *User 類
          ???package com.lzy
          ?? public class User{

          ?? private int id;
          ?? private String name;

          ? private Group group;//持有組的引用
          ???
          ?? public User(){};

          ?? //省略set,get方法
          ?}


          ? *Group類
          ?package com.lzy
          ?public class Group{
          ??
          ?? private int id;

          ?? private String name;
          ?? //省略get,set方法
          ?}

          3.對對象進行關系映射,這也是Hibernate中比較難的一點。
          ? (1)User.hbm.xml
          ??????
          ??????<?xml version="1.0">
          ??????<!DOCTYPE?hibernate-mapping PUBLIC? "-//Hibernate/Hibernate Mapping DTD 3.0//EN" http://hibernate.sourceforge.net/hibernate-mapping-3.0
          .dtd">
          ?? ? <hibernate-mapping package="com.lzy">
          ?????????<class name="User" table="t_user">
          ???????????????<id name="id" column="id">
          ?????????????????????<genarator class="native"/>
          ????????????? </id>
          ????????????<property name="name" column="user_name" not-null="true"/>
          ????????????<many-to-one name="group" column="groupid"/>
          ??????? </calss>
          ???? </hibernate-mapping>


          ?? (2)Group.hbm.xml
          ?????????
          ??????<?xml version="1.0">
          ??????<!DOCTYPE?hibernate-mapping PUBLIC? "-//Hibernate/Hibernate Mapping DTD 3.0//EN" http://hibernate.sourceforge.net/hibernate-mapping-3.0
          .dtd">
          ?? ? <hibernate-mapping package="com.lzy">
          ?????????<class name="Group" table="t_group">
          ???????????????<id name="id" column="id">
          ?????????????????????<genarator class="native"/>
          ????????????? </id>
          ????????????<property name="name" column="group_name" not-null="true"/>
          ??????</class>
          ???</hibernate-mapping>

          4.測試

          public class? Test {
          ???
          ? public static void main(String args[]){

          ??????SessionFactory? sessionFactory=null;
          ????? Session? session=null;
          ????? Transaction?? transaction=null;
          ??????
          ??????sessionFactory = HibernateUtil.getSessionFactory();// 創建一個會話工廠
          ????? session = sessionFactory.openSession();// 創建一個會話實例
          ????? transaction = session.beginTransaction();// 申明一個事務

          ??User user= new User();
          ??Group?group = new Group();

          ??user.setName("龍一");

          ??group.setName("中南大學");
          ? user.setGroup(group);

          ??try {

          ???transaction.begin();
          ???session.save(user);
          ???transaction.commit();

          ??} catch (Exception e) {
          ???e.printStackTrace();
          ??}


          ?? }
          }

          posted @ 2009-10-12 17:56 IT追求者 閱讀(1422) | 評論 (2)編輯 收藏

          struts logic標簽使用詳解

          Logic 比較標簽(一)

          1.<logic:equal>

          判斷變量是否與指定的常量相等。例如:

          ???? <%

          ?????? pageContext.setAttribute("test",new Integer(100));

          ???? %>

          ???? <logic:equal value="100" name="test">

          ???????? test=100???? </logic:equal>

          2.<logic:greaterThan>

          判斷常量變量是否與指定的常量不相等。

          <html:link page="/greaterThan.jsp?test=123456">添加參數</html:link> //傳值

          <logic:greaterThan value="12347" parameter="test">

          ?????? true

          </logic:greaterThan>

          下面類似

          3.<logic:greaterEqual>

          判斷變量大小是否等于指定的常量。

          4.<logic:lessThan>

          判斷變量是否小于指定的常量。

          5.<logic:lessEqual>

          判斷變量是否小于等于指定的常量。

          Struts logic標簽(二)

          循環遍歷標簽<logic:iterate>

          該標簽用于在頁面中創建一個循環,以次來遍歷數組、Collection、Map這樣的對象。在Struts中經常用到!

          例如:

          <%

          String []testArray={"str0","str1","str2","str3","str4","str5"};

          pageContext.setAttribute("test",testArray);

          %>

          <logic:iterate id="array" name="test">

          ??????? <bean:write name="array"/>

          </logic:iterate>

          首先定義了一個字符串數組,并為其初始化。接著,將該數組存入pageContext對象中,命名為test1。然后使用logic:iterate標記的name屬性指定了該數組,并使用id來引用它,同時使用bean;write標記來將其顯示出來。

          還可以通過length屬性指定輸出元素的個數,offset屬性指定從第幾個元素開始輸出。例如:

          <logic:iterate id="array1" name="test1" length="3" offset="2">

          <bean:write name="array1"/><br>

          </logic:iterate>

          Struts logic標簽(三)

          <logic:match>

          <logic:notmatch>

          該標簽用于判斷變量中是否或者是否不包含指定的常量字符串。例如:

          <%

          ??????? pageContext.setAttribute("test","helloWord");

          %>

          <logic:match value="hello" name="test">

          ?????? hello 在helloWord中

          </logic:match>

          Match標記還有一個重要屬性,就是location屬性。location屬性所能取的值只有兩個,一個是"start",另一個是"end"。例如:

          <logic:match value="hello" name="test" location="start">

          ????????? helloWord以 hello開頭

          </logic:match>

          Struts logic存在標簽(四)

          <logic:present>

          <logic:notpresent>

          <logic:messagePresent>

          <logic:messageNotPresent>

          <logic:present>和<logic:notpresent>標簽主要用于判斷所指定的對象是否存在;

          例如:

          <%

          pageContext.setAttribute("test","testString");

          %>

          <logic:present name="test">

          ??????? true??

          </logic:present>

          <logic:present>和<logic:notpresent>標記有以下幾個常用屬性:

          header屬性:???? 判斷是否存在header屬性所指定的header信息。

          parameter屬性:判斷是否存在parameter屬性指定的請求參數。

          cookie屬性:???? 判斷cookie屬性所指定的同名cookie對象是否存在。

          name屬性:?????? 判斷name屬性所指定的變量是否存在。

          property屬性:和name屬性同時使用,當name屬性所指定的變量是一個JavaBean時,判斷property屬性所指定的對象屬性是否存在。

          <%

          ?????? Cookie cookie=new Cookie("name","value");

          ?????? response.addCookie(cookie);

          %>

          <logic:present cookie="name">

          ??????? true

          </logic:present>

          <logic:messagePresent>和<logic:messageNotPresent>這兩個標記是來判斷是否在request內存在特定的ActionMessages或ActionErrors對象。它們有幾個常用的屬性:

          name屬性:???? 指定了ActionMessages在request對象內存儲時的key值。

          message屬性:message屬性有兩種取值。當其為true時,表示使用Globals.MESSAGE_KEY做為從request對象中獲取ActionMessages的key值,此時無論name指定什么都無效;當其為false時,則表示需要根據name屬性所指定的值做為從request對象中獲取ActionMessages的key

          值,倘若此時未設置name屬性的值,則使用默認的Globals.ERROR_KEY。

          property屬性:指定ActionMessages對象中某條特定消息的key值。

          例如:

          ????? <%

          ???????? ActionErrors errors = new ActionErrors();

          ???????? errors.add("totallylost", new ActionMessage("application.totally.lost"));

          ???????? request.setAttribute(Globals.ERROR_KEY, errors);

          ???????? request.setAttribute("myerrors", errors);

          ????? %>

          ???????? <logic:messagesPresent name="myerrors">

          ?????????? Yes

          ???????? </logic:messagesPresent>

          Struts logic判空標簽(五)

          <logic:empty>

          <logic:notEmpty>

          該標簽用于判斷指定的字符是否為空。

          例如:

          ????? <%

          ??????? pageContext.setAttribute("test","");

          ????? %>

          ??

          ???? <logic:empty name="test">

          ???????? test 為空

          ???? </logic:empty>

          Struts logic轉發和重定向標簽(六)

          1.<logic:foward>轉發標簽

          該標簽用于進行全局轉發,使用該標簽的頁面一般不再編寫其他內容,因為隨著轉發,頁面將跳轉,原頁面中的內容也沒有意義了。例如:

          ????? this is a test

          <logic:forward name="welcome"/>

          ???? this is a new test

          2.<logic:redirect>重定向標簽

          該標簽用于進行重定向。具有屬性:

          href屬性:???? 將頁面重定向到href指定的完整外部鏈接。

          page屬性:???? 該屬性指定一個本應用內的一個網頁,標記將頁面重定向到這個新的網頁。

          forward屬性:該屬性與struts-config.xml中的<global-forward>內的子項相對應。即將頁面重定向到forward所指定的資源。

          posted @ 2009-07-16 23:51 IT追求者 閱讀(519) | 評論 (0)編輯 收藏

          struts bean標簽使用詳解

          Bean 標簽庫
          ??????? 此標簽庫和Java Bean有很強的關聯性,設計的本意是要在JSP 和JavaBean 之間提供一個接口。Struts 提供了一套小巧有用的標簽庫來操縱JavaBean和相關的對象:cookie、 header、 parameter、 define、write、message、 include、page、resource、size、struts。
          -------------------------------
          -------------------------------
          bean:cookie、bean:header、bean:parameter
          這三個標簽用來重新得到cookie, request header和request parameter。
          bean:header和bean:parameter標簽定義了一個字符串;bean:cookie標簽定義了一個Cookie對象。你可以使用value屬性做為默認值。如果找不到指定的值,且默認值沒有設定的話,會拋出一個request time異常。如果你期望返回多個值的話,可把multiple屬性設為true。
          <bean:cookie id="sessionID" name="JSESSIONID" value="JSESSIONID-ISUNDEFINED"/>
          // 這段代碼定義了一個名為sessionID的腳本變量,如果找不到一個名為JSESSIONID的cookie,那sessionID


          下面代碼會輸出一些Cookie對象的一些屬性:
          <jsp:getProperty name="sessionID " property="comment"/> …
          <jsp:getProperty name="sessionID" property="domain"/> …
          <jsp:getProperty name="sessionID" property="maxAge"/> …
          <jsp:getProperty name="sessionID" property="path"/> …
          <jsp:getProperty name="sessionID" property="value"/> …
          <jsp:getProperty name="sessionID" property="version"/> …

          下面是在request中輸出所有header的例子:
          <%
          ????????? java.util.Enumeration names =((HttpServletRequest) request).getHeaderNames();
          %>

          <%
          ????????? while (names.hasMoreElements()) {
          ????????? String name = (String) names.nextElement();
          %>
          <bean:header id="head" name="<%= name %>"/>
          … <%= name %>
          … <%= head %>

          <%
          ????????? }
          %>

          下面是parameter的例子:
          <bean:parameter id="param1" name="param1"/>
          <bean:parameter id="param2" name="param2" multiple="true"/>??? // 此處定義了一個param2[]。
          <bean:parameter id="param3" name="param3" value="UNKNOWN VALUE"/>

          于其它標簽結合使用:
          <bean:header id="browser" name="User-Agent"/>
          <P>You are viewing this page with: <bean:write name="browser"/></P>
          ----------------------------------------------------------------------------------------------------------------------------------
          <bean:cookie id="username" name="UserName" scope="session"
          value="New User" />
          <P>Welcome <bean:write name="username" property="value"/!</P>
          ??? // 根據cookie創建一個新的Bean,如果用戶名稱已經存儲在cookie中,它就不顯示為一個新用戶。
          -------------------------------
          -------------------------------
          bean:define:有三個用途。
          一是定義新字符串常量:
          <bean:define id="foo" value="This is a new String"/>
          <bean:define id="bar" value='<%= "Hello, " + user.getName() %>'/>
          <bean:define id="last" scope="session" value='<%= request.getRequestURI() %>'/>
          二是復制一個現有的bean給新的bean:
          <bean:define id="foo" name="bar"/>???
          <bean:define id="baz" name="bop" type="com.mycompany.MyClass"/>??? //定義腳本變量的類型,默認為Object???
          三是復制一個現有的bean的屬性給新的bean:
          <bean:define id="bop" name="user" property="role[3].name"/>
          ??? <bean:define id="foo" name="bar" property="baz" scope="request"??? toScope="session"/>
          ??? //toScope屬性指新bean的scope,默認為page??
          上段代碼的意思是把名為bar的bean的baz屬性賦值給foo,foo的類型為String(默認)。
          -------------------------------
          -------------------------------
          bean:include
          這個標簽和bean:include標簽和相似,不同點就是它定義了一個可以復用的腳本變量。用id屬性命名一個新的腳本變量,還支持forward、href、page和transaction.屬性,和html:link中的屬性意義一樣。
          <bean:include id="footerSpacer"??? page="/long/path/footerSpacer.jsp"/>
          然后你能夠在多個地方(scope為page)調用:
          <bean:write name="footerSpacer" />

          -------------------------------
          -------------------------------
          bean:message
          用來實現對國際化的支持的一個標簽,配合java.util數據包中定義的Locale和ResourceBundle類來完成這個任務,用java.text.MessageFormat類配置消息的格式。
          ??? 首先要指定資源文件的名稱。這個文件會包含用默認語言編寫的在程序中會出現的所有消息,這些消息以“關鍵字-值”的形式存儲。文件需要存儲在類路徑下,路徑要作為初始化參數傳送給ActionServlet。
          ??? 實現國際化的規定:所有的資源文件必須都存儲在基本資源文件所在的目錄中。基本資源文件包含的是用默認地區語言-本地語言編寫的消息。如果基本資源文件的名稱是ApplicationResources.properties,那么用其他特定語言編寫的資源文件的名稱就應該是ApplicationResources_xx.properties(xx為ISO編碼,如英語是en)。因此這些文件應包含相同的關鍵字,但關鍵字的值是用特定語言編寫的。
          ??? 然后,ActionServlet的區域初始化參數必須與一個true值一起傳送,這樣ActionServlet就會在用戶會話中的Action.LOCALE_KEY關鍵字下存儲一個特定用戶計算機的區域對象。現在可以運行一個國際化的web站點,它可以根據用戶計算機上的設置的區域自動以相應的語言顯示。

          使用特定的字符串來替換部分消息:
          在資源文件中的定義:info.myKey = The numbers entered are {0},{1},{2},{3}
          標記的使用:<bean:message key="info.myKey" arg0="5" arg1="6" arg2="7" arg3="8"/>
          Jsp頁面的顯示:The numbers entered are 5,6,7,8??? // 最多支持4個參數
          -------------------------------

          -------------------------------
          ?? bean:page:把Jsp中的內部對象做為腳本變量。
          <bean:page id="requestObj" property="request"/>
          -------------------------------
          -------------------------------
          bean:resource:獲得應用程序的資源,這個資源可以是一個String或從java.io.InputStream中讀入。使用ServletContext.getResource()ServletContext.getResourceAsStream() 方法檢索web應用中的資源,如果在檢索資源時發生問題,就會產生一個ruquest time異常。
          <bean:resource id="webxml" name="/WEB-INF/web.xml"/>
          使用input屬性時,資源會做為一個InputStream,如果不指定就被當成一個String。

          -------------------------------
          -------------------------------
          bean:size:得到存儲在array、collection或map中的數目,類型為java.lang.Integer。
          <bean:size id="count" name="employees" />
          -------------------------------
          -------------------------------
          bean:struts:復制Struct 對象(三種類型)給新的bean,scope為page。
          <bean:struts id="form" formBean="CustomerForm"/>???
          <bean:struts id="fwd" forward="success"/>
          <bean:struts id="map" mapping="/saveCustomer"/>
          -------------------------------
          -------------------------------
          bean:write:以字符串形式輸出bean的屬性值。
          filter屬性:設為true時,將HTML保留字轉換為實體("<" 轉換為 &lt);
          ignore屬性:如果對象不存在,不會拋出異常。
          <bean:write name="userRegistration" property="email" scope="request"/>

          posted @ 2009-07-16 23:13 IT追求者 閱讀(513) | 評論 (0)編輯 收藏

          struts Html標簽使用詳解

          1.html:form 標簽
                 該標簽的用法必須遵循很多規則.
                  (1)首先標簽中必須包含一個action屬性,action屬性是該標簽唯一一個必須的屬性.如果沒有該屬性jsp會拋出一個異常,     之后你必須給
          action屬性一個有效的值,這個值也就是對應struts配置文件(struts-config.xml)中的action標簽中的type屬性值.
                    例如:jsp頁面中有:<html:form action="/login">
                             則struts-config.xml文件中:
                                                                            <action-mapping>
                                                                                  <action type="/login"
                                                                                                type="對應事件處理類的路徑"
                                                                                                name="對應的formbean"
                                                                                                 />
                                                                              </action-mapping>
                 (2)html:form標簽還必須遵循一個規則:
                                      任何包含在<form>中用來接收用戶輸入的標簽(<text>、<password>、<hidden>、<textarea>、<radio>、<checkbox>、<select>
          )必須在相關的form bean中有一個指定的屬性值.例如,如果你有一個屬性值被指定為“username”的<text>標簽,那么相關的form bean中也
          必須有一個名為“username”的屬性。輸入<text>標簽中的值會被用于生成form bean的userName屬性。

          2.html:text標簽
                    html:text標簽是一個生成文本框的標簽,它必須包含一個和form bean的相同屬性的對應的"property"屬性.該標簽只有嵌套在form中才有效.


          3.html:password標簽

                  html:password標簽是用來顯示口令的文本框,它也必須包含一個和form bean的相同屬性對應的"property"屬性,該標簽中的
                   一個很重要的屬性是“redisplay”,它用于重新顯示以前輸入到這個區域中的值。該屬性的缺省值為true。然而,為了
                 使password不能被重新顯示,你或許希望將該屬性的值設為false。
                  例如:<html:password property="password"/>


          4.html:hadden標簽
                    html:hadden是生成一個隱藏的輸入文本區域,它必須包含和相關form bean中的相同屬性對應的“property”屬性。該標簽也必須嵌套在form中才有效.
                    例如:<html:hadden property="value"/>


          5.<html:textarea>標簽 
                  <html:textarea>標簽主要是生成一個文本域,它必須包含和相關form bean中相同屬性對應的"property"屬性.該標簽也必須嵌套在form中才有效.
                  例如:<html:textarea property="content"
                                                    cols="10"
                                                    rows="5"/>

          6.<html:radio>標簽

                      <html:radio>標簽是用于顯示一個單選按鈕.它必須包含"value"值
                     例如:<html:radio property="title" value="1"/>1
                               <html:radio property="title" value="2"/>2
                               <html:radio property="title" value="3"/>3



          7.<html:checkbox>標簽


                  <html:checkbox>標簽用于顯示checkbox類型的輸入區域。比如: <html:checkbox property= 
             "notify">Please send me notification 


          8.<html:submit>標簽

                      <html:submit>標簽是用于提交按鈕.
                      例如:<html:submit value="提交"/>



          9.<html:reset>標簽

                      <html:reset>標簽主要是用于重置按鈕


          10.<html:option>標簽

                         <option>標簽用于顯示select box中的一個選項。參照下面的<select>標簽。 


          11.<html:select>標簽

                          <html:select>標簽主要是用于顯示下拉列表,它也必須嵌套在form中才有效
                          例如:
                                  <html:select  property="color" size="3">
                                                <html:option  value="red">red</html:option>
                                                <html:option value="green">green</html:option>
                                                 <html:option value="blue">blue</html:option>

           




                     



                      








           

          posted @ 2009-07-16 22:00 IT追求者 閱讀(1629) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 岐山县| 岳西县| 石阡县| 金寨县| 闽清县| 霍州市| 眉山市| 沿河| 合川市| 同江市| 确山县| 三原县| 贵南县| 榆社县| 保靖县| 浦县| 疏勒县| 元朗区| 西和县| 沙田区| 广西| 剑川县| 北流市| 黄山市| 涟水县| 道真| 韶关市| 宿州市| 同仁县| 桂东县| 元江| 昌邑市| 军事| 郴州市| 旬阳县| 赤峰市| 余干县| 四会市| 甘谷县| 柏乡县| 天长市|