struts开发实践—实用小贴士

一、 struts使用小贴士(mainly from《struts in action》)

1. 在actionForm中使用array以匹配重复的变量。例如在使用multibox时,相对应的可以在form中定义array。

2.使用

  1<bean:size>显示collection的记录总数。 
  2
  3eg:<bean:size id="”listSize”" name="”resultList”">。如果resultList有两条记录,则显示2。 
  4
  53\. 显示循环序号标记: 
  6
  7<logic:iterate id="”element”" indexed="”index”" name="”list”">
  8<bean:write name="”index”/">
  9</bean:write></logic:iterate>
 10
 114.使用<logic:present></logic:present>检查对象是否存在。使用<logic:notempty></logic:notempty>检查属性是否存在。 
 12
 135\. 相对稳定的下拉列表数据集使用scope=”application”。(这个方法一直想用,但是具体上仍不太清楚,是否在系统登陆的时候获得这个数据集,以后就可以直接用了)。 
 14
 156.使用<html:rewrite>设置css,js文件的路径。(这一点看不出来与直接使用有什么区别)。 
 16
 177.javascript与form值的交互:这一点使用很频繁 
 18
 19eg:&lt; a href=’javascript:doDelete(<bean:write name="”testForm”" property="”id”">)’&gt;
 20
 218.使用DispatchAction将几个相关的操作放在一起,例如将save,delete操作放在一个action中。DispatchAction的使用需要在struts-config.xml中的action-mapping中设置parameter,具体可以参考 
 22
 239\. 在使用javascript的页面中,不能出现两个Form对象的名称相同。特别是submit按钮我们常常会不写name,缺省name都为submit,这是如果有onclick事件触发,就会出错。 
 24
 2510\. 几个ActionForm公用的属性,可以在ActionForm中定义一个子类。 
 26
 27eg: Class BasicInfo implement Serializable {}; 
 28
 29在ActionForm中需定义BasicInfo basicInfo = new BasicInfo(); 
 30
 31在jsp页面中的property="basicInfo.a" 
 32
 33**二、** **上传文件**
 34
 351\. 在actionForm中需定义FormFile类型字段。eg:FormFile myphoto 
 36
 372\. 在jsp页面中 
 38
 39<form enctype="multipart/form-data">
 40<html:file property="myphoto">
 41
 423.在submit以后可能会出现一下错误:no multipart request date sent” 
 43
 44解决方案: 
 45
 461.将struts-config.xml中redirect树性置为true。 
 47
 482.如果是带参数的跳转,采用以下方法: 
 49
 50ActionForward forward = mapping.findForward("success"); 
 51
 52StringBuffer bf = new StringBuffer(forward.getPath()); 
 53
 54bf.append("?id=1");//参数 
 55
 56return new ActionForward(bf.toString(),true); 
 57
 58**三、** **图片的显示** ****
 59
 601.写一个显示图片的Action,代码结构如下: 
 61
 62public class PhotoAction extends Action { 
 63
 64private static final String CONTENT_TYPE = "image/gif"; 
 65
 66public ActionForward perform(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) 
 67
 68throws ServletException, IOException{ 
 69
 70response.setContentType(CONTENT_TYPE); 
 71
 72...//GET PARAM employeeId 
 73
 74ServletOutputStream out = response.getOutputStream(); 
 75
 76InputStream in =null; 
 77
 78try{ 
 79
 80in = ...//get blob pic 
 81
 82} 
 83
 84catch (Throwable e) { 
 85
 86e.printStackTrace(); 
 87
 88} 
 89
 90if(in!=null){ 
 91
 92try { 
 93
 94int len; 
 95
 96byte buf[] = new byte[4096]; 
 97
 98while ( (len = in.read(buf, 0, 4096)) != -1) { 
 99
100out.write(buf, 0, len); 
101
102} 
103
104} 
105
106catch (IOException ioe) { 
107
108ioe.printStackTrace(); 
109
110} 
111
112} 
113
114return null; 
115
116} 
117
118} 
119
1202.在显示图片的jsp页面写如下代码: 
121
122<html:img align="center" alt="职工照片" border="0" height="98%" paramid="employeeId" paramname="ePhotoForm" paramproperty="employeeId" src="photoAction.do" width="150"></html:img>
123
124**四、使ApplicationResources.properties支持中文显示:**
125
126进入命令提示符页面,进入jdk安装路径下的bin目录,执行一下命令: 
127
128native2ascII - encoding gb2312 ApplicationResources_ISO.properties(原文件) AllicationResources.properties(新生成文件) 
129
130即可在jsp页面显示中文。 
131
132但是,目前还有一点问题没解决,就是在jsp页面显示的内容为“null error ”,你出现过相同问题吗?怎么解决的? 
133
134**五:Action中的文件路径问题:**
135
136在Action中需要调用一个xml文件,是用相对路径在本地编译没有问题,但是一放在服务器上执行就找不到文件路径。找了很多解决方法,最后调用了request.getRealPath("/")方法,行的通,但是这个方法已经不被建议使用了。你有更好的方法吗? 
137
138**六:jsp页面显示字符串数组:**
139
140<logic:iterate id="dateList" name="dateList" type="java.lang.String">
141<bean:write name="dateList">
142</bean:write></logic:iterate>
143
144**七:如何定义自己的jsp文件发布路径(windows2000环境下实践通过):**
145
146打开tomcat目录下conf文件夹下的server.xml文件,找到<!---Tomcat manager Context--->标签 
147
148在其<context>标签后,添加自己的发布路径,如下: 
149
150<context debug="0" docbase="D:/work" path="/mywebapps" reloadable="true"></context>
151
152重启tomcat,访问:/localhost:8080/mybwebapps/,ok! ****
153
154**八:如何解决偏僻难字的显示问题:**
155
156使用大字符集,将所有jsp页面字符集设置如下:```
157@ page contentType="text/html;charset=GBK" 
158```注意:GBK一定得大写。 
159
160**九:页面刷新数据重复提交问题:**
161
162解决的方法就是采用重定向。与二的解决方法相同: 
163
1641.将struts-config.xml中redirect属性置为true。 
165
1662.如果是带参数的跳转: 
167
168ActionForward forward = mapping.findForward("success"); 
169
170StringBuffer bf = new StringBuffer(forward.getPath()); 
171
172bf.append("?id=1");//参数 
173
174return new ActionForward(bf.toString(),true); 
175
176**十:其他:**
177
178编程规范建议: 
179
180数据库接口类命名:*DAO.JAVA 
181
182逻辑类命名:*BO.JAVA 
183
184ACTION命名:*Action.JAVA 
185
186ACTIONFORM命名:*Form.JAVA 
187
188然后一个功能模块的文件放在一个包内。 
189
190都是一些比较初级的问题,但是希望能够助你在学习struts之初少走点弯路,也希望对于文中的问题给予指点,:)。</context></html:file></form></bean:write></html:rewrite></bean:size></bean:size>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus