0

I am just trying to build a custom component with :

  • firstly, a basic onclick javascript event on my p tag rendered like alert("...")
  • secondly, a server sider handling of the click event

.

// HelloComponent.java
@FacesComponent(createTag = true, tagName = "helloComponent", namespace = "http://example.com/tags")
public class HelloComponent extends UIComponentBase {

    @Override
    public String getFamily() {
        return "Greeting";
    }

    @Override
    public void encodeBegin(FacesContext context) throws IOException {
        String message = (String) getAttributes().get("message");

        ResponseWriter writer = context.getResponseWriter();
        writer.startElement("p", this);
        writer.write(message);
        writer.endElement("p");
    }
}


// index.xhtml (simplified)
<html ...>
    <h:body>
        <t:helloComponent message="Hello World !" />
    </h:body>
</html>

I've been reading things about events/behaviors but I can not figure it out how it works.

robinvrd
  • 1,760
  • 12
  • 28
  • Just use `writer.writeAttribute(...)`? Or are you actually trying to ask how to listen for it in server side? (because that's where events/behaviors actually come into the play). – BalusC Apr 02 '20 at 14:01
  • @BalusC yes that's it, I want server side handling. – robinvrd Apr 02 '20 at 14:31
  • Then I don't understand the phrase "a basic onclick event" and the `alert(...)` example at all. This contradicts with the apparent wish to invoke a JSF bean action method. – BalusC Apr 02 '20 at 15:05
  • @BalusC the first step of building user interaction was to have client side handling but I was going to ask for server side rendering right after. I just edited my post. – robinvrd Apr 03 '20 at 07:19
  • So you want to let JSF generate a script which sends an ajax request to the server side? This is absolutely not "a basic onclick event like alert()". In the end you seem to merely want your component to support ``. Lookup the `ClientBehaviorHolder` interface if that indeed suits your wish. – BalusC Apr 03 '20 at 10:00
  • @BalusC I just want to set up an onclick event on my `p` that trigger a backing method on my component. – robinvrd Apr 03 '20 at 10:13
  • So in the end you want to be able to use something like `` instead of ``? – BalusC Apr 03 '20 at 10:24
  • Both are good, in fact, I'm creating a JSF component to embed a JS library, so I need to call in my javascript (which will be embed in my component) the ajax listener method of the component. – robinvrd Apr 03 '20 at 10:40
  • Because I want to be able to pass the data of my javascript library to my backing bean at any moment from my javascript calling the ajax listener of the component. – robinvrd Apr 03 '20 at 10:50
  • 1
    So in the end you want to duplicate the functionality of existing `` / `` / `` components? Might be worth the effort to just look in their source code how they did it. Or simply use them. Related: https://stackoverflow.com/q/16588327 and https://stackoverflow.com/q/15550165 – BalusC Apr 04 '20 at 10:50
  • Those comments does not allow to pass data through it, is there any way ? As I'm using a javascript library, the data are in javascript and I need to handle them in my backing bean. It would be like an `onchange` event in javascript that send new data to backing bean. – robinvrd Apr 06 '20 at 07:32
  • Comments? You mean components? Surely they allow parameter passing. See their documentation. – BalusC Apr 06 '20 at 09:12
  • Yes, components, sorry. I am using `` now, but it uses `toString` to pass to java, so from an javascript array of objects `[{x: 2}, {x: 4}]`, I receive `[object Object],[object Object]` in my bean, any help on this ? – robinvrd Apr 06 '20 at 12:32
  • Not sure where you this was shown in h:commandScript documentation, but that's recognizable as parameter syntax for p:remoteCommand. Are you sure you're not mixing up things? – BalusC Apr 06 '20 at 12:35
  • I have `` and I call it in javascript through `commandScript({ arr: [{x: 12}, {x: 4} ] })`, and in my `process` method : `System.out.println(Faces.getRequestParameter("positions"));` prints `[object Object],[object Object]`. – robinvrd Apr 06 '20 at 12:41
  • 1
    HTTP request parameters are strings. In client side, you need to convert objects to strings first. In server side, you need to convert strings to objects. This is not a JSF problem. – BalusC Apr 06 '20 at 12:43
  • Oh alright, as it just ajax behind all this I'm gonna use json format to pass data to my bean method. Thanks for this @BalusC ! – robinvrd Apr 06 '20 at 12:44
  • @BalusC just another quick question, I finally ended by create a component extending OmniFaces `CommandScript`, and as I said before, I pass my data from javascript to my backing bean method through a JSON string, is there any way to create a java data structure with this string before sending data to backing bean ? Like a `beforeAction` that could process the javascript string received and return a Java object. – robinvrd Apr 07 '20 at 14:53
  • As you're using OmniFaces already, just use `@Param` with `converter` attribute to inject and convert a HTTP request parameter using JSF-managed converters. – BalusC Apr 07 '20 at 15:52
  • But where should I put it ? On my backing bean method ? The point of what I was talking about is to handle everything in my component **before** the backing bean receive the data (so that is much cleaner as backing bean will receive clean java objects instead of JSON string) – robinvrd Apr 08 '20 at 07:22

0 Answers0