日期:2014-05-16  浏览次数:20328 次

jquery autocomplete 与struts2 json插件结合简单例子

?

一个 简单的 自动补全例子:

js代码:

$('#username').autocomplete({
				   source: function(request, response) {
					   $.ajax({
				   			url:'userList',
				   			type: 'POST',
				   			data: {"key":$('#username').val()},
				   			dataType: 'json',
				   			success: function(data){
				   				response(data);
				   			}, 
				   			error: function(error){
				   				alert('数据处理异常,请联系相关人员或刷新重试');
				   			}
				   		}); 
				   },
				   minChars: 1,  //最小显示条数  
				   max: 3  //最大显示条数  
	        });

?html:

<input type="text" id="username"/>

?action:

/**
 * @author myemptyname
 * @date   2012-09-27
 */
public class UserAction extends ActionSupport {
       private List<String> userNameList;
       private String key;
       private UserService serivce;
       
       public String execute(){		
		return SUCCESS;
	}
        
        public String userList(){
    	     this.userNameList = this.service.getAllUserName(this.key);
    	     return "userList"; 
        }



       	public List<String> getUserNameList() {
		return userNameList;
	}

	public void setUserNameList(List<String> userNameList) {
		this.userNameList = userNameList;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}


} 

?struts.xml

?

	<action name="user_*" class="UserAction " method = "{1}">
			<result name="success" type="json"></result>
			<result name="userList" type="json">     
				<param name="root">userNameList</param>   
<!-- 限定传过去json参数,我们只需要列表  -->

			</result>
	</action> 
?

?