springmvc+ajax实现省市区三级联动以及406 (Not Acceptable)的解决办法

我做一个小的测试实现功能

最主要是说一下遇到的问题:

我先用的spring3.2的包但是遇到好多的问我问题,

(1)首先就是不能使用*.htm,*.html的地址后缀;

(2)这个会报406 (Not Acceptable)的错误:

办法:1.你可以换成4.0的包,然后就能支持*.htm的后缀了;2.看看你的json包导进去没jackson-annotations-2.7.0.jar  ,jackson-core-2.7.0.jar ,jackson-databind-2.7.0.jar; 

3.在spring配置文件中加入以下代码

    
	        
	            
	            text/html;charset=UTF-8    
	            
	        
	    
	     
	    
	        
	            
	                
	            
	        
	

下面是实现的代码:

1.首先创建页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>


	
	个人网站
	 


       

省市区三级联动

省份: 城市: 区域:

2.后台代码:

 controller层:
@Controller
public class LoginController {
	
	@Autowired
	private ShengService ss;
	@Autowired
	private PraviceService ps;
	@RequestMapping("/wel.htm")
	  public String getWel(HttpServletRequest req)
	  {
		log.info("初始化页面.................");
		List list = ps.getId();
		req.setAttribute("list", list);
	    return "index";
	  }
	@RequestMapping("/sheng.htm")
	@ResponseBody
	public List getS(@RequestParam(value = "pravice")int praviceID){
		System.out.println(praviceID);
		List name = ss.getSheng(praviceID);
		for(Sheng s:name){
			System.out.println(s.getName());
		}
		return name;
	}
	@RequestMapping("/sheng1.htm")
	@ResponseBody
	public List getQ(@RequestParam(value = "sheng")int shengID){
		System.out.println(shengID);
		List name = ss.getSheng(shengID);
		for(Sheng s:name){
			System.out.println(s.getName());
		}
		return name;
	}
}                  
dao层

public interface ShengDao {
	
	@Select("select id,name,father from sheng where father=#{father}")
	List getSheng(int father);
	
}
public interface PraviceDao {
	@Select("select id,name from pravice")
	List getPravice();
	
}
service层
@Service
public class ShengService {
	
	@Autowired
	private ShengDao sd;
	
	public List getSheng(int id){
		List s = sd.getSheng(id);
		return s;
	}
	
}
@Service
public class PraviceService {

	@Autowired
	private PraviceDao pd;
	
	public List getId(){
		List list = new ArrayList();
		list = pd.getPravice();
		return list;
	}
	
}
实体类
实体类可以创建一个,不需要弄成两个或者多个。
public class Sheng {
	private int id;
	private String name;
	private int father;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getFather() {
		return father;
	}
	public void setFather(int father) {
		this.father = father;
	}
	
}
public class Pravice {
	private int id;
	private String name ;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}
数据库结构大家看着实体类自己做一下。

4.效果:

springmvc+ajax实现省市区三级联动以及406 (Not Acceptable)的解决办法_第1张图片

你可能感兴趣的:(java,javaweb)