pavel_kazlou
pavel_kazlou

Reputation: 2046

struts2: param tag doesn't use conversion

Problem

In my struts2 application I have JSP page where the list of book is displayed. Each book has an id and the name. Also I want to place a link "edit" alongside any book, which can be edited by current user. This "edit" link should invoke corresponding action, passing book id as parameter to it. So I implemented this page using the code:

<table>
    <s:iterator value="books" status="stat">
        <tr>
            <td><s:property value="id"/></td>
            <td><s:property value="name"/></td>
            <td>
            <s:if test="canEdit[#stat.index]">
                <s:a action="editBook">edit
                    <s:param name="bookId" value="id"/>
                </s:a>
            </s:if>
            </td>
        </tr>
    </s:iterator>
</table>

Book id is not of a basic type, I use custom class for it. So I decided to implement my own converter. And here comes the problem: in the code above converter is used only when evaluating tag <s:property value="id"/> but it is not used to evaluate <s:param name="bookId" value="id"/>. Instead toString() method of book id class is used. Why <s:param> doesn't use my converter? How do i force it to do so? Or maybe there is another way to pass book id as parameter in link?

Some (probably useless) details

To configure converter I placed xwork-conversion.properties inside my /src/main/resources/ folder with the following contents:

my.app.Id = my.app.struts.IdConverter

my.app.Id is an abstract class which is extended by book id class.

The result of rendering of the JSP for a single-entry list is the following:

<table>
    <tr>
        <td>8</td>
        <td>Book 01</td>
        <td><a href="/web/editBook.action?bookId=id%288%29">edit</a></td>
    </tr>
</table>

id%288%29 is an escaped version of string id(8) which is the result of toString() method (defined in base abstract class my.app.Id) for id with int value of 8.

Upvotes: 0

Views: 1093

Answers (2)

pavel_kazlou
pavel_kazlou

Reputation: 2046

Found corresponding issue with explanation of problem: http://jira.opensymphony.com/browse/WW-808

In a few words the difference is between specifying <param value="myValue"/> and specifying <param><property value="myValue"/></param>. Conversion is used only in second case.

Upvotes: 0

Dave Newton
Dave Newton

Reputation: 160181

The <s:param> tag is meant for "simple" parameters. In this case, you may be able to just set a temporary value using <s:property> if that does the conversion you expect.

It uses the toString of the book id because that's what a getId() will print out when rendered, just as if you did a System.out.println(book.getId()).

Upvotes: 1

Related Questions