资源描述
杨教授大学堂 精心创作的优秀程序员 职业提升必读系列资料基于J2EE Struts框架的课程设计实训项目BBS论坛系统构建控制层中实现页面跳转的 Action程序类1.1.1 页面跳转的 Action程序类PageForwordAction1、添加一个实现页面跳转的 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)也就是下面的状态 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() /本构造方法是在利用 Spring IOC获得目标对象时被应用super();/* String bbsInfoManageBeanClassName=null;String userInfoManageBeanClassName=null;public PageForwordAction() throws WebBBSException /本构造方法是在利用 可配置化的工厂时被应用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();/创建用户和BBS信息管理业务类的对象实例titleInfoVOArrayList_10 = bbsInfoManagerBean.doGetBBSTitleInfo(titleCounte); catch (WebBBSException e) request.setAttribute(errorText, 出现如下的错误: +e.getMessage(); return mapping.findForward(showWebAppError);/获得总的分类数目trytotalCounterOfTitleInfo = bbsInfoManagerBean.doGetTotalBBSTitleInfoCounter(); catch (WebBBSException e)request.setAttribute(errorText, 出现如下的错误: +e.getMessage(); return mapping.findForward(showWebAppError);int hitBBSCounte=10;/只返回前10个热门的BBS信息tryallHothitsBBSInfoVOArrayList = bbsInfoManagerBean.doGetBBSInfoByHits(hitBBSCounte); catch (WebBBSException e)request.setAttribute(errorText, 出现如下的错误: +e.getMessage(); return mapping.findForward(showWebAppError);/* 下面的代码是获得今日帖数目*/ 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, 出现如下的错误: +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, 出现如下的错误: +e.getMessage(); return mapping.findForward(showWebAppError); int totalBBSInfoCounter = 0; try totalBBSInfoCounter = bbsInfoManagerBean.doGetTotalBBSInfoCounter(); catch (WebBBSException e)request.setAttribute(errorText, 出现如下的错误: +e.getMessage(); return mapping.findForward(showWebAppError); int totalUserCounter = 0; try totalUserCounter = userInfoManageBean.doGetTotalRegisterUserCounter(); catch (WebBBSException e)request.setAttribute(errorText, 出现如下的错误: +e.getMessage(); return mapping.findForward(showWebAppError); UserInfoVO oneMaxIDUserInfoVO = null; try oneMaxIDUserInfoVO = userInfoManageBean.doGetMaxIDUserInfo(); catch (WebBBSException e)request.setAttribute(errorText, 出现如下的错误: +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,您已经从本系统中在线退出,因此您将失除所有的权限。如果您还需要做其它的行为,请重新登录!); return mapping.findForward(showWebAppError);public ActionForward showUpdateUserInfo(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) String targetPage=null; HttpSession session=request.getSession();/下面为跳转到修改用户的信息的页面中 Object oneUserInfoVOObject=session.getAttribute(oneUserInfoVO); UserInfoVO oneUserInfoVO=(UserInfoVO)oneUserInfoVOObject; Integer type_User_Admin=oneUserInfoVO.getType_User_Admin(); switch(type_User_Admin.intValue() case 1: /转到前台用户 targetPage=showUpdateUserInfo; break; case 2: /转到后台管理员用户 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参数是通过get方法传递来的,因此ActionServlet无法进行正确递编码转换,需要手动转换*/bbsTitleText=new String(bbsTitleText.getBytes(ISO-8859-1),gb2312); catch (UnsupportedEncodingException e)request.setAttribute(errorText, 在PageForwordAction类中showDigestBBSInfo方法出现如下的错误: +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参数是通过get方法传递来的,因此ActionServlet无法进行正确递编码转换,需要手动转换*/bbsTitleText=new String(bbsTitleText.getBytes(ISO-8859-1),gb2312); catch (UnsupportedEncodingException e)request.setAttribute(errorText, 在PageForwordAction类中showDigestBBSInfo方法出现如下的错误: +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();/创建用户和BBS信息管理业务类的对象实例ArrayList allBBSTitleVOArrayList=null;tryallBBSTitleVOArrayList=bbsInfoManagerBean.doGetBBSTitleInfo();catch(WebBBSException e) request.setAttribute(errorText, 出现如下的错误: +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();/创建用户和BBS信息管理业务类的对象实例try /获得总数 int hotCounter=1; totalBBSInfoCounter = bbsInfoManagerBean.doGetTotalHotBBSInfoCounter(hotCounter); catch (WebBBSException e) request.setAttribute(errorText, 出现如下的错误: +e.getMessage(); return mapping.findForward(showWebAppError);if(totalBBSInfoCounter=0)request.setAttribute(errorText, 很对不起!目前系统中还没有产生热门信息帖,我们会继续努力的!); return mapping.findForward(showWebAppError); int currentPage = 1; /当前正在显示的页数目 int firstResult = (currentPage - 1) * WebBankAllConstants.hotBBSCounterPerPageDigit; /根据当前的页数计算出的开始的行号(从0 计数) 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, 出现如下的错误: +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获得请求的参数,而只能通过request */String bbsTitleID=request.getParameter(bbsTitleID);String bbsTitleText=request.getParameter(bbsTitleText);try /*由于bbsTitleText参数是通过get方法传递来的,因此ActionServlet无法进行正确递编码转换,需要手动转换*/bbsTitleText=new String(bbsTitleText.getBytes(ISO-8859-1),gb2312); catch (UnsupportedEncodingException e) request.setAttribute(errorText, 在PageForwordAction类中showAllBBSInClass方法出现如下的错误: +e.getMessage(); return mapping.findForward(showWebAppError);/newUserAndBBSManageBean();/创建用户和BBS信息管理业务类的对象实例try /获得总数 totalBBSInfoCounter = bbsInfoManagerBean.doGetPageTotalBBSInfoCounterByTitleID(Integer.parseInt(bbsTitleID); catch (WebBBSException e) request.setAttribute(errorText, 出现如下的错误: +e.getMessage(); return mapping.findForward(showWebAppError); int currentPage = 1; /当前正在显示的页数目/根据当前的页数计算出的开始的行号(从0 计数) 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, 出现如下的错误: +e.getMessage(); return mapping.findForward(showWebAppError);/下面的代码是获得该分类的完整信息(也包括版主信息)BBSTitleVO oneBBSTitleVO = null;try oneBBSTitleVO = bbsInfoManagerBean.doGetBBSTitleInfoByTitleID(Integer.parseInt(bbsTitleID);catch (WebBBSException e) request.setAttribute(errorText, 出现如下的错误: +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.
展开阅读全文