Struts(七)模型驱动及分层体系结构

1.分层体系架构

DAO :(Data Access Objects) 数据访问对象是第一个面向对象的接口,数据访问层.一般有接口和该接口的实现类! 接口用于规范实现类! 
实现类一般用于用于操作数据库!

一般操作修改,添加,删除数据库操作的步骤很相似,就写了一个公共类DAO类 ,修改,添加,删除数据库操作时 直接调用公共类DAO类!

 

例子:

LoginService

package com.liule.service;

public interface LoginService
{
    public boolean IsLogin(String username,String password);
}

LoginSeriverImp

package com.liule.impl;

import com.liule.service.LoginService;

public class LoginSeriverImpl implements LoginService
{

    @Override
    public boolean IsLogin(String username, String password)
    {
        if("hello".equals(username)&& "world".equals(password))
        {
            return true;
        }
        return false;
    }

}

LoginAction.java

LoginSeriverImpl ls = new LoginSeriverImpl();
    public String execute() throws Exception
    {
        if(ls.IsLogin(username,password))
        {
            return SUCCESS;
        }
        return INPUT;
    }

struts.xml

<action name="login" class="com.liule.action.LoginAction">
            <result name="success">/servlet.jsp</result>
            <result name="input">/login.jsp</result>
        </action>

2.模型驱动(Model Driven):对象与属性都是后台自动设置成功,实现ModelDriven<T>接口

属性驱动(Property Driven):对象与属性都是需要手动设置,以前使用的

LoginAction

package com.liule.action;

import com.liule.bean.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class LoginAction2 extends ActionSupport implements ModelDriven<User>
{
    private User user = new User();

    @Override
    public String execute() throws Exception
    {
        
        return SUCCESS;
    }

    @Override
    public User getModel() //返回自己定义的模型
    {
        
        return user;
    }
}

属性驱动与模型模式的区别:

1)属性驱动灵活,准确:模型驱动不灵活,因为很多时候,页面所提交过来的参数不属于模型中的属性,也就是说页面所提交过来的参数与模型中的属性并不一致,这是很常见的情况。

2)模型驱动更加符合面向对象的编程风格,使得我们获得的是对象而不是一个个离散的值。

 

推荐使用:属性驱动!

 

http://www.cnblogs.com/jbelial/p/3294520.html

posted on 2015-11-06 03:31  彩屏黑白  阅读(214)  评论(0编辑  收藏  举报

导航