第十天;
构思
1.前台拿到要搜索的字符串,上传到后台
2.后台进行模糊查询,然后将数据返回前台
ps:
虽然知道没人看。。。。
还是写完吧在线答题源代码
onepage.jsp
添加如下代码
$(function() { //easyui组件封装以这种方式触发按钮
$("#searchtxt").textbox("textbox").bind("keyup", function() {
$("#searchShow").html("");
var time=new Date().getTime();
var searchContent = $("#searchtxt").val();
$.ajax({
type: "post",
url: "answer/searchlike.action",
data:{//传参
searchContent:searchContent,
time:time
},
success:function(datajson){
var jsonarry = datajson;
$.each(jsonarry,function(index,content){
var searchdiv = "<p align='left'> <a herf='javascript:;' onclick='searchtitle("+content.subjectid+");'>"+content.subjecttext+"</a></p><hr/>";
$("#searchShow").append(searchdiv);
});
},
error:function(data){
alert("error");
},
});
});
});
struts.xml
添加下面一段action
<action name="searchlike" class="subjectAction" method="searchlike"/>
SubjectAction.java
String searchContent;
public String getSearchContent() {
return searchContent;
}
public void setSearchContent(String searchContent) {
this.searchContent = searchContent;
}
/**
* 进行模糊搜索
* @throws IOException
*/
public void searchlike() throws IOException {
allSubject = subjectService.searchlike(searchContent);
utils.subjectJson(allSubject);
}
SubjectService.java
添加如下代码
/**
* 模糊查询
* @param searchContent
* @return
*/
public List searchlike(String searchContent) {
// TODO Auto-generated method stub
return subjectDAO.searchlike(searchContent);
}
SubjectDAO.java
添加如下代码
public List searchlike(String searchContent) {
String queryString = "from Subject where subjecttext like '%"+searchContent+"%'";
return getHibernateTemplate().find(queryString);
}
现在已经能查询出来数据了