Spring Ajax一個簡單範例

來源:互聯網
上載者:User

標籤:upload   UI   round   util   view   generate   request   pac   png   

配置不說了。要在前面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;@Controllerpublic 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;

不要搞錯了!!!

!!

Spring Ajax一個簡單範例

相關文章

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。