<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Junky's IT Notebook

    統計

    留言簿(8)

    積分與排名

    WebSphere Studio

    閱讀排行榜

    評論排行榜

    體驗Struts(2)---整體結構

    為了讓大家更方便的了解我這個設計,我先把我的一些整體的規劃都說出來吧,由于我是初學,難免會參照本書籍來看,我買的是那本孫某女的書《精通:*****》,看了看她前面的介紹,我一看了不得,能出書,寫的還都不錯,這女的可不得了,漸漸疑惑的地方非常多,比如例子里面注釋都拽上了英語,搞不懂,而當我從網上下到電子盜版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>" );
    ??? }
    }

    posted on 2006-05-25 20:27 junky 閱讀(298) 評論(0)  編輯  收藏 所屬分類: struts

    主站蜘蛛池模板: 国产在线观看免费不卡| 亚洲国产精品久久久久网站 | 亚洲综合精品网站| 182tv免费视频在线观看| 亚洲三级中文字幕| 亚洲性在线看高清h片| 99精品视频在线视频免费观看| 亚洲免费福利在线视频| 亚洲真人无码永久在线| 在线观看视频免费完整版| jizz日本免费| 亚洲第一男人天堂| 亚洲av午夜福利精品一区人妖| 成年大片免费视频| 日韩视频免费在线观看| 在线观看免费亚洲| 亚洲精品电影在线| 亚洲人午夜射精精品日韩| 在线观看日本免费a∨视频| 91免费国产视频| 国产亚洲精品免费| 亚洲乱码中文论理电影| 国产亚洲AV手机在线观看| 四虎免费大片aⅴ入口| 免费国产黄网站在线观看可以下载| 国产精品久久亚洲一区二区| 亚洲美女视频网站| 亚洲一区二区三区AV无码| 在线观看免费成人| 日日麻批免费40分钟日本的| 91成人免费福利网站在线| 羞羞的视频在线免费观看| 77777午夜亚洲| 亚洲国产精品综合久久久| 久久精品国产亚洲麻豆| 无码欧精品亚洲日韩一区夜夜嗨 | 亚洲人成网站在线观看播放动漫 | 伊人久久亚洲综合| 国产免费资源高清小视频在线观看| 久久综合国产乱子伦精品免费| 久久久久女教师免费一区|