【SpringMVC】Ajax

来源:互联网 发布:数据库 er设计 编辑:程序博客网 时间:2024/05/21 14:42

SpringMVC的Ajax网上大部分的教程都使用@Respondbody然后返回一个map完成,我觉得这样很繁琐,甚至还要自己配上一个json转换包,不然会出现406警告。其实我觉得大可不必这样,毕竟SpringMVC的@RequestMapping方法中,可以用HttpServletRequest request, HttpServletResponse respons作为参数,直接在网页该打印什么就打印,返回给Ajax就行。

做一个如下的小例子说明这个问题:


先贴上自己的工程目录结构:


1、首先是web.xml这里要说明一下,因为我不想用javascript原生的ajax写这么多代码,所以,引入了jquery。那就注定要使用jquery的js文件。所以这里我将SpringMVC的action都改成了*.html,这点的详细我在《【SpringMVC】指明actions的后缀》(点击打开链接)说过了。

<?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》(点击打开链接)中对比是一字没改的!

<?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" /><beanclass="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 onclick="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交互》(点击打开链接),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;@Controllerpublic 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();}}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 nike拖鞋买小了怎么办 京东换货没货了怎么办 淘宝申请退货退款卖家不处理怎么办 家具连接扣坏了怎么办 锅炉炉水碱度低怎么办 快递里面的东西少了怎么办 很多闲置在家里的东西怎么办 闲鱼买的东西与原物不对怎么办 退款后又发货了怎么办 淘宝卖家不发货也不退款怎么办 已经申请退款卖家仍发货怎么办 淘宝卖家已发货买家申请退款怎么办 爱上街App不发货怎么办 阿里卖家虚假发货怎么办 联系不到淘宝卖家怎么办 没货买家不退款怎么办 买到假货没有发票怎么办 淘宝买的手机发票怎么办 淘宝上的发票丢了怎么办 淘宝买的东西电子发票怎么办 发票联给错顾客怎么办 客人让多给发票怎么办 13岁早晨上学叫不醒怎么办 官换机过保坏了怎么办 官换机在保坏了怎么办 留学生美国东西寄回国怎么办便宜 淘宝东西没到收货了怎么办 在淘宝上买东西被骗怎么办 网上把钱骗走了怎么办 在淘宝上卖号被骗了怎么办 如果淘宝商家保证金不够退款怎么办 天猫账号忘记了怎么办 姨妈来了10天了怎么办 微店商品无资质被下架怎么办 微店商品无资质怎么办 淘宝买东西提前确认收货了怎么办 苹果手机王者荣耀充值充多了怎么办 买家淘宝账号出现虚拟交易怎么办 贵州通登录不上怎么办 华为手机媒体声音小怎么办 苹果七内存满了怎么办