简体   繁体   中英

Pass object from JSP to a action class in Struts 1.3

I want to pass an object from JSP to an action class. 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. Here's EmpInfo is value object and it has all the getter and setters in it.

The control flow of Struts is like this:

  1. A HttpRequest apears
  2. ActionServlet receives it, populates proper ActionForm, and pass the control to an Action
  3. Action performs the logic, and forwards or redirects to an 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.

So the only way to pass an object is to pass its properties as request parameters (GET or POST preferred).

By the way using Struts 1.2+ you are not forced to have just String fields in ActionForm. Have your object in action form referenced it with Java Beans convention:

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.

Struts 1.2+ uses Apache Commons BeanUtils internally, so you can use its Converters for complex types.

Add your object to the session or request object by setAttribute() method. You can get it from an action class by 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

 <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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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