您的位置:首页 > 编程语言 > Java开发

struts中常用的action

2012-05-09 18:29 375 查看

====Action

====ForwardAction:

·就是在项目中不出现*.jsp 全部都是*.do

·或者jsp页面在WEB-INF里面,不能访问

就可以在struts-config.xml里面配置:

<action path="/..." type="org.apache.struts.actions.ForwardAction" parameter="路径"/>

和下面的配置效果是一样的

<action path="/login" forward="/MyJsp.jsp"/>

起到 了一个中转站的作用

====DispatcherAction

不要重写里面的execute方法

派发:

在<action path="/login"...... parameter="method" />

实现类要继承DispatcherAction

访问的时候通过 工程名?login.do&method=name

这个name就是在Dispatcher里面的方法

假如说:对于这个dispatchAction里面多个方法,有些方法不需要进行表单验证,但是在struts-config.xml里面配置了要使用表单验证,所以当请求到不需要进行表单验证的action方法的时候,先要通过formbean的验证方法,而这个请求是不需要formbean的,所以会出现空指针异常

所以要建立两个action,一个放置需要进行表单验证的,一个放置不需要表单验证的

MappingDispatchAction

针对每一个action方法要配置一个<action >

· public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception

· public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception

· public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception

· public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception

· public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception

for which you would create corresponding <action> configurations that reference this class:

<action path="/createSubscription"

type="org.example.SubscriptionAction"

parameter="create">

<forward name="success" path="/editSubscription.jsp"/>

</action>

<action path="/editSubscription"

type="org.example.SubscriptionAction"

parameter="edit">

<forward name="success" path="/editSubscription.jsp"/>

</action>

<action path="/saveSubscription"

type="org.example.SubscriptionAction"

parameter="save"

name="subscriptionForm"

validate="true"

input="/editSubscription.jsp"

scope="request">

<forward name="success" path="/savedSubscription.jsp"/>

</action>

<action path="/deleteSubscription"

type="org.example.SubscriptionAction"

name="subscriptionForm"

scope="request"

input="/subscription.jsp"

parameter="delete">

<forward name="success" path="/deletedSubscription.jsp"/>

</action>

<action path="/listSubscriptions"

type="org.example.SubscriptionAction"

parameter="list">

<forward name="success" path="/subscriptionList.jsp"/>

</action>

使用的话要注意几个地方:

<action >里面的parameter的配置值是MappingDispacheAction里面的方法名对应,在页面可以通过

XXX.do?method='配置文件中的parameter值'

当在使用Spring的整合的时候,用MappingDispatchAction的时候需要在Spring的配置文件中ApplicationContext.xml中要实例化一个bean

====DownLoadAction

首先写个类,继承DownLoadAction ,实现getStreamInfo这个方法

public class HwtDownLoadAction extends DownloadAction {

protected StreamInfo getStreamInfo(ActionMapping arg0, ActionForm arg1,

HttpServletRequest request, HttpServletResponse response)

throws Exception {

String filename = request.getParameter("filename");

String realpath = this.getServlet().getServletContext().getRealPath(

"/upload");

File file = new File(realpath + "/" + filename);

if (file.exists()) {

//操作,声明文件的名字

response.addHeader("Content-Disposition", "attachment;filename="

+ URLEncoder.encode(filename, "utf-8"));

//(类型,servletContext(),路径)

return new DownloadAction.ResourceStreamInfo("application/zip",

this.getServlet().getServletContext(), "/upload/"

+ filename);

} else {

return null;

}

}

}

配置文件中:

<action path="/download" type="hwt.cmp.action.HwtDownLoadAction"/>

就直接通过 download.do?filename=fffff.zip直接方法

====LocaleAction

localeaction直接配置一下就可以使用:

使用LocaleAction必须使用一个form来接收language, country ,page的数据

我们使用一个dynamicform

<form-bean name="languageForm" type="org.apache.struts.action.DynaActionForm">

<form-property name="language" type="java.lang.String"/>

<form-property name="country" type="java.lang.String"/>

<form-property name="page" type="java.lang.String"/>

</form-bean>

<action path="/changelanguage" scope="request" name="languageForm" type="org.apache.struts.actions.LocaleAction" />

页面:(这样是可以填充dynamicformbean的)

<a href="changelanguage.do?language=zh&country=CN&page=/WEB-INF/page/register.jsp">简体中文</a>

<a href="changelanguage.do?language=en&country=US&page=/WEB-INF/page/register.jsp">English</a>

=====LookupAction

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: