【SpringMVC】Ajax 刺骨的言语ヽ痛彻心扉 2022-06-05 07:57 145阅读 0赞 SpringMVC的Ajax网上大部分的教程都使用@Respondbody然后返回一个map完成,我觉得这样很繁琐,甚至还要自己配上一个json转换包,不然会出现406警告。其实我觉得大可不必这样,毕竟SpringMVC的@RequestMapping方法中,可以用HttpServletRequest request, HttpServletResponse respons作为参数,直接在网页该打印什么就打印,返回给Ajax就行。 做一个如下的小例子说明这个问题: ![20171121223024989][] 先贴上自己的工程目录结构: ![20171121223120281][] 1、首先是web.xml这里要说明一下,因为我不想用javascript原生的ajax写这么多代码,所以,引入了jquery。那就注定要使用jquery的js文件。所以这里我将SpringMVC的action都改成了\*.html,这点的详细我在《【SpringMVC】指明actions的后缀》([点击打开链接][Link 1])说过了。 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> </web-app> 如果SpringMVC默认是拦截所有链接的,也就是url-pattern配成/,就应该将js文件剔除在外。web.xml需要声明: <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> 要是还有.css你就再仿造上面加多一行就行了。 2、接下来是SpringMVC-servlet.xml,和《【SpringMVC】Helloworld》([点击打开链接][Link 2])中对比是一字没改的! <?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="test.actions" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans> 3、顺带也贴上views/index.jsp的内容吧,非常简单,利用ajax将内容post到ajax.html这个action里面。 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>主页</title> <script src="js/jquery-1.8.3.min.js"></script> </head> <body> 发送<input id="ajax_send" type="text" />给Ajax<button οnclick="ajax()">确定</button><hr> <div id="ajax_receive"></div> </body> <script> function ajax(){ var ajax_send=$("#ajax_send").val(); $.ajax({ type:"POST", url:"ajax.html", data:{ ajax_send : ajax_send }, success:function(data){ $("#ajax_receive").html("Ajax返回的内容:"+data); } }); } </script> </html> 4、在action里面,也就是我WebActions.java的内容,你和平常一样让SpringMVC接受前台传过来的参数一样处理,然后用Javaee原生的那一套打印一个界面出来,可以参考《【Servlet】Servlet3.0与纯javascript通过Ajax交互》( [点击打开链接][Link 3]),index.jsp就会自动处理了。比起SpingMVC那一套又要自己配json处理包,@Respondbody又非要返回一个map,前台又要处理json,我是觉得好多了,尤其是这种只是返回一个简单的字符串的。就算真要个json,也可以自己用for循环拼字符串拼出来啊! package test.actions; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @Controller public class WebActions { @RequestMapping(value = "/index") public String index(ModelMap model) { return "index"; } @RequestMapping(value = "/ajax", method = RequestMethod.POST) public void spring_ajax(@RequestParam("ajax_send") String ajax_send, HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html; charset=utf-8"); PrintWriter printWriter = response.getWriter(); printWriter.print(ajax_send); printWriter.flush(); printWriter.close(); } } [20171121223024989]: /images/20220605/adaa619c446745ed841758351f9e2952.png [20171121223120281]: /images/20220605/958a99b80b8042ccb73d4c005be3306f.png [Link 1]: http://blog.csdn.net/yongh701/article/details/78543179 [Link 2]: http://blog.csdn.net/yongh701/article/details/78542420 [Link 3]: http://blog.csdn.net/yongh701/article/details/45558681
还没有评论,来说两句吧...