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>