Let an server-side Beanshell script interact with the user in a JavaServer Faces environment

StackOverflow https://stackoverflow.com//questions/9581279

  •  08-12-2019
  •  | 
  •  

문제

first I have to admit this might be a pretty specific problem. In a JavaServer Faces Application I want to be able to let a script run on the server-side interact with the user.

To give you more of a detail I give you an example of what I want to do: In my application a user can input data and on the server side there is a script which can check/modify this data. I don't know the exact script because the user is able to make a custom one. All in all so far there is no problem, but in the script it is possible to do interactions with the user in the way of a question dialog. In the following example, the further script execution depends on the answer of the user.

...

if( inputData.equals("Something") ){
    if( askUser("Question?","Some question...") )
    {
        doSomething();
    }
    else
    {
        doSomethingElse();
    }
}

...

As you can see the method "askUser" has to wait for the result before the script can continue. The script itself is a Beanshell-script and the method "askUser" is a Java method which is called by the Beanshell interpreter.

I had in my mind to use threads and Java synchronization methods to let the script pause, but thread creation is not allowed by EJB. Still I tried that way, but it failed due to the FacesContext not being available in user-created threads.

I hope I could make my intention clear and you have any idea how this could be done... Thanks in advance. :)

도움이 되었습니까?

해결책

Your question demonstrates a complete misunderstanding of how not only JSF technology works, but also the inherently stateless nature of HTTP communication and web servers.

Typically in a desktop based application this simple algorithm you listed above can be easily implemented, probably with modal dialogs and events. Web applications do not work this way because they are inherently stateless. A user or browser agent makes an HTTP request to a web server, the server processes this request and formulates a response, then returns that HTTP response back to the browser agent to be rendered. Typically this data can be textual, HTML, XML, Javascript, CSS, images or even a file.

The problem with incorporating the algorithm above on a server is that the functionality for askUser cannot possibly be done by the server, because the server cannot just ask the user a question and wait for the response. That would be backwards. The user is the client and asks questions of the server.

So how is this common problem solved in a web application? In JSF, the server can send as a response a Javascript to the browser that will allow the browser to ask the question, then when the user makes the choice, the Javascript can then post back the selection of the user to the server in an HTTP request. If that selection involves any changes to the DOM or page elements at all then that information will be sent in the response.

JSF bridges the gap between desktop and web applications by allowing giving the illusion of desktop and event driven functionality to an otherwise stateless technology, and it does this by having an event lifecycle on the serverside and using Ajax to tightly control changes to document elements.

A good tutorial on JSF technology can be found here, however I recommend becoming more familiar with HTTP and Java Servlets before you delve too deep: http://www.mkyong.com/tutorials/jsf-2-0-tutorials/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top