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