gavanwanggw

导航

struts1.3中使用DispatchAction的一个问题

近期做项目发现我们公司的项目是用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方法


文章能够写得不够完整。怎样疑问,请留言


posted on 2017-05-02 21:49  gavanwanggw  阅读(135)  评论(0编辑  收藏  举报