How to return plain text for an AJAX request?

I base my knowledge on how to do this in this Crunchify tutorial .

I have a single page application.

It has two functions. It must either send a request to the HTTP servlet, which will call its own java, and from which it will receive a JSON string containing any errors / recommendations of the servlet, what to do next.

Another function is that it requests a dialog for saving a file from a servlet.

The question is how can I structure my servlet so that it returns a plain text HTTP response for the AJAX request in question.

I have a very cool way to do this, and I would like to suggest how to achieve the same in a 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 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. ModelAndView ViewName = text, ModelName = model.

/jsp/text.jsp( ), . AJAX, , .

?

+4
3

, .

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.

ModelAndView

, .

submitQuery() @ResponseBody:

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

@ResponseBody , 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();
}
+11

, @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");
}
+4

:

  • MyServlet, @Controller, . MVC.
  • , , - DispatcherServlet. Spring MVC
  • DispatcherServlet, MyServlet ,

, . , , Spring MVC.

Ajax Spring MVC: http://www.mkyong.com/spring-mvc/spring-mvc-jquery-autocomplete-example/

+2

All Articles