最近做项目发现我们公司的项目是用struts1写的,在多方百度下,总有理解了struts1.3的DispatchAction的用法
一:struts.xml文件的配置
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="messageBoardFrom" type="com.jobe23.struts.form.message.MessageBoardForm"> </form-bean> </form-beans> <action-mappings> <action path="/zc/graduate/index" type="com.jobe23.struts.action.message.MessageBoardAction" name="messageBoardFrom" parameter="action"> <forward name="successMsg" path="/zc/graduate/index.jsp" /> <forward name="retention" path="/zc/graduate/retention.jsp" /> </action> </action-mappings> </struts-config>
二:action的配置
package com.jobe23.struts.action.message; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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.jobe23.dao.message.MessageBoardDAO; import com.jobe23.entity.message.MessageBoard; /** * 专场留言 * @author clyao * @param mapping * @param form * @param request * @param response * @return * @throws Exception */ public class MessageBoardAction extends DispatchAction { public ActionForward message(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { MessageBoard mb = new MessageBoard(); MessageBoardDAO msgdao = new MessageBoardDAO(); List<MessageBoard> msgList = new ArrayList<MessageBoard>(); String mobile = request.getParameter("mobile"); String msgContent = request.getParameter("msgContent"); String specialTitle = request.getParameter("specialTitle"); mb.setMobile(mobile); mb.setMsgContent(msgContent); mb.setSpecialTitle(specialTitle); try { if(mobile==null || mobile.equals(null)){ }else{ msgdao.save(mb); } msgList = msgdao.findAll(); request.setAttribute("msgList", hideMobile(msgList)); } catch (Exception e) { e.printStackTrace(); } return mapping.findForward("successMsg"); } }
特别注意:这里的类必须继承DispatchAction,否则无法跳转到对应的方法
三:测试一下
http://localhost:8080/zc/graduate/index.do?action=message
它就会执行对应的message方法
文章可以写得不够完整,如何疑问,请留言
时间: 2024-10-09 08:08:29