简体   繁体   English

将对象从JSP传递到Struts 1.3中的操作类

[英]Pass object from JSP to a action class in Struts 1.3

I want to pass an object from JSP to an action class. 我想将对象从JSP传递到动作类。 How to do it? 怎么做? I have no idea regarding it. 我对此一无所知。 Some of my sample codes are here: 我的一些示例代码在这里:

<tr class="alt">
    <td><html:link href=""><bean:write name="EmpInfo" property="empId" /></html:link></td>
    <td><bean:write name="EmpInfo" property="empName" /></td>
    <td><bean:write name="EmpInfo" property="empAddress" /></td>
    <td><bean:write name="EmpInfo" property="empPhNumber" /></td>
    <td><bean:write name="EmpInfo" property="empEmailId" /></td>
    <td><bean:write name="EmpInfo" property="empLocName" /></td>
    <td><bean:write name="EmpInfo" property="empCountryName" /></td>
    <td><bean:write name="EmpInfo" property="empDob" /></td>
    <td><html:link href=""><bean:message key="view.single.emp.update"/></html:link></td>
</tr>

in the above code in the <html:link href=""> tag i wanna pass the empId or the object EmpInfo to an action class. 在上述代码中的<html:link href="">标签中,我想将empId或对象EmpInfo给操作类。 Here's EmpInfo is value object and it has all the getter and setters in it. 这里的EmpInfo是值对象,并且其中包含所有的getter和setter方法。

The control flow of Struts is like this: Struts的控制流程如下:

  1. A HttpRequest apears 一个HttpRequest豌豆
  2. ActionServlet receives it, populates proper ActionForm, and pass the control to an Action ActionServlet接收它,填充适当的ActionForm,然后将控件传递给Action
  3. Action performs the logic, and forwards or redirects to an ActionForward Action执行逻辑,并转发或重定向到ActionForward

So if you forward to a JSP, then you won't pass the control to another action again directly, but the next action is invoked after a new request from user browser is sent. 因此,如果转发到JSP,则不会再次将控件直接传递给另一个动作,但是在发送来自用户浏览器的新请求之后,将调用下一个动作。

So the only way to pass an object is to pass its properties as request parameters (GET or POST preferred). 因此,传递对象的唯一方法是将其属性作为请求参数传递(首选GET或POST)。

By the way using Struts 1.2+ you are not forced to have just String fields in ActionForm. 通过使用Struts 1.2+,您不必在ActionForm中仅具有String字段。 Have your object in action form referenced it with Java Beans convention: 使用Java Beans约定让操作中的对象引用它:

class MyActionForm extends ActionForm {
    private MyBean myBean;

    public MyBean getMyBean() {
        if (myBean == null) {
            myBean = new MyBean();
        }
        return myBean;
    }
}

Now you can access its properties with myBean.property1 in your JSP tags, and a request parameter with name myBean.property1 will be populated to the property. 现在,您可以在JSP标记中使用myBean.property1访问其属性,并且名称为myBean.property1的请求参数将被填充到该属性中。

Struts 1.2+ uses Apache Commons BeanUtils internally, so you can use its Converters for complex types. Struts 1.2+在内部使用Apache Commons BeanUtils,因此您可以将其Converters用于复杂类型。

Add your object to the session or request object by setAttribute() method. 通过setAttribute()方法将您的对象添加到会话或请求对象中。 You can get it from an action class by getAttribute() . 您可以通过getAttribute()从动作类中获取它。

使用html:param标记,可以在html:link标记的正文中使用

<html:link href=""><html:param name="empId"><bean:write name="EmpInfo" property="empId" /></html:param></html:link>

this is the correct working code. 这是正确的工作代码。 thanx to @roman c for helping me out 感谢@roman c帮助我

 <td>
                <html:link href="viewDetailInfo.do">
                    <html:param name="empId">
                        <bean:write name="empList" property="empId" />
                    </html:param>
                        <bean:write name="empList" property="empId" />
                </html:link>
 </td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM