分享
 
 
 

Struts快速学习指南6(内部培训教材)-大部分素材来自于《Programming Jakarta Struts》一书

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

1.1.1.1 Action类

如果说ActionServlet是Struts框架的入口,RequestProcessor是消化过滤系统,则org.apache.struts.action.Action类可以说是整个框架的心脏。他是客户请求和业务操作的连接桥,也可以将其看作是业务操作的客户代理。

在前面对ReqeustProcessor类的学习中,我们了解到一旦确定并得到了一个action实例,ReqeustProcessor会调用action的execute()方法处理客户请求,你需要扩展action类,并实现它的execute()方法,在此方法中添加你自己的处理代码。下面给出是一个示例,这个action用来处理用户的登录请求:

package com.oreilly.struts.storefront.security;

import java.util.Locale;

import javax.servlet.http.*;

import org.apache.struts.action.*;

import com.oreilly.struts.storefront.customer.view.UserView;

import com.oreilly.struts.storefront.framework.exceptions.BaseException;

import com.oreilly.struts.storefront.framework.UserContainer;

import com.oreilly.struts.storefront.framework.StorefrontBaseAction;

import com.oreilly.struts.storefront.framework.util.IConstants;

import com.oreilly.struts.storefront.service.IStorefrontService;

/**

* Implements the logic to authenticate a user for the Storefront application.

*/

public class LoginAction extends StorefrontBaseAction {

/**

* Called by the controller when the user attempts to log in to the

* Storefront application.

*/

public ActionForward execute( ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response )

throws Exception{

// The email and password should have already been validated by the ActionForm

String email = ((LoginForm)form).getEmail( );

String password = ((LoginForm)form).getPassword( );

// Log in through the security service

IStorefrontService serviceImpl = getStorefrontService( );

UserView userView = serviceImpl.authenticate(email, password);

// Create a single container object to store user data

UserContainer existingContainer = null;

HttpSession session = request.getSession(false);

if ( session != null ){

existingContainer = getUserContainer(request);

session.invalidate( );

}else{

existingContainer = new UserContainer( );

}

// Create a new session for the user

session = request.getSession(true);

// Store the UserView in the container and store the container in the session

existingContainer.setUserView(userView);

session.setAttribute(IConstants.USER_CONTAINER_KEY, existingContainer);

// Return a Success forward

return mapping.findForward(IConstants.SUCCESS_KEY);

}

}

1.1.1.1.1 Action类缓冲

Action类被设计为线程安全的,在每个应用中每个Action类只会被实例化一次,供所有线程共享。RequestProcessor利用一个HashMap用来保存Action实例。

思考题?

所有线程共享一个Action类实例意味着什么?我们在编程中需要注意些什么呢?

1.1.1.2 ActionForward类

从前面的介绍我们已经了解到,Action的execute( )方法返回一个ActionForward对象。ActionForward对象是JSP页面、Java servlet等web资源的抽象表现。

ActionForward的用途是为了减少应用和物理资源(JSP页面,Java servlet)的耦合,物理资源只需要在配置文件中指定(利用name,path属性和forward元素的redirect属性),而不是在代码中指定。RequestDispatcher利用ActionForward来执行重定向操作。

要在Action中返回一个ActionForward对象,你可以动态地创建一个ActionForward 对象,不过更为通用的解决方案是,通过在Struts配置文件中进行action映射,然后通过关键字去查找一个ActionForward 。下面是代码示例:

return mapping.findForward( "Success" );

上面的代码中,"Success"作为参数被传递到ActionMapping的findFoward( )方法中,findFoward( )方法在Struts配置文件的global-forwards区域,以及被调用的action的forward元素中查找名字和"Success"相匹配的元素。下面是action元素中的forward示例:

<action

input="/security/signin.jsp"

name="loginForm"

path="/signin"

scope="request"

type="com.oreilly.struts.storefront.security.LoginAction"

validate="true">

<forward name="Success" path="/index.jsp" redirect="true"/>

<forward name="Failure" path="/security/signin.jsp" redirect="true"/>

</action>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有