Struts2中action向页面传值的问题
action:publicclassUsersActionextendsActionSupport{privateUsersusers=newUsers();privat...
action:
public class UsersAction extends ActionSupport {
private Users users=new Users();
private UsersBiz usersBiz=null;
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
public UsersBiz getUsersBiz() {
return usersBiz;
}
public void setUsersBiz(UsersBiz usersBiz) {
this.usersBiz = usersBiz;
}
private List<Users> ulist=new ArrayList<Users>();;
public List<Users> getUlist() {
return ulist;
}
public void setUlist(List<Users> ulist) {
this.ulist = ulist;
}
public String login()
{
HttpServletRequest request = ServletActionContext.getRequest();
ulist=usersBiz.getUsersByConditon(users);
Users us=new Users();
for(int i=0;i<ulist.size();i++)
{
us=ulist.get(i);
}
request.setAttribute("us", us);
request.setAttribute("ulist", ulist);
******us 和 ulist 都有数据******
return "add";
}
页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%
String path=request.getContextPath();
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
%>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:form action="usersview.login" method="post">
<table border="1">
<tr >
<td>编号</td>
<td>部门名称</td>
<td>电话</td>
<td>门牌号</td>
<td>操作</td>
</tr>
<tr>
<td>${us.uid}</td>
<td>${us.uname}</td>
<td>${us.uage}</td>
<td>${us.uphone}</td>
<td><a href="">添加</a></td>
</tr>
<s:iterator value="request.ulist" id="us">
<tr>
<td><s:property value="#us.uid"/></td>
<td><s:property value="#us.uname"/></td>
<td><s:property value="#us.uage"/></td>
<td><s:property value="#us.uphone"/></td>
<td><a href="">添加</a></td>
</tr>
</s:iterator>
</table>
</s:form>
</body>
</html>
两个tr都输出不来值~求教高手帮解答一下 展开
public class UsersAction extends ActionSupport {
private Users users=new Users();
private UsersBiz usersBiz=null;
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
public UsersBiz getUsersBiz() {
return usersBiz;
}
public void setUsersBiz(UsersBiz usersBiz) {
this.usersBiz = usersBiz;
}
private List<Users> ulist=new ArrayList<Users>();;
public List<Users> getUlist() {
return ulist;
}
public void setUlist(List<Users> ulist) {
this.ulist = ulist;
}
public String login()
{
HttpServletRequest request = ServletActionContext.getRequest();
ulist=usersBiz.getUsersByConditon(users);
Users us=new Users();
for(int i=0;i<ulist.size();i++)
{
us=ulist.get(i);
}
request.setAttribute("us", us);
request.setAttribute("ulist", ulist);
******us 和 ulist 都有数据******
return "add";
}
页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%
String path=request.getContextPath();
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
%>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:form action="usersview.login" method="post">
<table border="1">
<tr >
<td>编号</td>
<td>部门名称</td>
<td>电话</td>
<td>门牌号</td>
<td>操作</td>
</tr>
<tr>
<td>${us.uid}</td>
<td>${us.uname}</td>
<td>${us.uage}</td>
<td>${us.uphone}</td>
<td><a href="">添加</a></td>
</tr>
<s:iterator value="request.ulist" id="us">
<tr>
<td><s:property value="#us.uid"/></td>
<td><s:property value="#us.uname"/></td>
<td><s:property value="#us.uage"/></td>
<td><s:property value="#us.uphone"/></td>
<td><a href="">添加</a></td>
</tr>
</s:iterator>
</table>
</s:form>
</body>
</html>
两个tr都输出不来值~求教高手帮解答一下 展开
4个回答
展开全部
如果你非要把struts2和struts1混用的话,上面的不可能实现,在action中 ,你只要声明Users 的get方法,并且在login方法中给us赋值,不需要像struts1那样放在request中,然后前台直接可以通过<property value="us.uid"/>这样就可以取到,第二个集合也是 ,声明List<Users>集合的属性并提供get,set 方法,然后<s:property value="uid"/>就可以,id="us"也可以不用
追问
页面:
添加
还是查不出来。。。。
追答
这么写
再一个 你的action里写的有问题
Users us=new Users();
for(int i=0;i<ulist.size();i++)
{
us=ulist.get(i);
}
这是什么意思,是取ulist中的最后一个Users对象吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
struts2框架会自动将Action中getXXX()形式的xxx属性放在ValueStack中,所以页面上通过struts2标签直接取属性即可.
<s:iterator value="request.ulist" id="us">改成<s:iterator value="ulist" id="us">即可
<s:iterator value="request.ulist" id="us">改成<s:iterator value="ulist" id="us">即可
追问
添加
改完后还是显示不出来
追答
1.检查Users类的属性是不是都有set,get方法
2.看看ulist属性是否有值
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1.你的us需要在上面通过自动注入的方式声明,然后添加setter、getter方法
private User user;
private User user;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
us没写get set方法 将us写成属性
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询