它有两个功能.它需要向HTTP servlet发送请求,该servlet将调用自己的java,并从中接收包含任何错误的JSON字符串/建议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 = model的ModelAndView.
然后,调度程序重定向到/jsp/text.jsp(指定视图),在其上显示模型.最终呈现的输出将返回给AJAX对象,然后可以按原样访问它.
有没有更直接的方式这样做?
解决方法
根据crunchify教程,您将返回ModelAndView对象.这就是你在text.jsp上获得模型对象的原因
ModelAndView: It returning both model and view @R_424_4045@ion 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.
现在转到需要返回纯文本的另一种方式.
使用@ResponseBody注释在控制器中注释您的submitQuery()方法:
@RequestMapping(value="/submitQuery") @ResponseBody public String submitQuery() { return "Response"; }
The
@ResponseBody
can be put on a method and indicates that the return
type should be written straight to the HTTP response body (and not
placed in a Model,or interpreted as a view name)
在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",true); xmlhttp.send(); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。