3

UPDATE

As the question itself has changed a little (this turned out to be more about the list/dropdown list, I started a new question concerning the problem with the data passing: new question


I am trying to write an application where I first have a list of objects, then navigate to a page where only one of the objects is displayed. Each object has a list of several other objects (in my program its a list of exams and each exam has a list of questions). I then press a button to be directed to a third page where I can fill a form to create a new attached object.

When saving the attached Object, I want to be redirected to the second page displaying the same object as before. And this is my problem. The form fields on the page to which I return are empty.

Now, when calling the SaveAttachedObject Action (AttachedObjectAction, method "save") I get redirected to the object.jsp. However, no object is selected and I receive my error message that I usually invoke when I click on the editbutton in objectlist.jsp and have not selected an object. This message is produced in the validate method of the ObjectAction which usually checks if either object or attachedObject is null.

I suspect, the input for the object.jsp is missing.

in my struts.xml I have the SaveAttachedObject action which has the attachedObject.jsp as input, so the attachedObject (including its variable object) should exist. In that I call the ShowObject Action which shows the object.

<action name="SaveAttachedObject" class="de.example.project.action.AttachedObjectAction"
        method="save">
        <result name="success" type="redirect">ShowObject.action</result>
        <result type="tiles" name="input">attachedObjectForm</result>
</action> 

<action name="ShowObject" class="de.example.project.action.ObjectAction"
        method="load">
        <result type="tiles">objectForm</result>
</action>    

(all tiles are defined in the tiles.xml)

If I add an

<result name= "input" type="tiles">attachedObjectForm</result>     

to the ShowObject action. i always receive the error that the "dropdown-list" could not be resolved as a collection/array/map/enumeration/iterator type:

HTTP Status 500 - ServletException including path '/layout/mainLayout.jsp'.

Stacktrace:

tag 'select', field 'list', name 'attachedObject.type': The requested list key 'objectTypes' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]

Here is the attachedObject.jsp with the select field that causes the error:

I dont know whether I can just give the object(id) to the ShowObject action instead of the whole attachedObjectForm?
Or, which I dont unterstand: why does it want to read the whole objectTypes list? I just want to use the attachedObject (only using the selected item of the list as the new parameter of attachedObject).

If you need any futher code or information, please let me know.

Here are the concerning code parts of the main classes of my project:

ObjectAction: http://pastebin.com/q080N1NB

attachedObjectForm: http://pastebin.com/m2HMXHwP

Community
  • 1
  • 1
  • +1 great question for a first-timer! – avalancha Nov 11 '14 at 18:21
  • Looks like you're missing (a) some form of `prepare` implementation where the necessary form data is prepared, and (b) the loading of the object in question. – Dave Newton Nov 11 '14 at 19:50
  • And where would I need to put the prepare implementation? in the ObjectAction? I understand that I there could initiate the List of objectTypes that is causing the error, but I still dont know, where I would get the attachedObject I was working on before (it has the object ID it belongs to and which I want to display on the page I am directing back to) – SomethingChocolate Nov 11 '14 at 21:22

1 Answers1

1

I'm not completely following you; +1 for a very nice first question, though probably excessively verbose. You can add all the details you want, but you should always contract the real question (the expected result, the obtained result, the problem) in a "one-liner", or short sentence(s), separated by all the other bunch of details potentially useful for a more deep debugging of the problem.

From what I've got, at least one of your problems is that you are populating the list in your load() method (not reached in case of INPUT result returned by an Interceptor). Change that by using the prepare() method like follows:

public void prepare() throws Exception{
    objectTypes = new ArrayList<String>();
    ObjectType[] allTypes = ObjectType.values();
    for (ObjectType objectType : allTypes) {
            objectTypes.add(objectType.toString());
    }
}

public String load() {
    Long id = 0l;
    if (objectId != null) {
        id = objectId;
    } else if (attachedObject.getObject() != null) {
        id = attachtObject.getObject().getId();
    }
    if (id == (long) 0) {
        return ERROR;
    } else {
        exam = objectService.loadObject(id);
        return SUCCESS;
    }
}

I'm not sure which your workflow exactly is, but this is the way.

If you need also exam populated in case of INPUT result, then put all the stuff of your load() method in the prepare() method, and ensure you are using the paramsPrepareParamsStack instead of the defaultStack.

You might discover that your ObjectAction is not even needed anymore, and put all its business in a prepare() method of another action.

Note that in the posted code (most likely cleaned up before publishing it), you are missing the getter for objectTypes.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Thank you very much for your answer and thank you for your advice concerning my question style. I'll try to include it for my next question! Concering your suggestions: The prepare method has already helped me. Now i don't have the error concerning the list anymore. However, I still "miss" my exam. That's why I wanted to follow your suggestion using the paramsPrepareParamsStack. Unfortunatelly, I have only worked with the defaultStack up to now and have no idea, where I need to define the usage. My search of the internet has not been successful either. Could you help me a litte more with this? – SomethingChocolate Nov 12 '14 at 12:58
  • Sorry, I was just editing my first comment, when you replied. It solved the first part of my problem, so thank you for that! I only have problems using the paramsPrepareParamsStack – SomethingChocolate Nov 12 '14 at 13:16
  • Ah, missed that :) In the question I linked [the paramsPrepareParamsStack](http://stackoverflow.com/a/22907340/1654265) answer... that stack is taken from the documentation. Copy and paste all that stuff in your struts.xml (inside ``HERE`` then immediately after add ``. First you declare "your" new stack, then you instruct struts to apply it to every action. This is the easiest way, otherwise you can apply it to some actions only, but it's not needed here, just go with that – Andrea Ligios Nov 12 '14 at 13:33
  • I'm afraid, this hasn't changed anything. I added the code to my struts.xml and I still have the same result as before (after adding the prepare method: I stay on page three (attachedObjectForm) but receive my error message "Please chose an object". :( – SomethingChocolate Nov 12 '14 at 13:56
  • Please, could you edit your question (placing an `#UPDATE` at the top and pushing the old question down with a `---`), and describing in very short what's happening now (after the prepare() adjustment), which is the desired result, and which the obtained one ? I've no time to re-read all your question, a short paragraph with the essential, relevant informations (and those only) would be greatly appreciated :) – Andrea Ligios Nov 12 '14 at 14:07
  • I updated the question linking to the "current" problem which I put in a separat question. Thank you very much for your help so far!! – SomethingChocolate Nov 12 '14 at 16:12