2015-03-31 11 views
4

我基於我的知識如何做到這一點on this Crunchify tutorial如何爲AJAX查詢返回純文本?

我有一個單頁的應用程序。

它有兩個功能。它需要向HTTP servlet發送一個請求,該servlet將調用它自己的java,並從中接收包含任何錯誤的JSON字符串/建議servlet接下來要做什麼。

另一個功能是它提示servlet的保存文件對話框。

問題是 - 我如何構造我的servlet,使其返回一個純文本HTTP響應,以供AJAX查詢檢查。

我對此做了一個非常全面的討論,並且希望能夠以更簡單的方式達到同樣目的。

的web.xml

<servlet> 
     <servlet-name>MyServlet</servlet-name> 
     <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>MyServlet</servlet-name> 
     <url-pattern>/submitQuery</url-pattern>  
      <url-pattern>/saveFile 
    </servlet-mapping> 

MyServlet-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 

    <context:component-scan base-package="world.hello.myservlets" /> 

    <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="viewClass" 
      value="org.springframework.web.servlet.view.JstlView" /> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 
</beans> 

MyServlet.java

package world.hello.myservlets; 

@Controller 
public class MyServlet{ 


    @RequestMapping("/submitQuery") 
    public ModelAndView submitQuery() 
    {  

     return new ModelAndView("text", "model", "hello world"); 
    } 

} 

/WEB-INF/jsp/text.jsp

{model} 

的index.html

<html> 
<head> 
<script> 
function myAjax() 
{ 
     xmlhttp.onreadystatechange=function() 
      { 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
      { 
       alert(xmlhttp.responseText) 
       /*do something with the http response*/ 
      }  

      } 


    xmlhttp=new XMLHttpRequest(); 
    xmlhttp.open("GET", "submitQuery", true); 
    xml.send(); 
} 
</script> 
</head> 
<body> 
    <button onclick="myAjax()">Click me</button> 
</body> 
</html> 

我的理解是這樣的工作原理是當發送/submitQuery URI,它映射到的方式MyServlet servlet。然後servlet返回ViewName = text, ModelName = modelModelAndView

然後調度程序重定向到/jsp/text.jsp(指定的視圖),在其上顯示模型。最終呈現的輸出返回到AJAX對象,然後可以訪問它的方式。

有沒有更直接的方法來做到這一點?

回答

2

請看看下面的一些意見:

  • 其中標註有@ControllerMyServlet類不是一個servlet。它負責MVC設計模式的控制器方面。
  • 這裏唯一使用的servlet是DispatcherServlet。這是所有基於Spring MVC appliations面向前的servlet
  • 我建議重命名DispatcherServlet名稱,MyServlet名字給人一種感覺,這個servlet是手寫

也沒有,這不是一個關於方式輪的實施。您遵循的步驟是使用Spring MVC的標準方式。

本博客文章給出了使用Spring MVC的一個樣本Ajax實現: http://www.mkyong.com/spring-mvc/spring-mvc-jquery-autocomplete-example/

+0

有關servlet名稱的確定好處,而應該稱爲「MyServletController」。 – dwjohnston 2015-04-08 03:46:27

4

有這樣做的稍微更直接的方式,它涉及@ResponseBody。你可以通過這樣做跳過jsp渲染:

@RequestMapping(value = "/somestring", method = RequestMethod.GET) 
public @ResponseBody 
String getSomeString() { 
    return "some string"; 
} 

如果你想使用除字符串之外的東西,你可以。該對象將被序列化爲JSON。例如:

@RequestMapping(value = "/myobject", method = RequestMethod.GET) 
public @ResponseBody 
MyObject getSomeString() { 
    return new MyObject("blah"); 
} 
11

是的,有更直接的方法來做到這一點。

根據crunchify教程您將返回ModelAndView對象。所以這是對text.jsp

ModelAndView: It returning both model and view information from a controller. Holder for both Model and View in the web MVC framework. Note that these are entirely distinct. This class merely holds both to make it possible for a controller to return both model and view in a single return value. 

More about ModelAndView

你所得到的模型對象的原因現在來看看在你需要返回純文本的其他方式。

@ResponseBody註釋標註您submitQuery()方法控制器:

@RequestMapping(value="/submitQuery") 
@ResponseBody 
public String submitQuery() { 
    return "Response"; 
} 

@ResponseBody可以在方法put和指示返回 類型應該直接寫入HTTP響應主體(而不是 放置在模型中,或解釋爲視圖名稱)

在javascript中訪問參數。

function myAjax() 
{ 
    xmlhttp=new XMLHttpRequest(); 
    xmlhttp.onreadystatechange=function() 
    { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) 
     { 
      alert(xmlhttp.responseText); 
      console.log(xmlhttp.responseText); 
     } 
    } 
    xmlhttp.open("GET", "submitQuery", true); 
    xmlhttp.send(); 
} 
+0

我不認爲'window.location.href =「你的網址」是必要的。他實際上並不想重定向,他只是想獲得響應文本。 – wholevinski 2015-04-09 10:58:46

+0

我多次閱讀。重定向這個詞是他對JSP呈現工作方式產生誤解的副產品:「調度器然後重定向到/jsp/text.jsp(指定的視圖),在其上顯示模型,最終呈現的輸出返回給AJAX對象然後可以訪問它想要的。「 – wholevinski 2015-04-09 11:23:57

+0

應該絕對沒有重定向邏輯;他的問題明確指出:「問題是 - 我如何構建我的servlet,使其返回純文本HTTP響應以供AJAX查詢檢查。」他甚至提到可能需要使用JSON:「並從中接收包含任何錯誤的JSON字符串/建議servlet接下來要做什麼。」 – wholevinski 2015-04-09 11:27:01