posts - 14, comments - 22, trackbacks - 0, articles - 4
  BlogJava :: 首页 ::  :: 联系 :: 聚合  :: 管理

体验Struts(2)---整体结构

Posted on 2006-05-25 01:07 誰伴我闖蕩 阅读(1163) 评论(0)  编辑  收藏

为了让大家更方便的了解我这个设计,我先把我的一些整体的规划都说出来吧,由于我是初学,难免会参照本书籍来看,我买的是那本孙某女的书《精通:*****》,看了看她前面的介绍,我一看了不得,能出书,写的还都不错,这女的可不得了,渐渐疑惑的地方非常多,比如例子里面注释都拽上了英语,搞不懂,而当我从网上下到电子盗版jakarta struts(我已安下栽说明要求的那样在24小时后删除了)这本书的时候我才恍然大悟,原来是抄袭啊?至于是谁抄的谁,口说无凭,不能乱诽谤,不过大家心里都该有杆称!

下面就是代码了:
package com.boya.subject.model;
public interface Person
{
    public Long getId();
    public void setId( Long id );
    public String getName();
    public void setName( String name );
    public String getPassword();
    public void setPassword( String password );
    public String getTelphone();
    public void setTelphone( String telphone );
    public String getUser();
    public void setUser( String user );
    public String getType();
}

package com.boya.subject.model;
public abstract class User implements Person
{
    private Long id;数据库id
    private String user;用户名
    private String password;密码
    private String name;姓名
    private String telphone;
电话

    public Long getId()
    {
        return id;
    }

    public void setId( Long id )
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName( String name )
    {
        this.name = name;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword( String password )
    {
        this.password = password;
    }

    public String getTelphone()
    {
        return telphone;
    }

    public void setTelphone( String telphone )
    {
        this.telphone = telphone;
    }

    public String getUser()
    {
        return user;
    }

    public void setUser( String user )
    {
        this.user = user;
    }
}

package com.boya.subject.model;
public class Admin extends User
{
    private String grade = null;
管理员权限

    public String getGrade()
    {
        return grade;
    }

    public void setGrade( String grade )
    {
        this.grade = grade;
    }

    public String getType()
    {
        return "admin";
    }
}

package com.boya.subject.model;
public class Teacher extends User
{
    private String level;
教师职称

    public String getLevel()
    {
        return level;
    }

    public void setLevel( String level )
    {
        this.level = level;
    }

    public String getType()
    {
        return "teacher";
    }
}

package com.boya.subject.model;

public class Student extends User
{
    private String sn;学生学号
    private SchoolClass schoolClass;
班级

    public SchoolClass getSchoolClass()
    {
        return schoolClass;
    }

    public void setSchoolClass( SchoolClass schoolClass )
    {
        this.schoolClass = schoolClass;
    }

    public String getSn()
    {
        return sn;
    }

    public void setSn( String sn )
    {
        this.sn = sn;
    }

    public String getType()
    {
        return "student";
    }
}

而对于Action我分别做了一个抽象类,之后别的从这里继承
先是Action的
package com.boya.subject.controller;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.boya.subject.frame.ID;
import com.boya.subject.frame.ServiceFactory;
import com.boya.subject.model.Person;
import com.boya.subject.service.Service;
import com.boya.subject.util.HtmlUtil;

public abstract class BaseAction extends Action
{
    /**
     * 由服务工厂方法创建服务
     * @return 数据库操作的服务
     * 2006-5-16 18:10:04
     */
    public Service getService()
    {
        ServiceFactory factory = (ServiceFactory) getAppObject( ID.SF );
        Service service = null;
        try
        {
            service = factory.createService();
        }
        catch ( Exception e )
        {
        }
        return service;
    }

    /**
     * 判断用户是否合法登陆
     * @param req
     * @return 用户是否登陆
     * 2006-5-16 18:11:26
     */
    public boolean isLogin( HttpServletRequest req )
    {
        if ( getPerson( req ) != null ) return true;
        else
            return false;
    }

   
    /**
     * 抽象方法,子类实现
     * @param mapping
     * @param form
     * @param req
     * @param res
     * @return
     * @throws Exception
     * 2006-5-16 18:12:54
     */
    protected abstract ActionForward executeAction( ActionMapping mapping,
            ActionForm form, HttpServletRequest req, HttpServletResponse res )
            throws Exception;

    /**
     * 获取session范围的用户
     * @param req
     * @return 当前用户
     * 2006-5-16 18:13:35
     */
    public abstract Person getPerson( HttpServletRequest req );

    /**
     * 父类的执行Action
     * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    public ActionForward execute( ActionMapping mapping, ActionForm form,
            HttpServletRequest req, HttpServletResponse res ) throws Exception
    {
        if ( !isLogin( req ) )
        {
            HtmlUtil.callParentGo( res.getWriter(), ID.M_UNLOGIN, ID.P_INDEX );
            return null;
        }
        return executeAction( mapping, form, req, res );
    }

    /**
     * 删除session中属性为attribute的对象
     * @param req
     * @param attribute 对象属性
     * 2006-5-16 18:16:59
     */
    public void removeSessionObject( HttpServletRequest req, String attribute )
    {
        HttpSession session = req.getSession();
        session.removeAttribute( attribute );
    }

    /**
     * 设置session中属性为attribute的对象
     * @param req
     * @param attribute 设置属性
     * @param o 设置对象
     * 2006-5-16 18:17:50
     */
    public void setSessionObject( HttpServletRequest req, String attribute,
            Object o )
    {
        req.getSession().setAttribute( attribute, o );
    }

    /**
     * 设置application中属性为attribute的对象
     * @param req
     * @param attribute 设置属性
     * @param o 设置对象
     * 2006-5-16 18:17:50
     */
    public void setAppObject( String attribute, Object o )
    {
        servlet.getServletContext().setAttribute( attribute, o );
    }

    public Object getSessionObject( HttpServletRequest req, String attribute )
    {
        Object obj = null;
        HttpSession session = req.getSession( false );
        if ( session != null ) obj = session.getAttribute( attribute );
        return obj;
    }

    public Object getAppObject( String attribute )
    {
        return servlet.getServletContext().getAttribute( attribute );
    }

    public void callParentGo( HttpServletResponse res, String msg, String url )
            throws IOException
    {
        HtmlUtil.callParentGo( res.getWriter(), msg, url );
    }

    public void callMeGo( HttpServletResponse res, String msg, String url )
            throws IOException
    {
        HtmlUtil.callMeGo( res.getWriter(), msg, url );
    }

    public void callBack( HttpServletResponse res, String msg )
            throws IOException
    {
        HtmlUtil.callBack( res.getWriter(), msg );
    }

    public void callMeConfirm( HttpServletResponse res, String msg, String ok,
            String no ) throws IOException
    {
        HtmlUtil.callMeConfirm( res.getWriter(), msg, ok, no );
    }
}
再是DispatchAction的
package com.boya.subject.controller;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.boya.subject.frame.ID;
import com.boya.subject.frame.ServiceFactory;
import com.boya.subject.model.Person;
import com.boya.subject.service.Service;
import com.boya.subject.util.HtmlUtil;

public abstract class BaseDispatchAction extends DispatchAction
{
    /**
     * 由服务工厂方法创建服务
     * @return 数据库操作的服务
     * 2006-5-16 18:10:04
     */
    public Service getService()
    {
        ServiceFactory factory = (ServiceFactory) getAppObject( ID.SF );
        Service service = null;
        try
        {
            service = factory.createService();
        }
        catch ( Exception e )
        {
        }
        return service;
    }

    /**
     * 判断用户是否合法登陆
     * @param req
     * @return 用户是否登陆
     * 2006-5-16 18:11:26
     */
    public boolean isLogin( HttpServletRequest req )
    {
        if ( getPerson( req ) != null ) return true;
        else
            return false;
    }

    /**
     * 获取session范围的用户
     * @param req
     * @return 当前用户
     * 2006-5-16 18:13:35
     */
    public abstract Person getPerson( HttpServletRequest req );

    /**
     * 父类的执行DispatchAction
     * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    public ActionForward execute( ActionMapping mapping, ActionForm form,
            HttpServletRequest req, HttpServletResponse res ) throws Exception
    {
        try
        {
            if ( !isLogin( req ) )
            {
                callParentGo( res, ID.M_UNLOGIN, ID.P_INDEX );
                return null;
            }
            return super.execute( mapping, form, req, res );
        }
        catch ( NoSuchMethodException e )
        {
            callBack( res, ID.M_NOMETHOD );
            return null;
        }
    }

    /**
     * 删除session中属性为attribute的对象
     * @param req
     * @param attribute 对象属性
     * 2006-5-16 18:16:59
     */
    public void removeSessionObject( HttpServletRequest req, String attribute )
    {
        HttpSession session = req.getSession();
        session.removeAttribute( attribute );
    }

    /**
     * 设置session中属性为attribute的对象
     * @param req
     * @param attribute 设置属性
     * @param o 设置对象
     * 2006-5-16 18:17:50
     */
    public void setSessionObject( HttpServletRequest req, String attribute,
            Object o )
    {
        req.getSession().setAttribute( attribute, o );
    }

    /**
     * 设置application中属性为attribute的对象
     * @param req
     * @param attribute 设置属性
     * @param o 设置对象
     * 2006-5-16 18:17:50
     */
    public void setAppObject( String attribute, Object o )
    {
        servlet.getServletContext().setAttribute( attribute, o );
    }

    public Object getSessionObject( HttpServletRequest req, String attribute )
    {
        Object obj = null;
        HttpSession session = req.getSession( false );
        if ( session != null ) obj = session.getAttribute( attribute );
        return obj;
    }

    public Object getAppObject( String attribute )
    {
        return servlet.getServletContext().getAttribute( attribute );
    }

    public void callParentGo( HttpServletResponse res, String msg, String url )
            throws IOException
    {
        HtmlUtil.callParentGo( res.getWriter(), msg, url );
    }

    public void callMeGo( HttpServletResponse res, String msg, String url )
            throws IOException
    {
        HtmlUtil.callMeGo( res.getWriter(), msg, url );
    }

    public void callBack( HttpServletResponse res, String msg )
            throws IOException
    {
        HtmlUtil.callBack( res.getWriter(), msg );
    }

    public void callMeConfirm( HttpServletResponse res, String msg, String ok,
            String no ) throws IOException
    {
        HtmlUtil.callMeConfirm( res.getWriter(), msg, ok, no );
    }
}

对于程序中的一些提示信息,我比较喜欢用JS来写,所以我把这些都放到了一个类中
import java.io.IOException;
import java.io.Writer;

public class HtmlUtil
{
    public static void callParentGo( Writer out, String msg, String url )
            throws IOException
    {
        out.write( "<script language=\"javascript\">" );
        out.write( "alert(\"" + msg + "\");" );
        out.write( "parent.location.href=\"" + url + "\";" );
        out.write( "</script>" );
    }

    public static void callMeGo( Writer out, String msg, String url )
            throws IOException
    {
        out.write( "<script language=\"javascript\">" );
        out.write( "alert(\"" + msg + "\");" );
        out.write( "location.href=\"" + url + "\";" );
        out.write( "</script>" );
    }

    public static void callMeConfirm( Writer out, String msg ,String ok, String no )
            throws IOException
    {
        out.write( "<script language=\"javascript\">" );
        out.write( "if(window.confirm(\"" + msg + "\")){" );
        out.write( "location.href=\"" + ok + "\"}else{" );
        out.write( "location.href=\"" + no + "\"}" );
        out.write( "</script>" );
    }

    public static void callBack( Writer out, String msg ) throws IOException
    {
        out.write( "<script language=\"javascript\">" );
        out.write( "alert(\"" + msg + "\");" );
        out.write( "parent.history.back();" );
        out.write( "</script>" );
    }
}


只有注册用户登录后才能发表评论。


网站导航:
 
有事在这里给我留言噢!