在 Struts1 中,如何在 action 标签中使用 set-property 标签? [英] In Struts1, how to use set-property tag inside action tag?

查看:22
本文介绍了在 Struts1 中,如何在 action 标签中使用 set-property 标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在使用 struts1 配置文件调用时传递一个值.我创建了一个具有以下属性的表单 bean

I want to pass a value in action when it is called using struts1 configuration file. I have create a form bean with following property

public class MyForm extends ActionForm {
    private String task;  

    public String getTask() {
        return task;
    }
    public void setTask(String task) {
        this.task = task;
    }
}

在 struts-config.xml 中,我定义了 form bean 和 action 如下.

In struts-config.xml, I have defined form bean and action as below.

<form-bean name="myForm" type="demo.MyForm"></form-bean>
<action path="/myAction" name="myForm" type="demo.MyAction" scope="request">
    <set-property value="view" property="task" />
    <forward name="success" path="/result.jsp"></forward>
</action>

我正在尝试使用这些配置在 web sphere 6.1 中运行它,它给出了以下异常

I am trying to run it in web sphere 6.1 with these configurations, it gives following exception

Deregister the mbean because of uncaught init() exception thrown by servlet action: javax.servlet.UnavailableException: Parsing error processing resource path file:/D:/workspaces/j-space/myProject/Web Content/WEB-INF/struts-config.xml
at org.apache.struts.action.ActionServlet.handleConfigException(ActionServlet.java:761)
at org.apache.struts.action.ActionServlet.parseModuleConfigFile(ActionServlet.java:744)
at org.apache.struts.action.ActionServlet.initModuleConfig(ActionServlet.java:689)
at org.apache.struts.action.ActionServlet.init(ActionServlet.java:356)
at javax.servlet.GenericServlet.init(GenericServlet.java:256)
....

我想我遗漏了什么或以错误的方式使用了 set-property 标签.有人可以帮忙吗?

I think I am missing something or using set-property tag in wrong way . Can anyone help?

推荐答案

Struts 1.3 DTD

set-property"元素在自定义子类时特别有用与 , , , 或元素.

The "set-property" element is especially useful when a custom subclass is used with , , , or elements.

使用您想要包含的属性创建 ActionMapping 的子类

Create Subclass of ActionMapping with properties you would like to inclide

public class CustomActionMapping extends ActionMapping {

    private String task;

    public String getTask() {
        return task;
    }

    public void setTask(String task) {
        this.task = task;
    }
}

struts-config.xml

<action-mappings type="CustomActionMapping">
   <action path="/myAction" name="myForm" type="demo.MyAction" scope="request">
      <set-property value="view" property="task" />
      <forward name="success" path="/result.jsp"></forward>
   </action>
</action-mappings>

doGet/doPost 方法中获取任务的值你的 Action

get the value of task in doGet/doPost method your Action class

CustomActionMapping cam = (CustomActionMapping) mapping;
String task = cam.getTask();

希望对你有帮助.

这篇关于在 Struts1 中,如何在 action 标签中使用 set-property 标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆