سؤال

I have a working Action and JSP form that I'm using for creating new entities in my very basic Struts 2.2.1.1 app. I'm trying to modify the app to re-use the same JSP form for the editing of entities.

I added a "hidden" ID tag and now I'm getting errors when I submit the form. Can someone please give me some assistance?

I've Googled this issue and seen others posting similar errors but I'm not sure how to resolve it.

Excerpt from Stack Trace when I submit the form:

2011-05-02 11:09:36,132 3198497 ["http-bio-8080"-exec-23] WARN
com.opensymphony.xwork2.ognl.OgnlValueStack - Error setting expression
'id' with value '[Ljava.lang.String;@100ac03'
ognl.MethodFailedException: Method "setId" failed for object
org.robbins.flashcards.model.Tag@1b9eb34 [name='null' ]
[java.lang.NoSuchMethodException:
org.robbins.flashcards.model.Tag.setId([Ljava.lang.String;)]
    at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1285)
    at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1474)

Excerpt from JSP:

<%@ taglib prefix="s" uri="/struts-tags"%>
...
<s:form action="saveOrUpdateTag" method="post">
    <s:hidden name="id" />
    <s:textfield name="name" key="label.tag.name" size="20" />
    <s:submit label="label.flashcard.submit" align="center" />
</s:form>

Excerpt from Action Class:

public class TagAction extends FlashCardsAppBaseAction implements
ModelDriven<Tag> {

    Tag tag = new Tag();

    public Tag getTag() {
        return tag;
    }

    public void setTag(Tag tag) {
        this.tag = tag;
    }
    public String createTag() {
        ...
       }
}

Excerpt from POJO:

public class Tag  implements java.io.Serializable {


     private int id;
     private String name;

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }
...
}

Excerpt from Struts.xml

    <action name="saveOrUpdateTag"
            class="org.robbins.flashcards.presentation.TagAction"
            method="createTag">
        <result name="success" type="tiles">displaytag.tiles</result>
        <result name="input" type="tiles">tagform.tiles</result>
    </action>

FYI - I also submitted this question to the Struts-User distribution list but haven't received any input so I'm going to post it here also. I'll update the other post and vice versa when more info is available.

هل كانت مفيدة؟

المحلول 2

It is worth noting the stack trace is a Warning, not an Error.

Also, the Warning is not thrown when the form field is actually populated with a number. Only when it is blank (or presumably if it were populated with an actual string) is the Warning and stack trace thrown. In fact, the Id field on the POJO model for the Action class is successfully populated with the JSP form field value if it is present (and a number of course).

Therefore, we can either ignore the Warning since it is not doing us any harm or default the Id to some numeric value and then add logic in our Action class to deal with it.

I've decided to ignore the warning and I have adjusted my log4j level accordingly:

# Struts OgnlUtil issues unimportant warnings
log4j.logger.com.opensymphony.xwork2.util.OgnlUtil=error
log4j.logger.com.opensymphony.xwork2.ognl.OgnlValueStack=error

More detailed discussion on this topic can be found in the Struts User mailing list here: http://mail-archives.apache.org/mod_mbox/struts-user/201105.mbox/%3CBANLkTinCzcTGjsn1jjotBr7fE_-5CX703w@mail.gmail.com%3E

نصائح أخرى

The NoSuchMethodException ... ([Ljava.lang.String;)] problem can be caused by having multiple attributes with the same name in the HTTP request.

If the case with multiple attributes with the same name is legitimate, you can handle multiple id values by changing setId(int id) to setId(String[] idArray) and parsing each array member string as an integer.

Make sure there is only one setId method (setId(String[] idArray)) in the action class. It seems some versions of struts/ognl (ognl 3.0.4?) can get confused if there are multiple methods with same name but different parameter types.

For example:

public void setId(String[] idArray) {
    for (String idString : idArray) {
       int id = Integer.parseInt(idString);
       ... handle different id values somehow ...
    }
}

My guess is that's an old bug. Try upgrading to the latest Ognl release. Think it's 2.7.something maybe. The most current version is on github. Search there if you want to be very sure but 2.7 should work I'd think.

I think it is the problem of hidden something like

this , if you having two pages and if two pages contains same hidden name like this,showing in below you will get an that ognl error,remove 1 something like if you kept hidden name in first page and that hidden name you are using in 2nd page remove in 2nd page(if same hidden name is present) and run you program will now not showing any error. Why it's like this means, probably we already hidden in the previous page and it's possible to use in 2nd page without hidden, with request.getParameter("your name"); (Here hidden variable over-write). and one more instead of giving give input hidden like input type hidden and give name and value it's best i think.

I also had the same error bro.but i somehow find it 1.we needn't define getter setter for the input field that are accepting from the jsp page,because it is automatically mapped based on name given. 2.Make sure the variable fields you are passing shouldn't reduntent. 3.In mycase i perform a ajax call with passing a field(eg:userId) and on submitting the form (parameters passing also has userId).For this reason the error was thrown.Than i altered the name of ajax call parameter as (eg:userIdTemp),it works well.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top