CandyGoo 2011-05-18
struts2、jquery与json的集成是怎样实现异步交互的,下面我们来做个综合的小案例。
做案例的前提是:先要引入必要的jar包。
比如:
1、struts2-core-2.x.x.x.jar:Struts2框架的核心类库
2、xwork-2.x.x.jar:XWork类库,Struts2在其上构建
3、ognl-2.x.x.jar:对象图导航语言(ObjectGraphNavigationLanguage),struts2框架通过其读写对象的属性
4、freemarker-2.3.x.jar:Struts2的UI标签的模板使用FreeMarker编写
5、commons-logging-1.1.x.jar:ASF出品的日志包,Struts2框架使用这个日志包来支持Log4J和JDK1.4+的日志记录。
6、commons-fileupload-1.2.1.jar文件上传组件,2.1.6版本后必须加入此文件
7、struts2-json-plugin.jarstruts2与json的插件
cn.z_xiaofei168.domain包
Java代码
packagecn.z_xiaofei168.domain;
importjava.io.Serializable;
importjava.util.Date;
publicclassUserimplementsSerializable{
/**
*@authorz_xiaofei168
*/
privatestaticfinallongserialVersionUID=1L;
/**用户的编号*/
privateIntegerid;
/**用户的名称*/
privateStringname;
/**用户的密码*/
privateStringpass;
/**用户的邮箱*/
privateStringemail;
/**用户的注册日期*/
privateDaterdate;
publicUser(){
super();
}
//以下省略所以的set和get方法
}
cn.z_xiaofei168.action包
Java代码
packagecn.z_xiaofei168.action;
importjava.util.List;
importcn.z_xiaofei168.dao.Pagination;
importcn.z_xiaofei168.domain.User;
importcn.z_xiaofei168.service.UserServiceImpl;
importcom.opensymphony.xwork2.ActionSupport;
publicclassUserActionextendsActionSupport{
/**
*@authorz_xiaofei168
*/
privatestaticfinallongserialVersionUID=1L;
/**业务层对象*/
privateUserServiceImpluserServiceImpl;
privateList<User>list;
publicUserServiceImplgetUserServiceImpl(){
returnuserServiceImpl;
}
/**set方法注入*/
publicvoidsetUserServiceImpl(UserServiceImpluserServiceImpl){
this.userServiceImpl=userServiceImpl;
}
publicList<User>getList(){
returnlist;
}
publicvoidsetList(List<User>list){
this.list=list;
}
/**模糊查新的方法*/
publicStringfindByDarkName()throwsException{
list=userServiceImpl.findByDarkName(user.getName());
return"findByDarkName";
}
}
cn.z_xiaofei168.dao包
Java代码
packagecn.z_xiaofei168.dao;
importjava.util.List;
importcn.z_xiaofei168.domain.User;
publicinterfaceUserDao{
publicList<User>findByDarkName(Stringname)throwsException;
}
Java代码
packagecn.z_xiaofei168.dao;
importjava.util.List;
importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;
importcn.z_xiaofei168.domain.User;
publicclassUserDaoImplextendsHibernateDaoSupportimplementsUserDao{
@SuppressWarnings("unchecked")
publicList<User>findByDarkName(Stringname)throwsException{
Listlist=this.getHibernateTemplate().find("fromUserwherenamelike?","%"+name+"%");
System.out.println("findByDarkName模糊:"+list.size());
returnlist;
}
}
cn.z_xiaofei168.service包
Java代码
packagecn.z_xiaofei168.service;
importcn.z_xiaofei168.dao.UserDao;
publicinterfaceUserServiceextendsUserDao{
}
Java代码
packagecn.z_xiaofei168.service;
importjava.util.List;
importcn.z_xiaofei168.dao.UserDaoImpl;
importcn.z_xiaofei168.domain.User;
publicclassUserServiceImplimplementsUserService{
privateUserDaoImpluserDaoImpl;
publicUserDaoImplgetUserDaoImpl(){
returnuserDaoImpl;
}
/**set方法进行注入*/
publicvoidsetUserDaoImpl(UserDaoImpluserDaoImpl){
this.userDaoImpl=userDaoImpl;
}
publicList<User>findByDarkName(Stringname)throwsException{
returnuserDaoImpl.findByDarkName(name);
}
}
struts.xml
Xml代码
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<packagename="z_xiaofei"extends="json-default"namespace="/csdn">
<global-results>
<resultname="input">/index.jsp</result>
</global-results>
<actionname="user_*"class="cn.z_xiaofei168.action.UserAction"method="{1}">
<!--返回的是一个user对象user对象(id,name,pass)-->
<resultname="findByDarkName"type="json">
<!--param参数的配置,includeProperties-->
<paramname="includeProperties">
list\[\d+\]\.id,list\[\d+\]\.name,list\[\d+\]\.pass
</param>
</result>
</action>
</package>
</struts>
jsp页面
Jsp代码
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"
+request.getServerName()+":"+request.getServerPort()
+path+"/";
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>用户信息输入</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->
<scripttype="text/javascript"
src="${pageContext.request.contextPath}/js/jquery-1.6.js">
</script>
<scripttype="text/javascript">
functionfindByDarkName(){
varname=$("#darkName").val();
$.ajax({
type:"post",
url:"csdn/user_findByDarkName",
data:"user.name="+name,
dataType:"json",
success:function(data){
$.each(data.list,function(i,entity){
$("#darklist").append("<trstyle='border:1px'></tr>")
.append("<td>"+entity.id+"</td>")
.append("<td>"+entity.name+"</td>")
.append("<td>"+entity.pass+"</td>");
});
}
});
}
</script>
</head>
<body>
<br/>
<div>
<h2>
<fontcolor="red">模糊查询</font>
</h2>
<br/>
<h3>
模糊性:在给出的信息中,与之有相同的元素即可
</h3>
<br/>
<inputtype="text"id="darkName"name="user.name"/>
<spanid="msg"></span>
<inputtype="button"value="模糊查询"onclick="findByDarkName()"/>
</div>
<table>
<thead>
<tr>
<th>序号</th>
<th>名称</th>
<th>密码</th>
<th>注册日期</th>
<th>操作</th>
</tr>
</thead>
<tbodyid="darklist">
</tbody>
</table>
</body>
</html>