基于J2EE Struts框架的課程設(shè)計實訓(xùn)項目——BBS論壇系統(tǒng)——構(gòu)建控制層中實現(xiàn)頁面跳轉(zhuǎn)的 Action程序類
楊教授大學(xué)堂 精心創(chuàng)作的優(yōu)秀程序員 職業(yè)提升必讀系列資料基于J2EE Struts框架的課程設(shè)計實訓(xùn)項目BBS論壇系統(tǒng)構(gòu)建控制層中實現(xiàn)頁面跳轉(zhuǎn)的 Action程序類1.1.1 頁面跳轉(zhuǎn)的 Action程序類PageForwordAction1、添加一個實現(xiàn)頁面跳轉(zhuǎn)的 Action類PageForwordAction(1)新建一個Action類1) Path為/pageForwordAction2) 基類:org.apache.struts.actions.DispatchAction3) 類型:com.px1987.webbbs.action.PageForwordAction4) /WebBBS/WebRoot/WEB-INF/struts-config_pageForward.xml(2)并在parameter中輸入action(3)并為它添加各個 Forward目標(4)也就是下面的狀態(tài)<action parameter="action" path="/pageForwordAction" type="com.px1987.webbbs.action.PageForwordAction" > <forward name="showRegisterForm" path="goShowRegisterForm" /> <!- 直接采用Tiles中的定義名稱作為 path的目標 -> <forward name="showWebAppError" path="goShowWebAppError" /> <forward name="doSystemManage" path="goSystemManage" /> <forward name="showAllBBSClassInfo" path="goShowAllBBSClassInfo" /> <forward name="showIndexContent" path="goWebBBSIndex" /> <forward name="showUpdateUserInfo" path="goShowUpdateUserInfo" /> <forward name="showUpdateAdminUserInfo" path="goShowUpdateAdminUserInfo" /> <forward name="showOnLineUserInfo" path="goShowOnLineUserInfo" /> <forward name="doGetUserPassWord" path="goGetUserPassWord" /> <forward name="doSendBBSInfo" path="goSendBBSInfo" /> <forward name="doReplyBBSInfo" path="goReplyBBSInfo" /> <forward name="showAllHotBBSInfo" path="goShowAllHotBBSInfo" /> <forward name="showAllBBSInClass" path="goShowAllBBSInClass" /> <forward name="showOneBBSInfo" path="goShowOneBBSInfo" /> <forward name="showLoginUserAllBBSInfo" path="goShowLoginUserAllBBSInfo" /> <forward name="showAllBBSInfoByReplyUserInfo" path="goShowAllBBSInfoByReplyUserInfo" /> <forward name="showNewBBSInfo" path="goShowNewBBSInfo" /> <forward name="showPrevelInfo" path="goShowPrevelInfo" /> <forward name="showTitleLeaderInfo" path="goShowTitleLeaderInfo" /> </action>2、編程該Action類package com.px1987.webbbs.action;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import javax.servlet.ServletContext;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 org.apache.struts.validator.DynaValidatorForm;import com.px1987.webbbs.constant.WebBankAllConstants;import com.px1987.webbbs.exception.WebBBSException;import com.px1987.webbbs.model.BBSInfoManageInterface;import com.px1987.webbbs.model.BBSInfoVO;import com.px1987.webbbs.model.BBSTitleVO;import com.px1987.webbbs.model.UserInfoManageInterface;import com.px1987.webbbs.model.UserInfoVO;public class PageForwordAction extends DispatchAction BBSInfoManageInterface bbsInfoManagerBean=null;UserInfoManageInterface userInfoManageBean=null;public void setBbsInfoManagerBean(BBSInfoManageInterface bbsInfoManagerBean) this.bbsInfoManagerBean = bbsInfoManagerBean;public void setUserInfoManageBean(UserInfoManageInterface userInfoManageBean) this.userInfoManageBean = userInfoManageBean;public PageForwordAction() /本構(gòu)造方法是在利用 Spring IOC獲得目標對象時被應(yīng)用super();/* String bbsInfoManageBeanClassName=null;String userInfoManageBeanClassName=null;public PageForwordAction() throws WebBBSException /本構(gòu)造方法是在利用 可配置化的工廠時被應(yīng)用super();newUserAndBBSManageBean();public void newUserAndBBSManageBean() throws WebBBSExceptionbbsInfoManageBeanClassName=ClassNameConfig.getProperty("bbsInfoManageImple.className");bbsInfoManagerBean=BBSInfoManageFactory.newBBSInfoManageBean(bbsInfoManageBeanClassName);userInfoManageBeanClassName=ClassNameConfig.getProperty("userInfoManageImple.className");userInfoManageBean=UserInfoManageFactory.newUserInfoManageBean(userInfoManageBeanClassName);*/public ActionForward indexShowPattern_BarMethod(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)return mapping.findForward("showIndexContent_BarMethod");public ActionForward indexShowPattern_PlainMethod(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)return mapping.findForward("showIndexContent_PlainMethod");public ActionForward indexLeftMenuBar(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)return mapping.findForward("showIndexLeftMenuBar");public ActionForward indexSwitchMenu(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)return mapping.findForward("showIndexSwitchMenu");public ActionForward indexDefaultContent(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)return mapping.findForward("showIndexDefaultContent");public ActionForward showIndexContent(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)ArrayList titleInfoVOArrayList_10 = null;ArrayList allHothitsBBSInfoVOArrayList = null;int totalCounterOfTitleInfo;int totalCounterOfTodayBBSInfo;int titleCounte=10;/只返回前10個分類元素對象try/newUserAndBBSManageBean();/創(chuàng)建用戶和BBS信息管理業(yè)務(wù)類的對象實例titleInfoVOArrayList_10 = bbsInfoManagerBean.doGetBBSTitleInfo(titleCounte); catch (WebBBSException e) request.setAttribute("errorText", "出現(xiàn)如下的錯誤:" +e.getMessage(); return mapping.findForward("showWebAppError");/獲得總的分類數(shù)目trytotalCounterOfTitleInfo = bbsInfoManagerBean.doGetTotalBBSTitleInfoCounter(); catch (WebBBSException e)request.setAttribute("errorText", "出現(xiàn)如下的錯誤:" +e.getMessage(); return mapping.findForward("showWebAppError");int hitBBSCounte=10;/只返回前10個熱門的BBS信息tryallHothitsBBSInfoVOArrayList = bbsInfoManagerBean.doGetBBSInfoByHits(hitBBSCounte); catch (WebBBSException e)request.setAttribute("errorText", "出現(xiàn)如下的錯誤:" +e.getMessage(); return mapping.findForward("showWebAppError");/* 下面的代碼是獲得今日帖數(shù)目*/ ArrayList newBBSInfoList = null; String keyText = "" java.util.Date rightNow = new java.util.Date(); keyText = (1900 + rightNow.getYear() + "-" + (rightNow.getMonth() + 1) + "-" + rightNow.getDate();/* try/此時在ArrayList中將包含有今天的新貼(BBSInfoVO類型)的對象集合 newBBSInfoList = bbsInfoManageImple.doGetTodayAllBBSInfo(); catch (WebBBSException e)request.setAttribute("errorText", "出現(xiàn)如下的錯誤:" +e.getMessage();oneRequestDispatcher=request.getRequestDispatcher("/errorDeal/showWebAppError.jsp"); oneRequestDispatcher.forward(request, response); return; totalCounterOfTodayBBSInfo = newBBSInfoList.size();*/ try totalCounterOfTodayBBSInfo =bbsInfoManagerBean.doGetTodayAllBBSInfoCount(); catch (WebBBSException e)request.setAttribute("errorText", "出現(xiàn)如下的錯誤:" +e.getMessage(); return mapping.findForward("showWebAppError"); int totalBBSInfoCounter = 0; try totalBBSInfoCounter = bbsInfoManagerBean.doGetTotalBBSInfoCounter(); catch (WebBBSException e)request.setAttribute("errorText", "出現(xiàn)如下的錯誤:" +e.getMessage(); return mapping.findForward("showWebAppError"); int totalUserCounter = 0; try totalUserCounter = userInfoManageBean.doGetTotalRegisterUserCounter(); catch (WebBBSException e)request.setAttribute("errorText", "出現(xiàn)如下的錯誤:" +e.getMessage(); return mapping.findForward("showWebAppError"); UserInfoVO oneMaxIDUserInfoVO = null; try oneMaxIDUserInfoVO = userInfoManageBean.doGetMaxIDUserInfo(); catch (WebBBSException e)request.setAttribute("errorText", "出現(xiàn)如下的錯誤:" +e.getMessage(); return mapping.findForward("showWebAppError"); request.setAttribute("oneMaxIDUserInfoVO", oneMaxIDUserInfoVO); request.setAttribute("totalUserCounter", new Integer(totalUserCounter).toString(); request.setAttribute("totalBBSInfoCounter", new Integer(totalBBSInfoCounter).toString(); request.setAttribute("titleInfoVOArrayList_10", titleInfoVOArrayList_10); request.setAttribute("totalCounterOfTitleInfo", new Integer(totalCounterOfTitleInfo). toString(); request.setAttribute("totalCounterOfTodayBBSInfo", new Integer(totalCounterOfTodayBBSInfo). toString(); request.setAttribute("allHothitsBBSInfoVOArrayList",allHothitsBBSInfoVOArrayList);return mapping.findForward("showIndexContent");public ActionForward showRegisterForm(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)this.saveToken(request); /(1)return mapping.findForward("showRegisterForm");public ActionForward showUserLogoutForDWR(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) HttpSession session=request.getSession(); session.removeAttribute("oneUserInfoVO"); session.invalidate(); return null;public ActionForward showUserLogout(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) HttpSession session=request.getSession(); session.removeAttribute("oneUserInfoVO");/ session.invalidate(); request.setAttribute("errorText","您已經(jīng)從本系統(tǒng)中在線退出,因此您將失除所有的權(quán)限。<br>如果您還需要做其它的行為,請重新登錄!"); return mapping.findForward("showWebAppError");public ActionForward showUpdateUserInfo(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) String targetPage=null; HttpSession session=request.getSession();/下面為跳轉(zhuǎn)到修改用戶的信息的頁面中 Object oneUserInfoVOObject=session.getAttribute("oneUserInfoVO"); UserInfoVO oneUserInfoVO=(UserInfoVO)oneUserInfoVOObject; Integer type_User_Admin=oneUserInfoVO.getType_User_Admin(); switch(type_User_Admin.intValue() case 1: /轉(zhuǎn)到前臺用戶 targetPage="showUpdateUserInfo" break; case 2: /轉(zhuǎn)到后臺管理員用戶 targetPage="showUpdateAdminUserInfo" break; return mapping.findForward(targetPage);public ActionForward doShowOnLineUserInfo(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) return mapping.findForward("showOnLineUserInfo");public ActionForward doGetUserPassWord(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) return mapping.findForward("doGetUserPassWord");public ActionForward goSendOrPreView(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) HttpSession session = request.getSession();UserInfoVO oneUserInfoVO=(UserInfoVO)session.getAttribute("oneUserInfoVO");String userName=oneUserInfoVO.getUserName();String userID=oneUserInfoVO.getId();DynaValidatorForm pageForwordActionForm = (DynaValidatorForm) form;String bbsTitleID=(String)pageForwordActionForm.get("bbsTitleID");String bbsTitleText=(String)pageForwordActionForm.get("bbsTitleText");try /*由于bbsTitleText參數(shù)是通過get方法傳遞來的,因此ActionServlet無法進行正確遞編碼轉(zhuǎn)換,需要手動轉(zhuǎn)換*/bbsTitleText=new String(bbsTitleText.getBytes("ISO-8859-1"),"gb2312"); catch (UnsupportedEncodingException e)request.setAttribute("errorText", "在PageForwordAction類中showDigestBBSInfo方法出現(xiàn)如下的錯誤:" +e.getMessage(); return mapping.findForward("showWebAppError");request.setAttribute("userName",userName);request.setAttribute("userID",userID);request.setAttribute("bbsTitleID",bbsTitleID);request.setAttribute("bbsTitleText",bbsTitleText);return mapping.findForward("doSendBBSInfo");public ActionForward goReplyOrPreView(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) HttpSession session = request.getSession();UserInfoVO oneUserInfoVO=(UserInfoVO)session.getAttribute("oneUserInfoVO");String userName=oneUserInfoVO.getUserName();String userID=oneUserInfoVO.getId();DynaValidatorForm pageForwordActionForm = (DynaValidatorForm) form;String bbsTitleID=(String)pageForwordActionForm.get("bbsTitleID");String bbsTitleText=(String)pageForwordActionForm.get("bbsTitleText");String bbsID=(String)pageForwordActionForm.get("bbsID");String bbsTitle=(String)pageForwordActionForm.get("bbsTitle");try /*由于bbsTitleText參數(shù)是通過get方法傳遞來的,因此ActionServlet無法進行正確遞編碼轉(zhuǎn)換,需要手動轉(zhuǎn)換*/bbsTitleText=new String(bbsTitleText.getBytes("ISO-8859-1"),"gb2312"); catch (UnsupportedEncodingException e)request.setAttribute("errorText", "在PageForwordAction類中showDigestBBSInfo方法出現(xiàn)如下的錯誤:" +e.getMessage(); return mapping.findForward("showWebAppError");request.setAttribute("userName",userName);request.setAttribute("userID",userID);request.setAttribute("bbsTitle",bbsTitle);request.setAttribute("bbsID",bbsID);request.setAttribute("bbsTitleID",bbsTitleID);request.setAttribute("bbsTitleText",bbsTitleText);return mapping.findForward("doReplyBBSInfo");public ActionForward doSystemManage(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) return mapping.findForward("doSystemManage");public ActionForward doReplyBBSInfo(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)return mapping.findForward("doReplyBBSInfo");public ActionForward showAllClassInfo(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) /newUserAndBBSManageBean();/創(chuàng)建用戶和BBS信息管理業(yè)務(wù)類的對象實例ArrayList allBBSTitleVOArrayList=null;tryallBBSTitleVOArrayList=bbsInfoManagerBean.doGetBBSTitleInfo();catch(WebBBSException e) request.setAttribute("errorText", "出現(xiàn)如下的錯誤:" +e.getMessage(); return mapping.findForward("showWebAppError");request.setAttribute("allBBSTitleVOArrayList",allBBSTitleVOArrayList);return mapping.findForward("showAllBBSClassInfo");public ActionForward showAllHotBBSInfo(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) ArrayList allBBSInfoVOList = null;int totalBBSInfoCounter, totalBBSInfoPageCounter;/newUserAndBBSManageBean();/創(chuàng)建用戶和BBS信息管理業(yè)務(wù)類的對象實例try /獲得總數(shù) int hotCounter=1; totalBBSInfoCounter = bbsInfoManagerBean.doGetTotalHotBBSInfoCounter(hotCounter); catch (WebBBSException e) request.setAttribute("errorText", "出現(xiàn)如下的錯誤:" +e.getMessage(); return mapping.findForward("showWebAppError");if(totalBBSInfoCounter=0)request.setAttribute("errorText", "很對不起!目前系統(tǒng)中還沒有產(chǎn)生熱門信息帖,我們會繼續(xù)努力的!"); return mapping.findForward("showWebAppError"); int currentPage = 1; /當(dāng)前正在顯示的頁數(shù)目 int firstResult = (currentPage - 1) * WebBankAllConstants.hotBBSCounterPerPageDigit; /根據(jù)當(dāng)前的頁數(shù)計算出的開始的行號(從0 計數(shù)) totalBBSInfoPageCounter = (totalBBSInfoCounter + WebBankAllConstants.hotBBSCounterPerPageDigit - 1) /WebBankAllConstants.hotBBSCounterPerPageDigit; int actualHotBBSCounterPerPageDigit=WebBankAllConstants.hotBBSCounterPerPageDigit; if (totalBBSInfoCounter < WebBankAllConstants.hotBBSCounterPerPageDigit) actualHotBBSCounterPerPageDigit = totalBBSInfoCounter; tryallBBSInfoVOList = bbsInfoManagerBean.doGetBBSInfoByHits(firstResult,actualHotBBSCounterPerPageDigit);catch (WebBBSException e) request.setAttribute("errorText", "出現(xiàn)如下的錯誤:" +e.getMessage(); return mapping.findForward("showWebAppError");request.setAttribute("allBBSInfoVOList", allBBSInfoVOList);request.setAttribute("totalBBSInfoPageCounter",new Integer(totalBBSInfoPageCounter).toString();request.setAttribute("currentPage", new Integer(currentPage).toString();return mapping.findForward("showAllHotBBSInfo");public ActionForward showAllBBSInClass(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)ArrayList allBBSInfoVOList = null;int totalBBSInfoCounter, totalBBSInfoPageCounter;/* * 本功能是通過get提交的請求,因此不能通過DynaValidatorForm獲得請求的參數(shù),而只能通過request */String bbsTitleID=request.getParameter("bbsTitleID");String bbsTitleText=request.getParameter("bbsTitleText");try /*由于bbsTitleText參數(shù)是通過get方法傳遞來的,因此ActionServlet無法進行正確遞編碼轉(zhuǎn)換,需要手動轉(zhuǎn)換*/bbsTitleText=new String(bbsTitleText.getBytes("ISO-8859-1"),"gb2312"); catch (UnsupportedEncodingException e) request.setAttribute("errorText", "在PageForwordAction類中showAllBBSInClass方法出現(xiàn)如下的錯誤:" +e.getMessage(); return mapping.findForward("showWebAppError");/newUserAndBBSManageBean();/創(chuàng)建用戶和BBS信息管理業(yè)務(wù)類的對象實例try /獲得總數(shù) totalBBSInfoCounter = bbsInfoManagerBean.doGetPageTotalBBSInfoCounterByTitleID(Integer.parseInt(bbsTitleID); catch (WebBBSException e) request.setAttribute("errorText", "出現(xiàn)如下的錯誤:" +e.getMessage(); return mapping.findForward("showWebAppError"); int currentPage = 1; /當(dāng)前正在顯示的頁數(shù)目/根據(jù)當(dāng)前的頁數(shù)計算出的開始的行號(從0 計數(shù)) int firstResult = (currentPage - 1) * WebBankAllConstants.objectCounterPerPageDigit; totalBBSInfoPageCounter = (totalBBSInfoCounter + WebBankAllConstants.objectCounterPerPageDigit - 1) /WebBankAllConstants.objectCounterPerPageDigit; int actualHotBBSCounterPerPageDigit=WebBankAllConstants.hotBBSCounterPerPageDigit; if (totalBBSInfoCounter < WebBankAllConstants.hotBBSCounterPerPageDigit) actualHotBBSCounterPerPageDigit = totalBBSInfoCounter; try allBBSInfoVOList = bbsInfoManagerBean.doGetPageBBSInfoByTitleID(Integer.parseInt(bbsTitleID), firstResult,actualHotBBSCounterPerPageDigit);catch (WebBBSException e) request.setAttribute("errorText", "出現(xiàn)如下的錯誤:" +e.getMessage(); return mapping.findForward("showWebAppError");/下面的代碼是獲得該分類的完整信息(也包括版主信息)BBSTitleVO oneBBSTitleVO = null;try oneBBSTitleVO = bbsInfoManagerBean.doGetBBSTitleInfoByTitleID(Integer.parseInt(bbsTitleID);catch (WebBBSException e) request.setAttribute("errorText", "出現(xiàn)如下的錯誤:" +e.getMessage(); return mapping.findForward("showWebAppError");request.setAttribute("allBBSInfoVOList", allBBSInfoVOList);request.setAttribute("bbsTitleID", bbsTitleID);request.setAttribute("bbsTitleText", bbsTitleText);request.setAttribute("oneBBSTitleVO", oneBBSTitleVO);request.setAttribute("totalBBSInfoPageCounter",new Integer(totalBBSInfoPageCounter).toString();request.setAttribute("currentPage", new Integer(currentPage).toString();return mapping.findForward("showAllBBSInClass");public ActionForward showOneBBSInfo(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) BBSInfoVO oneBBSInfoVO = null; ArrayList allBBSReplyInfoListByOneBBS = null; DynaValidatorForm pageForwordActionForm = (DynaValidatorForm) form;String bbsID=(String)pageForwordActionForm.get("bbsID");/獲得該BBS的ID號/獲得該BBS所在的分類的標題文字String bbsTitleText=(String)pageForwordActionForm.get("bbsTitleText"); /獲得該BBS所在的分類的ID String bbsTitleID=(String)pageForwordActionForm.