spring - How do I return plain text for an AJAX query? -


i'm basing knowledge on how on crunchify tutorial.

i have single page application.

it has 2 functions. needs either send request http servlet, go call own java, , recieve json string containing errors/advising servlet next.

the other function prompts save file dialog servlet.

the question - how can structure servlet such returns plain text http response ajax query examine.

i have round way of doing this, , i'd suggestion how achieve same thing in simpler manner.

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 http response*/             }                 }        xmlhttp=new xmlhttprequest();      xmlhttp.open("get", "submitquery", true);      xml.send(); } </script> </head> <body>     <button onclick="myajax()">click me</button> </body> </html> 

my understanding way works when /submitquery uri sent, it's mapped myservlet servlet. servlet returns modelandview of viewname = text, modelname = model.

the dispatcher redirects /jsp/text.jsp (the specified view), displays model on it. final rendered output returned ajax object can access how wants.

is there more straight forward way of doing this?

yes there more straight way doing this.

as per crunchify tutorial returning modelandview object. that's reason getting model object on text.jsp

modelandview: returning both model , view information controller. holder both model , view in web mvc framework. note these entirely distinct. class merely holds both make possible controller return both model , view in single return value. 

more modelandview

now come other way in need return plain text.

annotate submitquery() method in controller @responsebody annotation:

@requestmapping(value="/submitquery") @responsebody public string submitquery() {     return "response"; } 

the @responsebody can put on method , indicates return type should written straight http response body (and not placed in model, or interpreted view name)

access parameter in 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(); } 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -