小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

struts開發(fā) 實踐—實用小貼士

 longssl 2006-01-13



 

一、struts使用小貼士(mainly from《struts in action》)
1. 在actionForm中使用array以匹配重復(fù)的變量。例如在使用multibox時,相對應(yīng)的可以在form中定義array。
2.使用<bean:size>顯示collection的記錄總數(shù)。
 eg:<bean:size id=”listSize” name=”resultList”>。如果resultList有兩條記錄,則顯示2。
3. 顯示循環(huán)序號標記:
 <logic:iterate id=”element” name=”list” indexed=”index”>
  <bean:write name=”index”/>
 </logic:iterate>
4.使用<logic:present></logic:present>檢查對象是否存在。使用<logic:notEmpty ></logic:notEmpty>檢查屬性是否存在。
5. 相對穩(wěn)定的下拉列表數(shù)據(jù)集使用scope=”application”。(這個方法一直想用,但是具體上仍不太清楚,是否在系統(tǒng)登陸的時候獲得這個數(shù)據(jù)集,以后就可以直接用了)。
6.使用<html:rewrite>設(shè)置css,js文件的路徑。(這一點看不出來與直接使用有什么區(qū)別)。
7.javascript與form值的交互:這一點使用很頻繁
 eg:< a href=’javascript:doDelete(<bean:write name=”testForm” property=”id”>)’>
8.使用DispatchAction將幾個相關(guān)的操作放在一起,例如將save,delete操作放在一個action中。DispatchAction的使用需要在struts-config.xml中的action-mapping中設(shè)置parameter,具體可以參考
9. 在使用javascript的頁面中,不能出現(xiàn)兩個Form對象的名稱相同。特別是submit按鈕我們常常會不寫name,缺省name都為submit,這是如果有onclick事件觸發(fā),就會出錯。
10. 幾個ActionForm公用的屬性,可以在ActionForm中定義一個子類。
 eg: Class BasicInfo  implement Serializable {};
      在ActionForm中需定義BasicInfo basicInfo = new BasicInfo();
     在jsp頁面中的property="basicInfo.a"
二、上傳文件
1. 在actionForm中需定義FormFile類型字段。eg:FormFile myphoto
2. 在jsp頁面中
 <form enctype="multipart/form-data">
 <html:file property="myphoto">
3.在submit以后可能會出現(xiàn)一下錯誤:no multipart request date sent”
解決方案:
 1.將struts-config.xml中redirect樹性置為true。
 2.如果是帶參數(shù)的跳轉(zhuǎn),采用以下方法:
 ActionForward forward = mapping.findForward("success");
 StringBuffer bf = new StringBuffer(forward.getPath());
 bf.append("?id=1");//參數(shù)
 return new ActionForward(bf.toString(),true);

三、圖片的顯示

1.寫一個顯示圖片的Action,代碼結(jié)構(gòu)如下:

public class PhotoAction extends Action {

  private static final String CONTENT_TYPE = "image/gif";

  public ActionForward perform(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)

  throws ServletException, IOException{

    response.setContentType(CONTENT_TYPE);

    ...//GET PARAM employeeId

    ServletOutputStream out = response.getOutputStream();

    InputStream in =null;

    try{

        in = ...//get blob pic

    }

     catch (Throwable e) {

        e.printStackTrace();

     }

   if(in!=null){

     try {

       int len;

       byte buf[] = new byte[4096];

       while ( (len = in.read(buf, 0, 4096)) != -1) {

         out.write(buf, 0, len);

       }

     }

     catch (IOException ioe) {

       ioe.printStackTrace();

     }

   }

    return null;

  }

}

2.在顯示圖片的jsp頁面寫如下代碼:

<html:img height="98%" alt="職工照片" src="photoAction.do" paramId="employeeId" paramName="ePhotoForm" paramProperty="employeeId" width="150" border="0" align="center" />

四、使ApplicationResources.properties支持中文顯示:

進入命令提示符頁面,進入jdk安裝路徑下的bin目錄,執(zhí)行一下命令:

native2ascII - encoding gb2312 ApplicationResources_ISO.properties(原文件) AllicationResources.properties(新生成文件)

即可在jsp頁面顯示中文。

但是,目前還有一點問題沒解決,就是在jsp頁面顯示的內(nèi)容為null error ,你出現(xiàn)過相同問題嗎?怎么解決的?

五:Action中的文件路徑問題:

在Action中需要調(diào)用一個xml文件,是用相對路徑在本地編譯沒有問題,但是一放在服務(wù)器上執(zhí)行就找不到文件路徑。找了很多解決方法,最后調(diào)用了request.getRealPath("/")方法,行的通,但是這個方法已經(jīng)不被建議使用了。你有更好的方法嗎?

六:jsp頁面顯示字符串數(shù)組:

<logic:iterate name="dateList" id="dateList" type="java.lang.String">

<bean:write name="dateList">

</logic:iterate>

七:如何定義自己的jsp文件發(fā)布路徑(windows2000環(huán)境下實踐通過):

打開tomcat目錄下conf文件夾下的server.xml文件,找到<!-Tomcat manager Context->標簽

在其<context >標簽后,添加自己的發(fā)布路徑,如下:

<context path="/mywebapps" docbase="D:/work" debug="0" reloadable="true"/>

重啟tomcat,訪問:/localhost:8080/mybwebapps/,ok!

八:如何解決偏僻難字的顯示問題:

使用大字符集,將所有jsp頁面字符集設(shè)置如下:<%@ page contentType="text/html;charset=GBK" %>注意:GBK一定得大寫。

九:頁面刷新數(shù)據(jù)重復(fù)提交問題:

解決的方法就是采用重定向。與二的解決方法相同:

    1.struts-config.xmlredirect屬性置為true。

    2.如果是帶參數(shù)的跳轉(zhuǎn):

    ActionForward forward = mapping.findForward("success");

    StringBuffer bf = new StringBuffer(forward.getPath());

    bf.append("?id=1");//參數(shù)

    return new ActionForward(bf.toString(),true); 

十:其他:

編程規(guī)范建議:

數(shù)據(jù)庫接口類命名:*DAO.JAVA

邏輯類命名:*BO.JAVA

ACTION命名:*Action.JAVA

ACTIONFORM命名:*Form.JAVA

然后一個功能模塊的文件放在一個包內(nèi)。 

都是一些比較初級的問題,但是希望能夠助你在學(xué)習(xí)struts之初少走點彎路,也希望對于文中的問題給予指點,:)。




    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多