【问题标题】:Sending command output from JSF managedBean to jQuery Terminal将 JSF managedBean 的命令输出发送到 jQuery 终端
【发布时间】:2013-10-31 10:25:25
【问题描述】:

我已将jQuery Terminal Emulator 集成到 JSF 应用程序中,目的是在 ManagedBean 中而不是在 jQuery 中处理命令。 我已成功向 bean 发送命令,然后将结果设置为回调参数,但我不知道如何在终端中显示此结果。

这里是相关代码sn-ps:

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”,我想返回它,以便当jQuery终端函数调用Primefaces中指定的sendRemoteCommand&lt;p:remoteCommand&gt;标签,可以得到后者返回的结果

提前非常感谢!

编辑:我正在使用 JSF 2.0(Mojarra 2.2、Glassfish 4、Primefaces 4.0)

【问题讨论】:

    标签: jquery jsf jsf-2 primefaces jquery-terminal


    【解决方案1】:

    我终于改变了策略,并传递了一个 servlet 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);
                }});
            });
    

    Servlet:

    @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)));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多