`
uule
  • 浏览: 6338100 次
  • 性别: Icon_minigender_1
  • 来自: 一片神奇的土地
社区版块
存档分类
最新评论

[基础]项目中Struts的配置

 
阅读更多

 

DispatchAction用于分发的Action,主要的好处是把一些功能类似的Action放到一个Action中,通过传入的不同参数来觉得执行哪个操作. 

是一个抽象类,它实现了父类(Action)的execute()方法,所以它的子类就不用来实现这个方法了,只需要专注与实际操作的方法


DispatchAction就是在struts-config中用parameter参数配置一个表单字段名,这个字段的值就是最终替代execute被调用的方法. 例如parameter="method"而request.getParameter("method")="save",其中"save"就是MethodName。struts的请求将根据parameter被分发到"save"或者"edit"或者什么。但是有一点,save()或者edit()等方法的声明和execute必须一模一样。 

即:

<action path="/admin/user" name="userForm" scope="request" parameter="method" validate="false">

    <forward name="list" path="/admin/userList.jsp"/>

</action>

    其中parameter="method" 设置了用来指定响应方法名的url参数名为method,即/admin/user.do?method=list 将调用UserAction的public ActionForward list(....) 函数。   

    public ActionForward unspecified(....) 函数可以指定不带method方法时的默认方法

也就是说  当你使用 DispatchAction 定义action的时候,如果没有指定method 方法,那么action  自动匹配到  unspecified  这个函数。

 

 

 

public class UpdateBookingOrderAction extends BaseDispathAction {
	@Override
	public String query(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws SystemException {
			
	}
	@Override
	public String update(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws SystemException {
	
	}
}

 

 

public abstract class BaseDispathAction extends DispatchAction {
	private static Log log = LogFactory.getLog(BaseDispathAction.class);

	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException {
		ActionForward actionForward = this.doInit("proccess", mapping, form, request, response);
		return actionForward;
	}

	public ActionForward query(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException {
		ActionForward actionForward = this.doInit("query", mapping, form, request, response);
		return actionForward;
	}

	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException {
		ActionForward actionForward = this.doInit("add", mapping, form, request, response);
		return actionForward;
	}

	public ActionForward read(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException {
		ActionForward actionForward = this.doInit("read", mapping, form, request, response);
		return actionForward;
	}

	public ActionForward doInit(String type, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException {
		HttpSession session = request.getSession();
		String id = session.getId();
		String remoteAddr = request.getRemoteAddr();
		String clientInfo = remoteAddr + "(" + id + ")";
		String strGotoPage = "success";
		try {
			if (type.equalsIgnoreCase("add")) {
				session.setAttribute("querystatus", "Add");
				strGotoPage = add(form, request, response);
			} else if (type.equalsIgnoreCase("update")) {
				session.setAttribute("querystatus", "Edit");
				strGotoPage = update(form, request, response);
			} else if (type.equalsIgnoreCase("addnew")) {
				session.setAttribute("querystatus", "Add");
				strGotoPage = addnew(form, request, response);
			} else if (type.equalsIgnoreCase("delete")) {
				session.setAttribute("querystatus", "Delete");
				strGotoPage = delete(form, request, response);
			} else if (type.equalsIgnoreCase("query")) {
				session.setAttribute("querystatus", "Query");
				strGotoPage = query(form, request, response);
			} else if (type.equalsIgnoreCase("read")) {
				session.setAttribute("querystatus", "Edit");
				strGotoPage = read(form, request, response);
			} else {// process
				session.setAttribute("querystatus", "Query");
				strGotoPage = process(form, request, response);
			}			
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
			throw new SystemException(e.getMessage());
		}
		ActionForward actionForward = mapping.findForward(strGotoPage);
		if (null != actionForward) {
			log.warn(clientInfo + "====> " + actionForward.getName() + ":" + actionForward.getPath());
		}
		return actionForward;
	}

	public abstract String process(ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException;
	public abstract String query(ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException;
	public abstract String add(ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException;
	public abstract String addnew(ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException;
	public abstract String read(ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException;
	public abstract String update(ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException;
	public abstract String delete(ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException;
}
 

JSP调用:

 

document.forms[0].action = "../updateBooking.do?method=updateStatus;
document.forms[0].submit();

 

或者:

public class ZonesAction extends BaseAction {
	
	@Override
	public String process(ActionForm form, HttpServletRequest request) throws Exception {

	}
}

public abstract class BaseAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession();
		String id = session.getId();
		String remoteAddr = request.getRemoteAddr();
		String clientInfo = remoteAddr + "(" + id + ")";
		log = LogFactory.getLog(this.getClass());
		String strGotoPage;
		try {
			strGotoPage = process(form,request);
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
			throw e;
		}
		ActionForward actionForward = mapping.findForward(strGotoPage);
		log.warn(clientInfo +"====> "+ actionForward.getName() + ":" + actionForward.getPath());
		return actionForward;
	}
	
	public abstract String process(ActionForm form, HttpServletRequest request)throws Exception;
	
}
 

 

DispatchAction, LookupDispatchAction, MappingDispatchAction深入分析

http://qmug.iteye.com/blog/215456

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    在Eclipse中配置Struts2项目

    在Eclipse中配置Struts2项目是一个涉及到多个步骤和技术组件的过程。本文将详细介绍如何在Eclipse集成开发环境中配置Struts2项目,同时考虑到JDK1.4的兼容性。Struts2是一个流行的Java web框架,它提供了MVC设计模式...

    struts2+jpa+spring2.5配置基础框架

    在这个基础框架的配置中,我们可能还需要考虑以下方面: 1. **Spring的AOP配置**:利用Spring的AOP,我们可以实现全局事务管理和日志记录等功能,只需在配置文件中声明相应的切面。 2. **数据源配置**:为了连接...

    MyEclipse+struts+Hibernate配置开发

    在MyEclipse中,可以通过向项目中添加Struts Capabilities来快速设置Struts框架,包括配置struts-config.xml文件、创建Action类等。 **Hibernate**则是一个对象关系映射(ORM)框架,它简化了数据库操作,将Java...

    struts+hibernate 项目

    在这个“Struts+Hibernate项目”中,用户登录功能是基础,这通常涉及到用户验证和权限控制。开发者可能使用Struts的ActionForm收集用户输入,然后通过Hibernate查询数据库进行身份验证。数据的显示可能涉及到多个...

    MyEclipse_如何配置struts2

    在IT领域,特别是Java开发中,Struts2框架是一个非常重要的MVC(Model-View-Controller)框架,它简化了Web应用的开发...希望上述信息能够帮助你在MyEclipse中成功配置Struts2,为后续的Java Web开发打下坚实的基础。

    struts2初始使用环境配置

    理解并掌握这些步骤是Java EE开发中必不可少的一部分,它为你后续深入学习Struts2和构建复杂Web应用打下基础。在实际开发中,你可能还需要了解更多关于Struts2的特性,如拦截器、插件、国际化、异常处理等内容。

    徒手配置Struts2

    配置Struts2环境的过程中,合理规划项目文件夹结构是基础。以下是推荐的文件夹结构示例: ``` test |— WEB-INF ||— classes(struts.xml、LoginAction.java等Java类) ||— lib(包含struts2-core-2.3.1.2.jar、...

    struts环境配置手记

    ### Struts环境配置详解 #### 一、Struts在Eclipse中的配置...通过以上步骤,你可以成功地在Eclipse环境中搭建Struts开发框架,并配置好与SQL Server 2000数据库的连接,为Java Web应用程序的开发奠定了坚实的基础。

    maven项目中struts的整合

    在Java Web开发中,Maven和...这只是一个基础的配置,实际应用中可能需要进一步配置拦截器、全局结果、异常处理等高级特性,以满足复杂的业务需求。记得始终关注框架的更新和最佳实践,确保代码的安全性和可维护性。

    struts2项目实例

    总的来说,"Struts2项目实例"涵盖了Java web开发中的关键环节,从Action设计、数据库操作、视图展现到框架配置,为我们提供了一个学习和实践Struts2的好起点。通过分析和理解这个实例,开发者可以更好地掌握Struts2...

    struts2 项目源码

    本项目源码提供了一个基础的Struts2应用程序实例,对于初学者来说,这是一个很好的学习资源,可以深入理解Struts2的工作原理和架构。 Struts2的核心组件包括: 1. **Action类**:Action类是业务逻辑的载体,它是...

    eclipse 配置struts2

    - 在Eclipse中,右键点击项目 -&gt; Build Path -&gt; Configure Build Path -&gt; Libraries -&gt; Add JARs 或 Add External JARs,将下载的Struts2库导入到项目的类路径中。 2. **创建Struts2配置文件** - 在WebContent...

    SSH笔记_Struts2配置

    - **使用拦截器**:在Action配置中通过`&lt;interceptor-ref&gt;`引用拦截器栈或单独的拦截器。 ```xml ``` 6. **常量配置** - **struts.properties或struts.xml**:定义全局常量,如字符编码、错误...

    struts配置文件

    了解了这些基础知识后,你可以根据自己的需求在`struts.xml`或其他配置文件中添加、修改或删除Action,配置拦截器,以及设置常量,从而定制Struts 2的行为。同时,你还可以参考博客中的详细内容,获取更深入的理解和...

    struts1和struts2项目实例

    在这个项目中,开发者可能已经配置好了Struts2的动作(Action)和结果页面,Spring的Bean配置,以及Hibernate的数据访问层。文件"ssh_2"可能代表了另一个版本或不同实现的SSH项目。 学习和实践这两个框架,开发者...

    Struts2Review项目

    项目中的文件可能包括Action类、JSP页面、配置文件(如struts.xml)、测试用例等。Action类通常会处理业务逻辑并返回结果;JSP页面则负责展示视图,通常通过Struts2的标签库简化视图层的开发;配置文件定义了Action...

    struts配置文件讲解

    理解并熟练掌握Struts配置文件的各个元素是开发高效、稳定Struts应用的基础。通过精心设计和维护配置文件,我们可以实现更灵活的控制流、更好的数据管理以及更优雅的错误处理。在实际项目中,还需要结合实际需求和...

    struts2配置环境需要的jar包

    在使用Struts2开发项目时,环境配置是至关重要的第一步。以下将详细介绍如何配置Struts2所需的jar包,以及如何将这些jar包应用于你的项目。 首先,`struts2配置环境需要的jar包`意味着你需要确保你的开发环境中包含...

Global site tag (gtag.js) - Google Analytics