《基于J2EE Struts框架的课程设计实训项目——《BBS论坛系统》——构建控制层中实现页面跳转的 Action程序类.doc》由会员分享,可在线阅读,更多相关《基于J2EE Struts框架的课程设计实训项目——《BBS论坛系统》——构建控制层中实现页面跳转的 Action程序类.doc(29页珍藏版)》请在三一文库上搜索。
1、杨教授大学堂 精心创作的优秀程序员 职业提升必读系列资料基于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) /WebB
2、BS/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;
3、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.validato
4、r.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.UserInf
5、oManageInterface;import com.px1987.webbbs.model.UserInfoVO;public class PageForwordAction extends DispatchAction BBSInfoManageInterface bbsInfoManagerBean=null;UserInfoManageInterface userInfoManageBean=null;public void setBbsInfoManagerBean(BBSInfoManageInterface bbsInfoManagerBean) this.bbsInfoMan
6、agerBean = bbsInfoManagerBean;public void setUserInfoManageBean(UserInfoManageInterface userInfoManageBean) this.userInfoManageBean = userInfoManageBean;public PageForwordAction() /本构造方法是在利用 Spring IOC获得目标对象时被应用super();/* String bbsInfoManageBeanClassName=null;String userInfoManageBeanClassName=null
7、;public PageForwordAction() throws WebBBSException /本构造方法是在利用 可配置化的工厂时被应用super();newUserAndBBSManageBean();public void newUserAndBBSManageBean() throws WebBBSExceptionbbsInfoManageBeanClassName=ClassNameConfig.getProperty(bbsInfoManageImple.className);bbsInfoManagerBean=BBSInfoManageFactory.newBBSIn
8、foManageBean(bbsInfoManageBeanClassName);userInfoManageBeanClassName=ClassNameConfig.getProperty(userInfoManageImple.className);userInfoManageBean=UserInfoManageFactory.newUserInfoManageBean(userInfoManageBeanClassName);*/public ActionForward indexShowPattern_BarMethod(ActionMapping mapping, ActionF
9、orm 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(showIn
10、dexContent_PlainMethod);public ActionForward indexLeftMenuBar(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)return mapping.findForward(showIndexLeftMenuBar);public ActionForward indexSwitchMenu(ActionMapping mapping, ActionForm form,HttpServletReques
11、t request, HttpServletResponse response)return mapping.findForward(showIndexSwitchMenu);public ActionForward indexDefaultContent(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)return mapping.findForward(showIndexDefaultContent);public ActionForward sh
12、owIndexContent(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)ArrayList titleInfoVOArrayList_10 = null;ArrayList allHothitsBBSInfoVOArrayList = null;int totalCounterOfTitleInfo;int totalCounterOfTodayBBSInfo;int titleCounte=10;/只返回前10个分类元素对象try/newUse
13、rAndBBSManageBean();/创建用户和BBS信息管理业务类的对象实例titleInfoVOArrayList_10 = bbsInfoManagerBean.doGetBBSTitleInfo(titleCounte); catch (WebBBSException e) request.setAttribute(errorText, 出现如下的错误: +e.getMessage(); return mapping.findForward(showWebAppError);/获得总的分类数目trytotalCounterOfTitleInfo = bbsInfoManagerBe
14、an.doGetTotalBBSTitleInfoCounter(); catch (WebBBSException e)request.setAttribute(errorText, 出现如下的错误: +e.getMessage(); return mapping.findForward(showWebAppError);int hitBBSCounte=10;/只返回前10个热门的BBS信息tryallHothitsBBSInfoVOArrayList = bbsInfoManagerBean.doGetBBSInfoByHits(hitBBSCounte); catch (WebBBSE
15、xception 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)
16、 + - + rightNow.getDate();/* try/此时在ArrayList中将包含有今天的新贴(BBSInfoVO类型)的对象集合 newBBSInfoList = bbsInfoManageImple.doGetTodayAllBBSInfo(); catch (WebBBSException e)request.setAttribute(errorText, 出现如下的错误: +e.getMessage();oneRequestDispatcher=request.getRequestDispatcher(/errorDeal/showWebAppError.jsp); o
17、neRequestDispatcher.forward(request, response); return; totalCounterOfTodayBBSInfo = newBBSInfoList.size();*/ try totalCounterOfTodayBBSInfo =bbsInfoManagerBean.doGetTodayAllBBSInfoCount(); catch (WebBBSException e)request.setAttribute(errorText, 出现如下的错误: +e.getMessage(); return mapping.findForward(
18、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 = use
19、rInfoManageBean.doGetTotalRegisterUserCounter(); catch (WebBBSException e)request.setAttribute(errorText, 出现如下的错误: +e.getMessage(); return mapping.findForward(showWebAppError); UserInfoVO oneMaxIDUserInfoVO = null; try oneMaxIDUserInfoVO = userInfoManageBean.doGetMaxIDUserInfo(); catch (WebBBSExcept
20、ion 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 In
21、teger(totalBBSInfoCounter).toString(); request.setAttribute(titleInfoVOArrayList_10, titleInfoVOArrayList_10); request.setAttribute(totalCounterOfTitleInfo, new Integer(totalCounterOfTitleInfo). toString(); request.setAttribute(totalCounterOfTodayBBSInfo, new Integer(totalCounterOfTodayBBSInfo). toS
22、tring(); request.setAttribute(allHothitsBBSInfoVOArrayList,allHothitsBBSInfoVOArrayList);return mapping.findForward(showIndexContent);public ActionForward showRegisterForm(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)this.saveToken(request); /(1)ret
23、urn 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;pub
24、lic ActionForward showUserLogout(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) HttpSession session=request.getSession(); session.removeAttribute(oneUserInfoVO);/ session.invalidate(); request.setAttribute(errorText,您已经从本系统中在线退出,因此您将失除所有的权限。如果您还需要做其它
25、的行为,请重新登录!); return mapping.findForward(showWebAppError);public ActionForward showUpdateUserInfo(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) String targetPage=null; HttpSession session=request.getSession();/下面为跳转到修改用户的信息的页面中 Object oneUserInfoVOOb
26、ject=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=showUpdateAdminUserInf
27、o; 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, Act
28、ionForm form,HttpServletRequest request, HttpServletResponse response) return mapping.findForward(doGetUserPassWord);public ActionForward goSendOrPreView(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) HttpSession session = request.getSession();UserIn
29、foVO 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=(S
30、tring)pageForwordActionForm.get(bbsTitleText);try /*由于bbsTitleText参数是通过get方法传递来的,因此ActionServlet无法进行正确递编码转换,需要手动转换*/bbsTitleText=new String(bbsTitleText.getBytes(ISO-8859-1),gb2312); catch (UnsupportedEncodingException e)request.setAttribute(errorText, 在PageForwordAction类中showDigestBBSInfo方法出现如下的错误:
31、 +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 goReply
32、OrPreView(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
33、();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)pageForwordActionFor
34、m.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 mappi
35、ng.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
36、(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, Http
37、ServletResponse response)return mapping.findForward(doReplyBBSInfo);public ActionForward showAllClassInfo(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) /newUserAndBBSManageBean();/创建用户和BBS信息管理业务类的对象实例ArrayList allBBSTitleVOArrayList=null;tryallBBSTi
38、tleVOArrayList=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;