struts开发实践—实用小贴士

 
一、 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("/") 方法 , 行的通,但是这个方法已经不被建议使用了。推荐使用 String path= this.servlet.getServletContext().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.       令牌机制
    3. 如果是带参数的跳转:
    ActionForward forward = mapping.findForward("success");
    StringBuffer bf = new StringBuffer(forward.getPath());
    bf.append("?id=1");// 参数
    return new ActionForward(bf.toString(),true);
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值