使用struts2返回json

UserAction:

package com.test.user.action;

import java.util.List;
import com.test.user.domain.User;

public class UserAction{
	
	private List<User> UserList;
	private User user;

	/**
	 * 查询所有信息
	 * @return String
	 */
	public String doList(){
		//查询出List集合
		usernList = userMapper.selectByExample(stationExample);
		
		return "json";
	}

	// get and set method

	public List<User> getUserList() {
		return userList;
	}


	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}


 

User:

package com.test.user.domain;


public class User {

	private String id;
	private String username;
	private String password;

	// get and set method
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}


 

struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

  <package name="struts-user" extends="json-default" namespace="/user">
    <action name="user_*" class="com.test.action.UserAction" method="{1}">
     	 <result name="json" type="json"></result>
    </action>

  </package>
</struts>


 

userList.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<base href="<%=basePath%>">
<title>Insert title here</title>
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript">
	function gotoPage() {
		new Ajax.Request('user/user_doList.action', {
			onComplete : function(o) {
				var obj = o.responseText.evalJSON();
				displayUser(obj.userList);
			}
		});
	}
	function displayUser(userList) {
		$('t_body').update();
		userList.each(function(item, n) {

			var row = new Element("tr", {});
			row.insert({
				"bottom" : new Element("td", {}).update(n + 1)
			});
			row.insert({
				"bottom" : new Element("td", {}).update(item.username)
			});
			row.insert({
				"bottom" : new Element("td", {}).update(item.password)
			});
			$("t_body").insert({
				"bottom" : row
			});
		});
	}
	gotoPage();
</script>

</head>
<body>

			<table >
				<thead>
					<tr>
						<td >编号</td>
						<td>用户名</td>
						<td>密码</td>
					</tr>
				</thead>
				<tbody id="t_body">
				</tbody>
			</table>
</body>
</html>


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值