Junky's IT Notebook

          統(tǒng)計(jì)

          留言簿(8)

          積分與排名

          WebSphere Studio

          閱讀排行榜

          評(píng)論排行榜

          體驗(yàn)Struts(2)---整體結(jié)構(gòu)

          為了讓大家更方便的了解我這個(gè)設(shè)計(jì),我先把我的一些整體的規(guī)劃都說出來吧,由于我是初學(xué),難免會(huì)參照本書籍來看,我買的是那本孫某女的書《精通:*****》,看了看她前面的介紹,我一看了不得,能出書,寫的還都不錯(cuò),這女的可不得了,漸漸疑惑的地方非常多,比如例子里面注釋都拽上了英語(yǔ),搞不懂,而當(dāng)我從網(wǎng)上下到電子盜版jakarta struts(我已安下栽說明要求的那樣在24小時(shí)后刪除了)這本書的時(shí)候我才恍然大悟,原來是抄襲啊?至于是誰(shuí)抄的誰(shuí),口說無憑,不能亂誹謗,不過大家心里都該有桿稱!

          下面就是代碼了:
          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;數(shù)據(jù)庫(kù)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;
          管理員權(quán)限

          ??? 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;學(xué)生學(xué)號(hào)
          ??? private SchoolClass schoolClass;
          班級(jí)

          ??? 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";
          ??? }
          }

          而對(duì)于Action我分別做了一個(gè)抽象類,之后別的從這里繼承
          先是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
          {
          ??? /**
          ???? * 由服務(wù)工廠方法創(chuàng)建服務(wù)
          ???? * @return 數(shù)據(jù)庫(kù)操作的服務(wù)
          ???? * 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;
          ??? }

          ???
          ??? /**
          ???? * 抽象方法,子類實(shí)現(xiàn)
          ???? * @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 當(dāng)前用戶
          ???? * 2006-5-16 18:13:35
          ???? */
          ??? public abstract Person getPerson( HttpServletRequest req );

          ??? /**
          ???? * 父類的執(zhí)行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的對(duì)象
          ???? * @param req
          ???? * @param attribute 對(duì)象屬性
          ???? * 2006-5-16 18:16:59
          ???? */
          ??? public void removeSessionObject( HttpServletRequest req, String attribute )
          ??? {
          ??????? HttpSession session = req.getSession();
          ??????? session.removeAttribute( attribute );
          ??? }

          ??? /**
          ???? * 設(shè)置session中屬性為attribute的對(duì)象
          ???? * @param req
          ???? * @param attribute 設(shè)置屬性
          ???? * @param o 設(shè)置對(duì)象
          ???? * 2006-5-16 18:17:50
          ???? */
          ??? public void setSessionObject( HttpServletRequest req, String attribute,
          ??????????? Object o )
          ??? {
          ??????? req.getSession().setAttribute( attribute, o );
          ??? }

          ??? /**
          ???? * 設(shè)置application中屬性為attribute的對(duì)象
          ???? * @param req
          ???? * @param attribute 設(shè)置屬性
          ???? * @param o 設(shè)置對(duì)象
          ???? * 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
          {
          ??? /**
          ???? * 由服務(wù)工廠方法創(chuàng)建服務(wù)
          ???? * @return 數(shù)據(jù)庫(kù)操作的服務(wù)
          ???? * 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 當(dāng)前用戶
          ???? * 2006-5-16 18:13:35
          ???? */
          ??? public abstract Person getPerson( HttpServletRequest req );

          ??? /**
          ???? * 父類的執(zhí)行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的對(duì)象
          ???? * @param req
          ???? * @param attribute 對(duì)象屬性
          ???? * 2006-5-16 18:16:59
          ???? */
          ??? public void removeSessionObject( HttpServletRequest req, String attribute )
          ??? {
          ??????? HttpSession session = req.getSession();
          ??????? session.removeAttribute( attribute );
          ??? }

          ??? /**
          ???? * 設(shè)置session中屬性為attribute的對(duì)象
          ???? * @param req
          ???? * @param attribute 設(shè)置屬性
          ???? * @param o 設(shè)置對(duì)象
          ???? * 2006-5-16 18:17:50
          ???? */
          ??? public void setSessionObject( HttpServletRequest req, String attribute,
          ??????????? Object o )
          ??? {
          ??????? req.getSession().setAttribute( attribute, o );
          ??? }

          ??? /**
          ???? * 設(shè)置application中屬性為attribute的對(duì)象
          ???? * @param req
          ???? * @param attribute 設(shè)置屬性
          ???? * @param o 設(shè)置對(duì)象
          ???? * 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 );
          ??? }
          }

          對(duì)于程序中的一些提示信息,我比較喜歡用JS來寫,所以我把這些都放到了一個(gè)類中
          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 閱讀(300) 評(píng)論(0)  編輯  收藏 所屬分類: struts

          主站蜘蛛池模板: 新乡市| 彩票| 唐河县| 连江县| 瑞金市| 探索| 宁德市| 佛冈县| 南和县| 康平县| 益阳市| 自治县| 九江市| 汉川市| 柯坪县| 邻水| 英吉沙县| 视频| 呼和浩特市| 安陆市| 台山市| 岳池县| 郓城县| 酉阳| 金川县| 平泉县| 拉孜县| 辽阳市| 枝江市| 孟连| 友谊县| 金乡县| 六盘水市| 京山县| 林周县| 秦安县| 泰州市| 漠河县| 塔河县| 承德市| 宽甸|