package jquery.getstart;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class AutoCompleteAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String queryString;
public String getQueryString() {
return queryString;
}
public void setQueryString(String queryString) {
this.queryString = queryString;
}
public List<ProvinceJsonBean> getResults(){
return new ArrayList<ProvinceJsonBean>(){
{
ProvinceJsonBean jsonBean=new ProvinceJsonBean();
jsonBean.setText("北京");
Province province=new Province();
province.setId("1");
province.setName("北京");
jsonBean.setJson(province);
this.add(jsonBean);
}
};
}
public String query() {
return SUCCESS;
}
public static class ProvinceJsonBean{
private String text;
private Province json;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Province getJson() {
return json;
}
public void setJson(Province json) {
this.json = json;
}
}
}
代码存放 http://svn.w18.net/svn/junfordemo/trunk/ssh_jquery
我的代码 实现了城市的查找
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>jQuery入门</title>
<link href="css/james.css" type="text/css" rel="stylesheet"/>
<link href="css/default.css" type="text/css" rel="stylesheet"/>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.james.js"></script>
<script type="text/javascript">
var id;
$(document).ready(function() {
$("#orderedlist").addClass("red");
});
$(document).ready(function() {
$("#orderedlist > li").addClass("blue");
});
$(document).ready(function() {
$("#orderedlist li").hover(function() {
$(this).addClass("blue");
},function(){
$(this).removeClass("blue");
});
});
$(document).ready(function() {
$("#orderedlist li:last").hover(function() {
$(this).addClass("blue");
},function(){
$(this).removeClass("blue");
});
});
$(document).ready(function() {
$('#faq').find('dd').hide().end().find('dt').click(function() {
$(this).next().slideToggle();
});
});
$(document).ready(function() {
$("#province").james("autocomplete.action",
{method:"post",minlength:1,varname:"queryString",
onSelect:function(data,json){
id=json.id;
return json.name;
}
});
});
$(document).ready(function() {
$("#savesss").click(function() {
document.location.href="save.action?id="+id
});
});
</script>
</head>
<body>
<h1>第一个程序</h1>
<a href="">Link</a>
<hr />
<ol id="orderedlist">
<li>fsdg</li>
<li>sdfg</li>
<li>王朝</li>
</ol>
<hr />
<h1>相邻元素的选择</h1>
<h2>FAQ</h2>
<dl id="faq">
<dt>为什么使用jQuery?</dt>
<dd>jQuery可以用最少的javascript代码实现最多的功能,而且性能很好。</dd>
<dt>和jQuery类似的api还有哪些?</dt>
<dd>比如prototype、mootools、YUI、dojo和extjs等。</dd>
</dl>
<hr />
<label for="province">你来自哪个省/直辖市:</label><input id="province" type="text" />
<input id="savesss" type="button" />
</body>
</html>
action的配置
<struts>
<constant name="struts.objectFactory"
value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<package name="/city" extends="json-default">
<action name="autocomplete" class="autocompleteAction" method="query">
<result type="json">
<param name="root">results</param>
</result>
</action>
</package>
</struts>
实体类