|
下面是我这几天学struts的一些认识,算不上心得,更算不上是总结。仅供参考,如有错误之处,请指出。谢谢。
附件,JBuilder2006工程: http://files.myopera.com/xlingFairy/soft/StrutsTest.rar
1,Map支持的ActionForm 首先要声明一个HashMap (java.util.HashMap)类型的变量。一开始,我没有注意,声明成Map了,而Map是一个接口,所以无论我怎么更改,都是失败。
ActionForm里:
public HashMap getMap(){ return map; }
public void setMap(HashMap pMap){ map = pMap; }
public void setValue(String key,Object value){ getMap().put(key,value); }
public Object getValue(String key){ return getMap().get(key); }
JSP里:
<html:text property="value(age)"></html:text>
2,Message Resources: 如下是struts-config.xml里的关于message-resources的片段(多资源文件):
<message-resources parameter="resource.Application" /> <message-resources key="ZHS" parameter="resource.App" />
其中,resource.Application是指存放在WEB-INF/class/resource文件夹下的Application.properties文件。resource.App同理。 上面的代码片段中,Key="ZHS"要结合如下JSP代码段:
<bean:message bundle="ZHS" key="ZHS.userName.required"/>
即:bundle="ZHS"是指出所用的资源是哪个资源文件里的。如果不加bundle,则默认是:resource.Application
properties文件的格式为: key1 = value1 key2 = value2 ...
3,从properties文件里提取的汉字为乱码 properties文件好像只能为ascii编码,(我试过把编码改为utf8,运行错误!) 百度一下,普遍推荐使用:native2ascii命令转换,我转换过,它是把汉字转成所指定的编码的文字编码
比如:
native2ascii -encoding utf-8 application.properties app1.properties
生成的app1.properties内容如下:
ZHS.userName.required = \ufffd\u00fb\ufffd\ufffd\ufffd\ufffd\ufffd\u03aa\ufffd\ufffd
都是一堆编码,横竖是看不懂,而且还麻烦。我说说我的方法,不过你要有Dreamweaver: 新建一html页面,然后:Modify->Page property->Title/Encoding ,把Encoding设为:西欧,在设计模式下输入汉字,比如:
密码不能为空
兩次輸入的密碼不一致
在Code模式下,自动被转换成了:
密码不能为空 兩次輸入的密碼不一致
这样做还不会涉及到简体和繁体的问题。
4,表单验证: 现在我还说不出哪个是什么业务层,逻辑层了,它们对我来说,有点抽象,我只知道实现! 首先在ActionForm里进行验证:
public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) { ActionErrors errors = new ActionErrors();
String userName =(String) getValue("userName"); String password = (String) getValue("password"); if(Validate.isNull(userName)){ errors.add("userName",new ActionMessage("ZHS.userName.required")); }
if(Validate.isNull(password)) errors.add("password",new ActionMessage("ZHS.password.required"));
return errors; }
ActionError以不赞成使用,所以用ActionMessage。 当这一层验证不成功的时候,会转发到配置文件中指定的input文件。 注意:
errors.add("userName",new ActionMessage("ZHS.userName.required"))
要理解它,请看下面的JSP代码:
<html:errors bundle="ZHS" property="userName"/>
一开始,我用:
if(password == null) errors.add(....)
无论如何,我都不到自定的错误提示,后来换成了下面的代码才行:
public class Validate { public static boolean isNull(String para){ if(para == null || para.trim().equals("")) return true; else return false; } }
如果在struts-config.xml里,设置action的validate为false或no的话,上面的验证方法将不被执行。
<action input="/MapSupportInput.jsp" name="mapSupportForm" path="/mapSupportAction" scope="request" type="App.MapSupportAction" validate="true"> <forward name="success" path="/success1.jsp" /> <forward name="failure" path="/failure.jsp" /> </action>
在Action里的验证: 如下代码:
package App;
import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForm; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.*;
public class MapSupportAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { MapSupportForm mForm = (MapSupportForm) form;
String userName = (String) mForm.getValue("userName"); String password = (String) mForm.getValue("password"); String password1 = (String) mForm.getValue("password1");
if(!Validate.isNull(password) && !Validate.isNull(password1) && password.equals(password1)){
UserBean ubean = new UserBean(); ubean.setValue("userName",userName);
request.setAttribute("ubean",ubean); request.setAttribute("valid",request.getRemoteAddr()); return mapping.findForward("success"); }else{ ActionErrors errors = new ActionErrors(); errors.add("passwordEqual",new ActionMessage("ZHS.password.equals")); saveErrors(request,errors); /* ActionMessages msgs = new ActionMessages(); msgs.add("passwordEqual",new ActionMessage("ZHS.password.equals")); saveMessages(request,msgs); */ return new ActionForward(mapping.getInput()); } } }
saveErrors以不赞成使用,换成saveMessages后,在JSP里用<html:errors />又得不到错误! return new ActionForward(mapping.getInput()); 不能写成: return mapping.findForward(mapping.getInput());
|