1

jQueryではなく ManagedBean でコマンドを処理する目的で、JSF アプリケーションにjQuery Terminal Emulatorを統合しました。コマンドを Bean に正常に送信し、結果をコールバック パラメータに設定しましたが、この結果を端末に表示する方法がわかりません。

関連するコード スニペットは次のとおりです。

terminal.xhtml

<h:form>
        <p:remoteCommand name="sendRemoteCommand"
            action="#{processCommand.rcAction}"
            oncomplete="handleComplete(xhr, status, args)" />
    </h:form>
    <h:panelGroup id="term_demo" layout="block" />
    <h:outputScript library="primefaces" name="jquery/jquery.js"
        target="body" />
    <h:outputScript library="js" name="jquery.terminal-min.js" />
    <h:outputScript target="body">
        $(document).ready(function() {
            $('#term_demo').terminal(function(command, term) {
                if (command !== '') {
                    try {
                        var result = sendRemoteCommand([ {
                            name : 'command',
                            value : command
                        } ]);
                        if (result !== undefined) {
                            term.echo(new String(result));
                        }
                    } catch (e) {
                        term.error(new String(e));
                    }
                } else {
                    term.echo('');
                }
            }, {
                greetings : 'Javascript Interpreter',
                name : 'js_demo',
                height : 200,
                prompt : 'js> '
            });
        });

        function handleComplete(xhr, status, args) {
            // Need to find a way to return args.result
            // to the terminal!
        }
    </h:outputScript>

ProcessCommand.java

@ManagedBean
@RequestScoped
public class ProcessCommand {
private String command;

public void rcAction() {
    FacesContext context = FacesContext.getCurrentInstance();
    Map map = context.getExternalContext().getRequestParameterMap();
    command = (String) map.get("command");

    RequestContext requestContext = RequestContext.getCurrentInstance();
    requestContext.addCallbackParam("result", command + "_ok");
}

JSの関数handleComplete()で、変数args.resultが「[command]_ok」にちゃんと設定されていてsendRemoteCommand、Primefacesの<p:remoteCommand>タグで指定されたjQueryの端末関数が呼び出されたときに、後者

よろしくお願いします!

編集:JSF 2.0(Mojarra 2.2、Glassfish 4、Primefaces 4.0)を使用しています

4

1 に答える 1

1

最終的に戦略を変更し、関数の最初の引数としてサーブレット URI を渡しterminalました。GSON のおかげで、端末から送信された JSON 要求を処理し、JSON 応答を送信できます。

これはずっときれいです:)

Terminal.xhtml で

jQuery(function($) {
            $('#terminalPanel').terminal("terminalSerlvet", {
            login: false,
            greetings: "Javascript Interpreter",
            tabcompletion: true,
            completion: function(term, string, callback) {
                callback(cmd);
            }});
        });

サーブレット:

@WebServlet(urlPatterns = "/terminalSerlvet")
public class JSONTerminalServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
        IOException {
    System.out.println("Entering JSONTerminalSerlvet#doPost...");

    Gson gson = new Gson();
    StringBuilder sb = new StringBuilder();
    BufferedReader reader = request.getReader();
    TerminalCommandData cmdData;

    try {
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        cmdData = (TerminalCommandData) gson.fromJson(sb.toString(), TerminalCommandData.class);

    } finally {
        reader.close();
    }

    if (cmdData != null) {

        // Write response data as JSON.
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(new Gson().toJson(buildJsonResponse(cmdData)));
    }
}
于 2013-11-01T13:14:02.760 に答える