<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
$.ajax()-->XMLHttpRequest
$.ajax({
url:xxx,type:post|get,data:提交的数据,async:true|false同步或异步处理,dataType:json|html|script预期服务器返回结果类型,success:成功回调函数,error:失败的回调函数,beforeSend:请求发送前回调函数,
});
$("#showBtn"),id选择器
$(".showBtn"),类选择器
$("a"),所有a标签
$("table tr") table下面的所有tr
<a href="#" id="showBtn">查看上证指数</a>
$("#showBtn").click(function(){});
var s_opt='<option value="'+id+'">'+name+'</option>';
var $opt=$(s_opt);//将字符串转换成jquery对象
//将option添加到select中
$("s1").append($opt);
省份:
<select id="s1"></select>
1.触发事件源,下拉选择框,$("#s1")
2.触发事件时机,onchange选项发生改变
3.执行什么样操作
//提取当前选中的option内容
//将内容显示到span中
$(function(){
$("#s1").change(function(){
//提取选中项
var value=$("#s1 option:selected").text();//var value=$("#s1").val();
//显示到span中
$("#name_span").html(value);
});
})
Servlet往前台传jsp:
Note note=new Note();
//note.setXXX();填很多例子
JSONObject json=JSONObject.fromObject(note);
String json_str=json.toString();
//将list转成json字符串
JSONArray jsonlist=JSONArray.fromObject(list);
response.setContentType("text/plain;charset=utf-8");
PrintWriter out=response.getWriter();
out.print(jsron_str);
out.flush();
out.close();
springmvc
--ioc,webmvc
--aplicationContext.xml
/ajax.do
-->DispatcherServlet
-->HandlerMapping
-->LoadNoteController
-->调用jackson.jar包将Controller返回结果转成json格式输出(以前是viewResolver转到jsp)
需要的jar包:
commons-logging-1.2.jar
jackson-annotations-2.2.1.jar
jackson-core-2.2.1.jar
jackson-databind-2.2.1.jar
spring-aop-4.0.2.RELEASE.jar
spring-beans-4.0.2.RELEASE.jar
spring-context-4.0.2.RELEASE.jar
spring-core-4.0.2.RELEASE.jar
spring-expression-4.0.2.RELEASE.jar
spring-web-4.0.2.RELEASE.jar
spring-webmvc-4.0.2.RELEASE.jar
web.xml:
<filter>
<filter-name>myfilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>myfilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置handlermaping -->
<mvc:annotation-driven/>
<!-- 扫描controller -->
<context:component-scan base-package="org.alexhe"></context:component-scan>
</beans>
LoadNoteController.java:
package org.alexhe.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.alexhe.entity.Note;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class LoadNoteController {
//调用Dao获取笔记数据,采用json返回
@RequestMapping("/ajax.do")
@ResponseBody//将返回结果调用jackson.jar包
public List<Note> execute(){
List<Note> list=new ArrayList<Note>();
Note note=new Note();
note.setId("01");
note.setName("springmvc");
Note note1=new Note();
note1.setId("02");
note1.setName("java");
list.add(note);
list.add(note1);
return list;
}
@RequestMapping("/ajax2.do")
@ResponseBody//将返回结果调用jackson.jar包
public Note load1(){
Note note=new Note();
note.setId("111");
note.setName("look");
return note;
}
@RequestMapping("/ajax3.do")
@ResponseBody//将返回结果调用jackson.jar包
public Map<String,Object> load2(){
Map<String,Object> map=new HashMap<String,Object>();
map.put("id", 1);
Note note =new Note();
note.setId("22");
note.setName("哈哈哈哈");
map.put("我是note", note);
return map;
}
}