浅谈struts2、jquery与json的集成
struts2、jquery与json的集成是怎样实现异步交互的,下面我们来做个综合的小案例。
做案例的前提是:先要引入必要的jar包。
比如:
1、struts2-core-2.x.x.x.jar :Struts 2框架的核心类库
2、xwork-2.x.x.jar :XWork类库,Struts 2在其上构建
3、ognl-2.x.x.jar :对象图导航语言(Object Graph Navigation Language),struts2框架通过其读写对象的属性
4、freemarker-2.3.x.jar :Struts 2的UI标签的模板使用FreeMarker编写
5、commons-logging-1.1.x.jar :ASF出品的日志包,Struts 2框架使用这个日志包来支持Log4J和JDK 1.4+的日志记录。
6、commons-fileupload-1.2.1.jar 文件上传组件,2.1.6版本后必须加入此文件
7、struts2-json-plugin.jar struts2与json的插件
cn.z_xiaofei168.domain包
package cn.z_xiaofei168.domain;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
/**
* @author z_xiaofei168
*/
private static final long serialVersionUID = 1L;
/** 用户的编号 */
private Integer id;
/** 用户的名称 */
private String name;
/** 用户的密码 */
private String pass;
/** 用户的邮箱 */
private String email;
/** 用户的注册日期 */
private Date rdate;
public User() {
super();
}
//以下省略所以的set和get方法
}
cn.z_xiaofei168.action包
package cn.z_xiaofei168.action;
import java.util.List;
import cn.z_xiaofei168.dao.Pagination;
import cn.z_xiaofei168.domain.User;
import cn.z_xiaofei168.service.UserServiceImpl;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
/**
* @author z_xiaofei168
*/
private static final long serialVersionUID = 1L;
/** 业务层对象 */
private UserServiceImpl userServiceImpl;
private List<User> list;
public UserServiceImpl getUserServiceImpl() {
return userServiceImpl;
}
/** set方法注入 */
public void setUserServiceImpl(UserServiceImpl userServiceImpl) {
this.userServiceImpl = userServiceImpl;
}
public List<User> getList() {
return list;
}
public void setList(List<User> list) {
this.list = list;
}
/** 模糊查新的方法 */
public String findByDarkName() throws Exception{
list = userServiceImpl.findByDarkName(user.getName());
return "findByDarkName";
}
}
cn.z_xiaofei168.dao包
package cn.z_xiaofei168.dao;
import java.util.List;
import cn.z_xiaofei168.domain.User;
public interface UserDao {
public List<User> findByDarkName(String name) throws Exception;
}
package cn.z_xiaofei168.dao;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.z_xiaofei168.domain.User;
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
@SuppressWarnings("unchecked")
public List<User> findByDarkName(String name) throws Exception {
List list = this.getHibernateTemplate().find("from User where name like ?","%"+name+"%");
System.out.println("findByDarkName模糊:"+list.size());
return list;
}
}
cn.z_xiaofei168.service包
package cn.z_xiaofei168.service;
import cn.z_xiaofei168.dao.UserDao;
public interface UserService extends UserDao {
}
package cn.z_xiaofei168.service;
import java.util.List;
import cn.z_xiaofei168.dao.UserDaoImpl;
import cn.z_xiaofei168.domain.User;
public class UserServiceImpl implements UserService {
private UserDaoImpl userDaoImpl;
public UserDaoImpl getUserDaoImpl() {
return userDaoImpl;
}
/** set方法进行注入 */
public void setUserDaoImpl(UserDaoImpl userDaoImpl) {
this.userDaoImpl = userDaoImpl;
}
public List<User> findByDarkName(String name) throws Exception {
return userDaoImpl.findByDarkName(name);
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <package name="z_xiaofei" extends="json-default" namespace="/csdn"> <global-results> <result name="input">/index.jsp</result> </global-results> <action name="user_*" class="cn.z_xiaofei168.action.UserAction" method="{1}"> <!-- 返回的是一个user对象 user对象(id,name,pass) --> <result name="findByDarkName" type="json"> <!-- param参数的配置,includeProperties --> <param name="includeProperties"> list\[\d+\]\.id, list\[\d+\]\.name, list\[\d+\]\.pass </param> </result> </action> </package> </struts>
jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>用户信息输入</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.6.js"> </script> <script type="text/javascript"> function findByDarkName() { var name = $("#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("<tr style='border:1px'></tr>") .append("<td>"+entity.id+"</td>") .append("<td>"+entity.name+"</td>") .append("<td>"+entity.pass+"</td>"); }); } }); } </script> </head> <body> <br /> <div> <h2> <font color="red">模糊查询</font> </h2> <br /> <h3> 模糊性:在给出的信息中,与之有相同的元素即可 </h3> <br /> <input type="text" id="darkName" name="user.name" /> <span id="msg"></span> <input type="button" value="模糊查询" οnclick="findByDarkName()" /> </div> <table> <thead> <tr> <th>序号</th> <th>名称</th> <th>密码</th> <th>注册日期</th> <th>操作</th> </tr> </thead> <tbody id="darklist"> </tbody> </table> </body> </html>