- 4
- 0
- 约6.89千字
- 约 57页
- 2017-06-05 发布于四川
- 举报
章节目录 Action接口 Action类的方法 Action的调用 Action接口 Struts2中的Action不需要实现任何接口 Struts2的API中提供了com.opensymphony.xwork2.Action接口,接口中提供了常量以及execute方法 Action类可以实现Action接口 Action类中的方法 默认情况下,Action类的方法必须符合如下声明形式 public String execute() throws Exception { } Action类的方法名字可以不是execute,但是必须在struts.xml中进行指定 action name=Register class=com.etc.action.RegisterAction method=register “合并”多个Action类 可以在一个Action中声明多个方法,如login、register等,必须在struts.xml中进行配置 action name=Customerlogin class=com.etc.action.CustomerAction method=login result name=success/welcome.jsp/result result name=fail/index.jsp/result /action action name=Customerregister class=com.etc.action.CustomerAction method=register …… Action类中定义了execute方法 如果Action中定义了符合规范的execute方法,那么就默认调用execute方法 action name=Login class=com.etc.action.LoginAction result name=success/welcome.jsp/result result name=fail/index.jsp/result /action Action类中的方法名不是execute 如果Action中的方法名不是execute,而是自定义方法,有两种方法可以调用Action类 在struts.xml中通过method属性,指定方法名 使用提交按钮的method属性,指定Action的方法名 章节目录 ActionContext ServletActionContext ActionContext概述 ActionContext类表示一个Action运行时的上下文 Struts2应用中,如果需要通过请求、会话、上下文存取属性,可以使用ActionContext完成,而不必调用Servlet API 获得ActionContext对象的方法 使用ActionContext存取请求属性 ActionContext中提供了两个方法,可以向请求范围存取属性 public void put(Object key, Object value) public Object get(Object?key) 使用ActionContext存取会话属性 ActionContext中提供了如下方法,获得与会话有关的Map public Map getSession() 使用ActionContext存取上下文属性 ActionContext中提供了如下方法,获得与上下文有关的Map public Map getApplication() 使用ServletActionContext 使用ServletActionContext可以获得真正的请求、会话、上下文对象 public static PageContext getPageContext():获得PageContext对象 public static HttpServletRequest getRequest():获得HttpServletRequest对象 public static HttpServletResponse getResponse():获得HttpServletResponse对象 public static ServletContext getServletContext():获得ServletContext对象 ActionContext使用实例 通过向“教程案例”增加查看所有用户信息的功能,使用ActionContext 调用ActionContext对象的put方法,往请求范围添加属性 章节目录 Field-driven方式 Model-driven方式 Field-Driven方式 Action通过属性的方式封装请求参数,被称为Field-Driven方式,即域驱动 API中提供了一个拦截器
原创力文档

文档评论(0)