配置不说了。要在前面helloworld的样例基础上弄。
相同在hello下新建ajax.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ page isELIgnored ="false" %>创建Controller:Spring MVC Example with AJAX call ${message}
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 = "Next Random # is " + r + ". Generated on " + new Date().toString() + ""; System.out.println("Debug Message from SpringAjaxJQuery Controller.." + new Date().toString()); return result; }}最重要的一个问题就是import包的时候,ModelAndView引入的包是
import org.springframework.web.servlet.ModelAndView;
不要搞错了!!!
!
!!
!
!
!
!
!