你怎么输出一个视图文件作为Java中的Ajax响应?你怎么、视图、文件、Ajax

2023-09-10 21:02:36 作者:平凡的向日葵

我是一个PHP开发人员,但我转换为Java。 (很新的Java在这一点)

I am a PHP developer but am transitioning to Java. (very new to Java at this point)

有没有一种方法,使一个AJAX调用一个Servlet,并配有单独的.jsp文件的输出响应(而不是HTML或直接在Servlet JSON创建的)?

Is there a way to make an ajax call to a Servlet, and respond with the output of a separate .jsp file (as opposed to html or json created directly in the Servlet)?

下面是对什么是常见的做法瓦特/ Zend框架,这就是我想要做的与Java一个例子,如果可能的:

Here is an example of what is common practice w/ Zend Framework, which is what I would like to do with Java if possible:

public function myAjaxCallAction(){
    $this->view->someVar = 'whatever';
    $this->view->hello = 'world';
    $output = $this->view->render('someViewScript.phtml'); // the above vars are in this view
    echo $output;
}

同样很新的Java中,任何有关此类情况的建议是多AP preciated!

Again very new to java, any advice pertaining to this type of situation would be much appreciated!

推荐答案

只是尝试加载所需的的.jsp。通常情况下,你将使用JSP片段(.jspf)。如果要加载的内容,你可以这样做:

Just try to load the .jsp that you want. Normally you will use a JSP fragment (.jspf). If you want to load its contents, you can do something like:

您的网页:

... content ...
<div id="container"></div>
... content ...

在页面上方的Javascript(使用jQuery):

Javascript of the page above (using jQuery):

$(function(){
    $( "#container" ).load( "pathToYoutJsp/file.jsp", { someVar: "whatever", hello: "world" } );
});

这将被载入看起来类似的JSP:

The JSP that will be loaded will look something like:

... content ...
${param.someVar} foo foo foo ${param.hello}
... content ...
 
精彩推荐