• Spring Ajax一个简单样例


    配置不说了。要在前面helloworld的样例基础上弄。

    相同在hello下新建ajax.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ page isELIgnored ="false" %>
    <!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>Spring MVC Example with AJAX call</title>
    <style type="text/css">
    body {
        background-image:
            url('http://cdn3.crunchify.com/wp-content/uploads/2013/03/Crunchify.bg_.300.png');
    }
    </style>
    <script type="text/javascript"
        src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script type="text/javascript">
        function crunchifyAjax() {
            $.ajax({
                url : 'ajaxtest.html',
                success : function(data) {
                    $('#result').html(data);
                }
            });
        }
    </script>
     
    <script type="text/javascript">
        var intervalId = 0;
        intervalId = setInterval(crunchifyAjax, 3000);
    </script>
    </head>
    <body>
     <div align="center">
            <br> <br> ${message} <br> <br>
            <div id="result"></div>
            <br>
            <p>
                by <a href="http://crunchify.com">Crunchify.com</a>
            </p>
        </div>
    </body>
    </html>

    创建Controller:

    package com.cqu.tutorial;
    
    import java.util.Date;
    import java.util.Random;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.servlet.ModelAndView;
    @Controller
    public class SpringAjaxJQuery {
    	 @RequestMapping("/ajax")
    	    public ModelAndView helloAjaxTest() {
    	        return new ModelAndView("ajax", "message", "Crunchify Spring MVC with Ajax and JQuery Demo..");
    	    }
    	 
    	    @RequestMapping(value = "/ajaxtest", method = RequestMethod.GET)
    	    public @ResponseBody
    	    String getTime() {
    	 
    	        Random rand = new Random();
    	        float r = rand.nextFloat() * 100;
    	        String result = "<br>Next Random # is <b>" + r + "</b>. Generated on <b>" + new Date().toString() + "</b>";
    	        System.out.println("Debug Message from SpringAjaxJQuery Controller.." + new Date().toString());
    	        return result;
    	    }
    }
    

    最重要的一个问题就是import包的时候,ModelAndView引入的包是
    import org.springframework.web.servlet.ModelAndView;
    
    

    不要搞错了!!!

    !!

  • 相关阅读:
    投资银行的IT部门——不同之处与常见误解
    C++ error C2440: “类型转换” : 无法从“std::vector::iterator”转换为“
    查看端口占用
    Sc config http start= disabled
    DDL、DML和DCL的区别与理解
    不同网段,在路由器上如何设置网关
    服务器调优
    查看SQL Server版本信息
    基于32位Windows2003的数据库服务器优化,启用AWE,优化SQL Server
    在WIN7操作系统下,如何显示文件夹里文件的数目
  • 原文地址:https://www.cnblogs.com/cxchanpin/p/6789890.html
Copyright © 2020-2023  润新知