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)
}
}
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, , .
?