springMVC与jquery结合使用完整例子

1.依赖


	4.0.0
	com.mycompany.app
	my-mvc
	war
	1.0-SNAPSHOT
	my-mvc Maven Webapp
	http://maven.apache.org

	
		3.2.13.RELEASE
		1.7.7
	

	
		
			junit
			junit
			4.10
			test
		

		
			org.springframework
			spring-context
			${spring-version}
		
		
			org.springframework
			spring-core
			${spring-version}
		
		
			org.springframework
			spring-context-support
			${spring-version}
		
		
			org.springframework
			spring-aop
			${spring-version}
		
		
			org.springframework
			spring-tx
			${spring-version}
		
		

		
			org.springframework
			spring-beans
			${spring-version}
		
		


		
		
			org.springframework
			spring-web
			${spring-version}
		
		
			org.springframework
			spring-webmvc
			${spring-version}
		

		
			javax.servlet
			jstl
			1.2
		

		
		
			javax.servlet
			servlet-api
			2.5
			provided
		
		
			javax.servlet.jsp
			jsp-api
			2.1
			provided
		

		
			org.codehaus.jackson
			jackson-core-lgpl
			1.9.13
		

		
			org.codehaus.jackson
			jackson-mapper-lgpl
			1.9.13
		
	
	
		my-mvc
		
			
				org.mortbay.jetty
				maven-jetty-plugin
			
			
				maven-war-plugin
				2.1-alpha-1
				
					
						default-war
						package
						
							war
						
					
				
			
		
	

          注意:上面必须添加jackson框架的依赖,否则springmvc就无法解析json,会报406错误

2.配置文件

        (1)web.xml

 
  
      
      
        index.jsp  
      
      
      
      
        my  
        org.springframework.web.servlet.DispatcherServlet  
        1  
      
      
      
        my  
        *.do  
     
  


      (2)springMVC配置文件my-servlet.xml




	
	
	
	
	
	
	    
		
	    
		
	






3.Get方式

       (1)Controller

package my.mvn.controller;



import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloWorldController 
{
	@RequestMapping(value="/ajax.do" ,method=RequestMethod.GET)
	@ResponseBody
	public Map say(Model model)
	{

		List list = new ArrayList();
		list.add("test");
		list.add("jquery");
	        Map modelMap = new HashMap(3);  
	        modelMap.put("total", "3");  
	        modelMap.put("data", list);  
	        modelMap.put("success", "true");  
	        return modelMap;  

	}
	@RequestMapping(value="/cpf.do")
	public String yemian(Model model)
	{
		System.out.println("enter");
		model.addAttribute("username", "lucy");
		
		return "cpf";
	}
}

       (2)页面


  








4.Post方式

       (1)Controller

package my.mvn.controller;



import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloWorldController 
{	
    @RequestMapping(value = "/ajax_post.do", method = RequestMethod.POST)  
    public @ResponseBody  
    User login(@RequestBody User user) {  
        return user;  
    }  
	
    @RequestMapping(value="/cpf.do")
    public String yemian(Model model)
    {
	System.out.println("enter");
	model.addAttribute("username", "lucy");
	
	return "cpf";
     }
	
}

          (2)页面


  








      测试:

springMVC与jquery结合使用完整例子_第1张图片




   另:项目结构为:

                                  springMVC与jquery结合使用完整例子_第2张图片



你可能感兴趣的:(springMVC)