struts2的action类:
public class AllUsersAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private Connection conn=null;
private Statement stmt=null;
private ResultSet rs=null;
private User user;
public void setUser(User user) {
this.user = user;
}
public User getUser() {
return user;
}
private ArrayList<User> users;
public ArrayList<User> getUsers() {
return users;
}
public void setUsers(ArrayList<User> users) {
this.users = users;
}
public String excute()throws Exception{
MYSQL_Conn mc=new MYSQL_Conn();
String sql=”select * from users”;
conn=mc.getConn();
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
int i=1;
while(rs.next()){
user.setId(rs.getInt(“id”));
user.setUsername(rs.getString(“username”));
user.setUsername(rs.getString(“password”));
users.add(i, user);
i++;
}
return SUCCESS;
}
}
struts.xml:
<action name=”allUsers” class=”com.action.AllUsersAction”>
<result name=”success”>/allUsers.jsp</result>
</action>
allUsers.jsp:
<%
List<User> users=new ArrayList<User>();
User user=new User();
%>
public class AllUsersAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private Connection conn=null;
private Statement stmt=null;
private ResultSet rs=null;
private User user;
public void setUser(User user) {
this.user = user;
}
public User getUser() {
return user;
}
private ArrayList<User> users;
public ArrayList<User> getUsers() {
return users;
}
public void setUsers(ArrayList<User> users) {
this.users = users;
}
public String excute()throws Exception{
MYSQL_Conn mc=new MYSQL_Conn();
String sql=”select * from users”;
conn=mc.getConn();
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
int i=1;
while(rs.next()){
user.setId(rs.getInt(“id”));
user.setUsername(rs.getString(“username”));
user.setUsername(rs.getString(“password”));
users.add(i, user);
i++;
}
return SUCCESS;
}
}
struts.xml:
<action name=”allUsers” class=”com.action.AllUsersAction”>
<result name=”success”>/allUsers.jsp</result>
</action>
allUsers.jsp:
<%
List<User> users=new ArrayList<User>();
User user=new User();
%>
<table>
<tr>
<td>ID</td><td>Username</td><td>Password</td>
</tr>
<%
for(int i=1;i<users.size();i++){
%>
<tr>
<td><%=users.get(i).getId() %></td>
<td><%=users.get(i).getUsername() %></td>
<td><%=users.get(i).getPassword() %></td>
</tr>
<%
}
%>
</table>
<a href=”index.jsp“>返回</a>
解决方案
15
放入struts值栈中
36
ActionContext 值可以put进去,在页面可以用el表达式查看
15
楼上正解
1
你不是都写出来了吗。
1