struts formbean

理由如下:
1.由于后台数据库表字段的修改造成此formbean的修改,同时前端表现页面展现代码要修改,Action类也要跟着修改,也就是业务代码要修改,开发初期表的字段增删改是非常正常的,没有人能做到设计都是完美不用修改的。
2.有些人美其名曰formbean是面向对象,面向对象造成了开发与维护的如此繁琐,为OO而OO,得不偿失。最需要OO的地方是业务规则以及框架。
我们需要什么:
1.struts的action控制机制还是要保留,tag部分可以彻底扔掉。
2.在表现层与业务逻辑层我们传递的就是VO(数值对象)而已。需要的只是一个数据存放的容器而已,能做到通用就行了。难道非要写成bean.getXXX()的形式才叫OO么?
3.对于刚刚接触struts的人,或者不喜欢用标签的人,怎么样才能更好的完成协作开发呢?
我们只需要他们会html,java就可以了,让标签们见鬼去吧,让formbean见鬼去吧。
改进方案:
1.一个通用的action ,BaseAction extendsAction.负责完成http request,http session的参数收集与整理,形成一个通用的form,BaseForm extends ActionForm.
此BaseForm就可以传递到表现层了。
2.如果你有些地方需要使用标签,可能因为您偏爱或者习惯使用struts标签了,所以你可以继承这个BaseForm,然后添加你的方法和属性。而对于不习惯的人,则可以使用方案1的逻辑了。
3.BaseAction最好能实现几个通用功能,比如分页,普通查询,修改,插入等,这样各个实现的业务action在代码上减少很多,维护就更简单了。
代码实现:
<wbr></wbr>

Java代码

public class OrderAction extends BaseAction {
public ActionForward execute(ActionMapping mapping, ActionForm form,
<wbr><wbr><wbr>HttpServletRequest request, HttpServletResponse response) {<br><wbr><wbr>//解析request 和 session 参数,在BaseAction中实现<br><wbr><wbr>BaseForm bf = this.parse(request, response);<br><wbr><wbr>//没准你还想在返回页面得到请求的参数<br><wbr><wbr>request.setAttribute(BaseForm.NAME, bf);<br><wbr><wbr>String action = (String) bf.getStringData("action");<br><wbr><wbr>try {<br><wbr><wbr><wbr>if (action.equals("add")<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr> .....<br><wbr><wbr><wbr> request.setAttribute("xxxx", xxx);<br><wbr><wbr><wbr> return mapping.findForward("xxx");<br><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> else if (action.equals("list"))<br><wbr><wbr><wbr> {<br><wbr><wbr><wbr> .....<br><wbr><wbr><wbr> request.setAttribute("xxxx", xxxxx);<br><wbr><wbr><wbr> return mapping.findForward("xxxxx");<br><wbr><wbr><wbr> }<br><wbr><wbr><wbr> else<br><wbr><wbr><wbr> .....</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> }catch (Exception ex) {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> ......<br><wbr><wbr><wbr>return mapping.findForward("error");<br><wbr><wbr><wbr><wbr><wbr> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr>}<br> }</wbr></wbr>

<wbr></wbr>

<wbr></wbr>

<wbr></wbr>


public class BaseForm extends ActionForm {
private Map datas=new HashMap();
<wbr>public Object getData(String name)<br><wbr>{<br><wbr><wbr>return datas.get(name.toUpperCase());<br><wbr>}<br><wbr>public Object setData(String name,Object obj)<br><wbr>{<br><wbr><wbr>return datas.put(name.toUpperCase(),obj);<br><wbr>}<br> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr></wbr>

<wbr></wbr>

<wbr></wbr>

整个配置文件就一个formbean而已
<struts-config>
<wbr><wbr><wbr> &lt;form-beans&gt;<br><wbr><wbr><wbr> &lt;form-bean name="baseform" type="********.****.***.BaseForm"&gt;&lt;/form-bean&gt;<br><wbr><wbr><wbr> &lt;/form-beans&gt;<br> &lt;/struts-config&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

你可能感兴趣的:(struts)