一、struts使用小貼士(mainly from《struts in action》)
1. 在actionForm中使用array以匹配重復(fù)的變量。例如在使用multibox時(shí),相對(duì)應(yīng)的可以在form中定義array。
2.使用<bean:size>顯示collection的記錄總數(shù)。
eg:<bean:size id=”listSize” name=”resultList”>。如果resultList有兩條記錄,則顯示2。
3. 顯示循環(huán)序號(hào)標(biāo)記:
<logic:iterate id=”element” name=”list” indexed=”index”>
<bean:write name=”index”/>
</logic:iterate>
4.使用<logic:present></logic:present>檢查對(duì)象是否存在。使用<logic:notEmpty ></logic:notEmpty>檢查屬性是否存在。
5. 相對(duì)穩(wěn)定的下拉列表數(shù)據(jù)集使用scope=”application”。(這個(gè)方法一直想用,但是具體上仍不太清楚,是否在系統(tǒng)登陸的時(shí)候獲得這個(gè)數(shù)據(jù)集,以后就可以直接用了)。
6.使用<html:rewrite>設(shè)置css,js文件的路徑。(這一點(diǎn)看不出來(lái)與直接使用有什么區(qū)別)。
7.javascript與form值的交互:這一點(diǎn)使用很頻繁
eg:< a href=’javascript:doDelete(<bean:write name=”testForm” property=”id”>)’>
8.使用DispatchAction將幾個(gè)相關(guān)的操作放在一起,例如將save,delete操作放在一個(gè)action中。DispatchAction的使用需要在struts-config.xml中的action-mapping中設(shè)置parameter,具體可以參考
9. 在使用javascript的頁(yè)面中,不能出現(xiàn)兩個(gè)Form對(duì)象的名稱(chēng)相同。特別是submit按鈕我們常常會(huì)不寫(xiě)name,缺省name都為submit,這是如果有onclick事件觸發(fā),就會(huì)出錯(cuò)。
10. 幾個(gè)ActionForm公用的屬性,可以在ActionForm中定義一個(gè)子類(lèi)。
eg: Class BasicInfo implement Serializable {};
在ActionForm中需定義BasicInfo basicInfo = new BasicInfo();
在jsp頁(yè)面中的property="basicInfo.a"
二、上傳文件
1. 在actionForm中需定義FormFile類(lèi)型字段。eg:FormFile myphoto
2. 在jsp頁(yè)面中
<form enctype="multipart/form-data">
<html:file property="myphoto">
3.在submit以后可能會(huì)出現(xiàn)一下錯(cuò)誤:no multipart request date sent”
解決方案:
1.將struts-config.xml中redirect樹(shù)性置為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.寫(xiě)一個(gè)顯示圖片的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頁(yè)面寫(xiě)如下代碼:
<html:img height="98%" alt="職工照片" src="photoAction.do" paramId="employeeId" paramName="ePhotoForm" paramProperty="employeeId" width="150" border="0" align="center" />
四、使ApplicationResources.properties支持中文顯示:
進(jìn)入命令提示符頁(yè)面,進(jìn)入jdk安裝路徑下的bin目錄,執(zhí)行一下命令:
native2ascII - encoding gb2312 ApplicationResources_ISO.properties(原文件) AllicationResources.properties(新生成文件)
即可在jsp頁(yè)面顯示中文。
但是,目前還有一點(diǎn)問(wèn)題沒(méi)解決,就是在jsp頁(yè)面顯示的內(nèi)容為“null error ”,你出現(xiàn)過(guò)相同問(wèn)題嗎?怎么解決的?
五:Action中的文件路徑問(wèn)題:
在Action中需要調(diào)用一個(gè)xml文件,是用相對(duì)路徑在本地編譯沒(méi)有問(wèn)題,但是一放在服務(wù)器上執(zhí)行就找不到文件路徑。找了很多解決方法,最后調(diào)用了request.getRealPath("/")方法,行的通,但是這個(gè)方法已經(jīng)不被建議使用了。你有更好的方法嗎?
六:jsp頁(yè)面顯示字符串?dāng)?shù)組:
<logic:iterate name="dateList" id="dateList" type="java.lang.String">
<bean:write name="dateList">
</logic:iterate>
七:如何定義自己的jsp文件發(fā)布路徑(windows2000環(huán)境下實(shí)踐通過(guò)):
打開(kāi)tomcat目錄下conf文件夾下的server.xml文件,找到<!-Tomcat manager Context->標(biāo)簽
在其<context >標(biāo)簽后,添加自己的發(fā)布路徑,如下:
<context path="/mywebapps" docbase="D:/work" debug="0" reloadable="true"/>
重啟tomcat,訪問(wèn):/localhost:8080/mybwebapps/,ok!
八:如何解決偏僻難字的顯示問(wèn)題:
使用大字符集,將所有jsp頁(yè)面字符集設(shè)置如下:<%@ page contentType="text/html;charset=GBK" %>注意:GBK一定得大寫(xiě)。
九:頁(yè)面刷新數(shù)據(jù)重復(fù)提交問(wèn)題:
解決的方法就是采用重定向。與二的解決方法相同:
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);
十:其他:
編程規(guī)范建議:
數(shù)據(jù)庫(kù)接口類(lèi)命名:*DAO.JAVA
邏輯類(lèi)命名:*BO.JAVA
ACTION命名:*Action.JAVA
ACTIONFORM命名:*Form.JAVA
然后一個(gè)功能模塊的文件放在一個(gè)包內(nèi)。
都是一些比較初級(jí)的問(wèn)題,但是希望能夠助你在學(xué)習(xí)struts之初少走點(diǎn)彎路,也希望對(duì)于文中的問(wèn)題給予指點(diǎn),:)。