| 
三、图片的显示
| struts开发实践—实用小贴士(上) |  
|  |  
|  |  
| 作者:中国论坛网收集 来源:http://www.51one.net 加入时间:2004-8-25   |  
| 一、struts使用小贴士(mainly from《struts in action》) 1. 在actionForm中使用array以匹配重复的变量。例如在使用multibox时,相对应的可以在form中定义array。
 2.使用<bean:size>显示collection的记录总数。
 eg:<bean:size id=”listSize” name=”resultList”>。如果resultList有两条记录,则显示2。
 3. 显示循环序号标记:
 <logic:iterate id=”element” name=”list” indexed=”index”>
 <bean:write name=”index”/>
 </logic:iterate>
 4.使用<logic:present></logic:present>检查对象是否存在。使用<logic:notEmpty ></logic:notEmpty>检查属性是否存在。
 5. 相对稳定的下拉列表数据集使用scope=”application”。(这个方法一直想用,但是具体上仍不太清楚,是否在系统登陆的时候获得这个数据集,以后就可以直接用了)。
 6.使用<html:rewrite>设置css,js文件的路径。(这一点看不出来与直接使用有什么区别)。
 7.javascript与form值的交互:这一点使用很频繁
 eg:< a href=’javascript:doDelete(<bean:write name=”testForm” property=”id”>)’>
 8.使用DispatchAction将几个相关的操作放在一起,例如将save,delete操作放在一个action中。DispatchAction的使用需要在struts-config.xml中的action-mapping中设置parameter,具体可以参考
 9. 在使用javascript的页面中,不能出现两个Form对象的名称相同。特别是submit按钮我们常常会不写name,缺省name都为submit,这是如果有onclick事件触发,就会出错。
 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以后可能会出现一下错误:no multipart request date sent”
 解决方案:
 1.将struts-config.xml中redirect树性置为true。
 2.如果是带参数的跳转,采用以下方法:
 ActionForward forward = mapping.findForward("success");
 StringBuffer bf = new StringBuffer(forward.getPath());
 bf.append("?id=1");//参数
 return new ActionForward(bf.toString(),true);
 
 |  1.写一个显示图片的Action,代码结构如下: 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目录,执行一下命令: native2ascII - encoding gb2312 ApplicationResources_ISO.properties(原文件) AllicationResources.properties(新生成文件) 即可在jsp页面显示中文。 但是,目前还有一点问题没解决,就是在jsp页面显示的内容为“null error ”,你出现过相同问题吗?怎么解决的? 五:Action中的文件路径问题: 在Action中需要调用一个xml文件,是用相对路径在本地编译没有问题,但是一放在服务器上执行就找不到文件路径。找了很多解决方法,最后调用了request.getRealPath("/")方法,行的通,但是这个方法已经不被建议使用了。你有更好的方法吗? 六:jsp页面显示字符串数组: <logic:iterate name="dateList" id="dateList" type="java.lang.String"> <bean:write name="dateList"> </logic:iterate> 七:如何定义自己的jsp文件发布路径(windows2000环境下实践通过): 打开tomcat目录下conf文件夹下的server.xml文件,找到<!-Tomcat manager Context->标签 在其<context >标签后,添加自己的发布路径,如下: <context path="/mywebapps" docbase="D:/work" debug="0" reloadable="true"/> 重启tomcat,访问:/localhost:8080/mybwebapps/,ok! 八:如何解决偏僻难字的显示问题: 使用大字符集,将所有jsp页面字符集设置如下:<%@ page contentType="text/html;charset=GBK" %>注意:GBK一定得大写。 九:页面刷新数据重复提交问题: 解决的方法就是采用重定向。与二的解决方法相同:     1.将struts-config.xml中redirect属性置为true。     2.如果是带参数的跳转:     ActionForward forward = mapping.findForward("success");     StringBuffer bf = new StringBuffer(forward.getPath());     bf.append("?id=1");//参数     return new ActionForward(bf.toString(),true);  十:其他: 编程规范建议: 数据库接口类命名:*DAO.JAVA 逻辑类命名:*BO.JAVA ACTION命名:*Action.JAVA ACTIONFORM命名:*Form.JAVA 然后一个功能模块的文件放在一个包内。  都是一些比较初级的问题,但是希望能够助你在学习struts之初少走点弯路,也希望对于文中的问题给予指点,:)。   |