首页 > 其他 > 详细

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

时间:2015-11-06 06:54:17      阅读:331      评论:0      收藏:0      [点我收藏+]

1.分层体系架构

技术分享

例子:

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)模型驱动更加符合面向对象的编程风格,使得我们获得的是对象而不是一个个离散的值。

 

推荐使用:属性驱动!

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

原文:http://www.cnblogs.com/liu-Gray/p/4941400.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!