1

I have been searching for an answer to my problem for two days now, so I figure its time to ask. Hopefully someone wiser in the ways of JSF and Seam (it won't take much, I'm still new to both) will be able to enlighten me.

I have a Seam application that requires a dropdown, but one that the user can type into as well. This will allow user-created entries as well as pre-defined ones. I am attempting to create this combobox using the FlexBox jQuery plugin. It seems like a good candidate for what I need. I just need to specify a div and a page that returns JSON data in order to create this combobox effect.

add-codes.html

...
<div>
Department: <div id="dept-dropdown" />
</div>
...

AccountComboBoxList.java

...
@Scope(SESSION)
@Name("actComboBoxAction")
public class AccountComboBoxList implements Serializable {
...
@WebRemote
public JSONObject getDepartmentJSON() {
    JSONObject returnJSON = new JSONObject();
    try {
        //if dept name is null, skip this and return empty obj
        if(deptName!=null) {
            JSONArray returnArray =
                JsonDataHelper.convertAcctToolEnt2JsonArray(deptName);
            returnJSON = new JSONObject();
            returnJSON.put("results",returnArray);
            logger.info("JSON results: "+returnJSON);
        }
    } catch (JSONException je) {
        logger.warn("JSONException thrown!");
        logger.warn(je.getMessage());
        je.printStackTrace();
    } finally {
        return returnJSON;
    }
}
...

deptString.xhtml - in its entirety

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:s="http://jboss.com/products/seam/taglib">

<h:outputText value="#{actComboBoxAction.getDepartmentJSON()}" />

</ui:composition>

Then in my javascript file I simply create the combobox with this line:

...
$("#test-string").flexbox('./includes/deptString.seam');
...

The problem comes in at this point. deptString.seam calls the seam method correctly and successfully returns a JSONObject. But the combobox doesn't work correctly because a '' is attached on the end of the string. For example, I copy-and-pasted this from the resulting 'View Source' of the page:

{"results":[{"id":"1","name":"Dept1"},{"id":"2","name":"Dept2"},{"id":"3","name":"Dept3"}]}</html>

What is adding this extraneous tag and is there any way to get rid of it? I created a text file with just the proper JSON in it and called that file into the .flexbox() method. The combobox worked as advertisted then, so I know the flexbox code works. I also tried removing all the JSF code from the deptString.xhtml file and had just the single line #{actComboBoxAction.getDepartmentJSON()} in the file. This caused a 'com.sun.facelets.FaceletException: Error Parsing /includes/deptString.xhtml: Error Traced[line: 1] Content is not allowed in prolog.' error.

Like I said, i'm new to JSF and Seam. If there is an obvious/better way to get a simple String from the backend to the frontend in a Seam/JSF application, I'm all ears.

Thanks in advance for your help.

CatsAndCode
  • 377
  • 9
  • 27
  • The Error Parsing /includes/deptString.xhtml: Error Traced[line: 1] Content is not allowed in prolog.' error. is happening because you still need the ui:composition tag around the expression. Although it probably wont fix your problem – reevesy Aug 11 '11 at 20:19
  • Not sure about the Seam part, but I can tell that you're basically abusing JSF as being a web MVC framework as a webservice. Consider using a real webservice API, such as JAX-WS or JAX-RS. – BalusC Aug 11 '11 at 20:24
  • 1
    If your javascript was in a facelet (xhtml) file the you should be able to do $("#test-string").flexbox('#{actComboBoxAction.getDepartmentJSON()}'); this link http://stackoverflow.com/questions/2547814/mixing-jsf-el-in-a-javascript-file mentions someways of achieving this – reevesy Aug 11 '11 at 20:28
  • Hey guys, Thanks for the answers! I have something that is working now, using your suggestion reevesy! I appreciate the input BalusC (and also for the answer on the question that reevesy linked to). I can see your point about the JSF framework abuse. I guess I got wrapped up and confused in the Seam Remoting that lets you call Seam methods from javascript (hence the @WebMethod annotation). I'll take a look at the frameworks you mentioned and and see if I can implement one of them easily. – CatsAndCode Aug 11 '11 at 21:24

1 Answers1

1

If your javascript was in a facelet (xhtml) file the you should be able to do

$("#test-string").flexbox('#{actComboBoxAction.getDepartmentJSON()}'); 

Mixing JSF EL in a JavaScript file mentions someways of achieving this

Community
  • 1
  • 1
reevesy
  • 3,452
  • 1
  • 26
  • 23
  • This doesn't actually answer the question. Why is the

    being included? Guess I'm being harsh, but whatever. It solves that particular person's problem, but why is that html tag being stuff in there?

    – Kevin Galligan Jan 07 '12 at 17:22