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包
001 | package cn.z_xiaofei168.domain; |
003 | import java.io.Serializable; |
004 | import java.util.Date; |
006 | public class User implements Serializable { |
009 | * @author z_xiaofei168 |
011 | private static final long serialVersionUID = 1L; |
033 | cn.z_xiaofei168.action包 |
037 | package cn.z_xiaofei168.action; |
039 | import java.util.List; |
041 | import cn.z_xiaofei168.dao.Pagination; |
042 | import cn.z_xiaofei168.domain.User; |
043 | import cn.z_xiaofei168.service.UserServiceImpl; |
045 | import com.opensymphony.xwork2.ActionSupport; |
047 | public class UserAction extends ActionSupport { |
050 | * @author z_xiaofei168 |
052 | private static final long serialVersionUID = 1L; |
055 | private UserServiceImpl userServiceImpl; |
056 | private List<User> list; |
058 | public UserServiceImpl getUserServiceImpl() { |
059 | return userServiceImpl; |
063 | public void setUserServiceImpl(UserServiceImpl userServiceImpl) { |
064 | this .userServiceImpl = userServiceImpl; |
067 | public List<User> getList() { |
071 | public void setList(List<User> list) { |
076 | public String findByDarkName() throws Exception{ |
077 | list = userServiceImpl.findByDarkName(user.getName()); |
078 | return "findByDarkName" ; |
089 | package cn.z_xiaofei168.dao; |
091 | import java.util.List; |
093 | import cn.z_xiaofei168.domain.User; |
095 | public interface UserDao { |
097 | public List<User> findByDarkName(String name) throws Exception; |
104 | package cn.z_xiaofei168.dao; |
106 | import java.util.List; |
108 | import org.springframework.orm.hibernate3.support.HibernateDaoSupport; |
110 | import cn.z_xiaofei168.domain.User; |
112 | public class UserDaoImpl extends HibernateDaoSupport implements UserDao { |
114 | @SuppressWarnings ( "unchecked" ) |
115 | public List<User> findByDarkName(String name) throws Exception { |
117 | List list = this .getHibernateTemplate().find( "from User where name like ?" , "%" +name+ "%" ); |
118 | System.out.println( "findByDarkName模糊:" +list.size()); |
125 | cn.z_xiaofei168.service包 |
130 | package cn.z_xiaofei168.service; |
132 | import cn.z_xiaofei168.dao.UserDao; |
134 | public interface UserService extends UserDao { |
141 | package cn.z_xiaofei168.service; |
143 | import java.util.List; |
145 | import cn.z_xiaofei168.dao.UserDaoImpl; |
146 | import cn.z_xiaofei168.domain.User; |
148 | public class UserServiceImpl implements UserService { |
150 | private UserDaoImpl userDaoImpl; |
152 | public UserDaoImpl getUserDaoImpl() { |
157 | public void setUserDaoImpl(UserDaoImpl userDaoImpl) { |
158 | this .userDaoImpl = userDaoImpl; |
161 | public List<User> findByDarkName(String name) throws Exception { |
162 | return userDaoImpl.findByDarkName(name); |
struts.xml
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | <!DOCTYPE struts PUBLIC |
03 | "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" |
04 | "http://struts.apache.org/dtds/struts-2.1.7.dtd"> |
06 | < package name = "z_xiaofei" extends = "json-default" namespace = "/csdn" > |
08 | < result name = "input" >/index.jsp</ result > |
11 | < action name = "user_*" class = "cn.z_xiaofei168.action.UserAction" method = "{1}" > |
14 | < result name = "findByDarkName" type = "json" > |
16 | < param name = "includeProperties" > |
17 | list\[\d+\]\.id, list\[\d+\]\.name, list\[\d+\]\.pass |
jsp页面
01 | <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> |
03 | String path = request.getContextPath(); |
04 | String basePath = request.getScheme() + "://" |
05 | + request.getServerName() + ":" + request.getServerPort() |
09 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
12 | < base href="<%=basePath%>"> |
16 | < meta http-equiv = "pragma" content = "no-cache" > |
17 | < meta http-equiv = "cache-control" content = "no-cache" > |
18 | < meta http-equiv = "expires" content = "0" > |
19 | < meta http-equiv = "keywords" content = "keyword1,keyword2,keyword3" > |
20 | < meta http-equiv = "description" content = "This is my page" > |
24 | < script type = "text/javascript" |
25 | src = "${pageContext.request.contextPath}/js/jquery-1.6.js" > |
27 | < script type = "text/javascript" > |
29 | function findByDarkName() { |
30 | var name = $("#darkName").val(); |
33 | url:"csdn/user_findByDarkName", |
34 | data:"user.name="+name, |
36 | success:function(data){ |
37 | $.each(data.list,function(i,entity){ |
38 | $("#darklist").append("< tr style = 'border:1px' ></ tr >") |
39 | .append("< td >"+entity.id+"</ td >") |
40 | .append("< td >"+entity.name+"</ td >") |
41 | .append("< td >"+entity.pass+"</ td >"); |
53 | < font color = "red" >模糊查询</ font > |
模糊性:在给出的信息中,与之有相同的元素即可
03 | < input type = "text" id = "darkName" name = "user.name" /> |
05 | < input type = "button" value = "模糊查询" onclick = "findByDarkName()" /> |
struts配置文件里有个type=json 并且文件继承了json-default,该文件在struts2-jon-plugin.jar里面定义了.里边定义了一个interceptor,会将action中提供了get set方法的属性封装成json格式数据response给客户端,