一、
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);