struts2+Json 点击按钮加载用户列表

User 实体类

package entity;
/**
 * User 实体类
 * */
public class User {
	private int id;//编号
	private String name;//用户名
	private int age;//年龄
	/*
	 * 构造方法
	 * */
	public User() {
	}
	public User(int id, String name, int age) {
		this.id = id;
		this.name = name;
		this.age = age;
	}
	/*
	 * 封装方法
	 * */
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

 

UserAction 控制器

package action;
/**
 * UserAction 控制器
 * */
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

import entity.User;
public class UserAction extends ActionSupport {
	private List<User> users;//用户列表
	private int count;//统计用户数量
	public List<User> getUsers() {
		return users;
	}
	public void setUsers(List<User> users) {
		this.users = users;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public String list(){
		users = new ArrayList<User>();
		users.add(new User(1, "景临境", 23));
		users.add(new User(2, "景临境2", 24));
		users.add(new User(3, "景临境3", 25));
		count = users.size();
		return SUCCESS;
	}
}

 

struts.xml 配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="default" namespace="/" extends="json-default">
		<action name="list" class="action.UserAction" method="list">
			<!-- 定义类型为 JSON -->
			<!-- result type="json" -->
				<!-- 根元素
				<param name="root">count</param>
			</result -->
			<result type="json">
				<!-- 只返回指定的 2 个属性 -->
				<param name="includeProperties">
					users\[\d+\]\,count
				</param>
			</result>
			<!-- 拦截指定的属性 -->
			<!-- result type="json"> 
				<param name="excludeProperties"> count </param> 
			</result -->
		</action>
	</package>
</struts>    

 

index.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>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
	$("#btnUserList").click(function(){
		$.get("list.action",function(data){
			var row = "";
			var users = data.users;
			for(var i=0; i <users.length; i++){
				row += "<tr><td>";
				row += users[i].id;
				row += "</td><td>";
				row += users[i].name;
				row += "</td><td>";
				row += users[i].age;
				row += "</td></tr>";
				$("#userBody").append(row);
				row ="";
			}
		});
		$("#userBody").html("");
	});
});
</script>
</head>
<body>
<input type="button" value="加载所有用户" id="btnUserList">
<table width="350" border="1" align="center">
	<thead><tr><td>编号</td><td>姓名</td><td>年龄</td></tr></thead>
	<tbody id="userBody"></tbody>
</table>
</body>
</html>

 

效果图:
struts2+Json 点击按钮加载用户列表


struts2+Json 点击按钮加载用户列表
 

 

 

你可能感兴趣的:(struts2)