S2SH框架下的页面始终获取不到list对象中的属性。JSP页面
<div><a href="${pageContext.request.contextPath}/form/userAction_toAdd.action">添加</a></div>
<div>
<s:if test="(list!=null)&&(!list.isEmpty())">
<div>用户列表</div>
<!-- 遍历用户 -->
<s:iterator var="a" value="#list">
<s:property value="#a.username"/>
<s:property value="#a.password"/>
</s:iterator>
</s:if>
<s:else>
<div>无用户</div>
</s:else>
</div>
Struts.xml
<package name="guoyuyuAction" extends="struts-default" namespace="/">
<interceptors>
<!--定义一个拦截器-->
<interceptor class="com.guoyuyu.interceptor.UserInterceptor" name="user" />
</interceptors>
<action name="query" class="com.guoyuyu.action.UserMGAction" method="query">
<result name="list">/page/main/userManageList.jsp</result>
<exception-mapping result="list" exception="exception.pageException"></exception-mapping>
</action>
</package>action文件:
package com.guoyuyu.action;import java.util.List;import javax.servlet.http.HttpServletRequest;import org.springframework.web.HttpRequestHandler;import net.sourceforge.jtds.jdbc.RequestStream;import com.guoyuyu.exception.pageException;
import com.guoyuyu.manage.userMGManager;
import com.guoyuyu.util.StudentUtil;
import com.guoyuyu.vo.StudentUser;
import com.opensymphony.xwork2.ModelDriven;public class UserMGAction implements ModelDriven<StudentUser>{
private List<StudentUser> list ;
public List<StudentUser> getList() {
return list;
}
public void setList(List<StudentUser> list) {
this.list = list;
}
StudentUser user = new StudentUser();
userMGManager userMGManager = new userMGManager();
public StudentUser getModel() {
return user;
}
public String query() throws Exception{
if(!StudentUtil.stringNull(user.getUsername())){
throw new pageException("学号不能为空");
}
if(!(user.getUsername().length()==11 && StudentUtil.isNum(user.getUsername()))){
throw new pageException("学号只能为11位数字");
}
try{
list = userMGManager.list(user.getUsername());
return "list";
}catch(Exception e){
e.printStackTrace();
throw new pageException(e.getMessage());
}
}
}
<div><a href="${pageContext.request.contextPath}/form/userAction_toAdd.action">添加</a></div>
<div>
<s:if test="(list!=null)&&(!list.isEmpty())">
<div>用户列表</div>
<!-- 遍历用户 -->
<s:iterator var="a" value="#list">
<s:property value="#a.username"/>
<s:property value="#a.password"/>
</s:iterator>
</s:if>
<s:else>
<div>无用户</div>
</s:else>
</div>
Struts.xml
<package name="guoyuyuAction" extends="struts-default" namespace="/">
<interceptors>
<!--定义一个拦截器-->
<interceptor class="com.guoyuyu.interceptor.UserInterceptor" name="user" />
</interceptors>
<action name="query" class="com.guoyuyu.action.UserMGAction" method="query">
<result name="list">/page/main/userManageList.jsp</result>
<exception-mapping result="list" exception="exception.pageException"></exception-mapping>
</action>
</package>action文件:
package com.guoyuyu.action;import java.util.List;import javax.servlet.http.HttpServletRequest;import org.springframework.web.HttpRequestHandler;import net.sourceforge.jtds.jdbc.RequestStream;import com.guoyuyu.exception.pageException;
import com.guoyuyu.manage.userMGManager;
import com.guoyuyu.util.StudentUtil;
import com.guoyuyu.vo.StudentUser;
import com.opensymphony.xwork2.ModelDriven;public class UserMGAction implements ModelDriven<StudentUser>{
private List<StudentUser> list ;
public List<StudentUser> getList() {
return list;
}
public void setList(List<StudentUser> list) {
this.list = list;
}
StudentUser user = new StudentUser();
userMGManager userMGManager = new userMGManager();
public StudentUser getModel() {
return user;
}
public String query() throws Exception{
if(!StudentUtil.stringNull(user.getUsername())){
throw new pageException("学号不能为空");
}
if(!(user.getUsername().length()==11 && StudentUtil.isNum(user.getUsername()))){
throw new pageException("学号只能为11位数字");
}
try{
list = userMGManager.list(user.getUsername());
return "list";
}catch(Exception e){
e.printStackTrace();
throw new pageException(e.getMessage());
}
}
}
debug过了 和后台的list.toString出来了是同一个 但是页面就是去不出来值 #号去过也试过了 还是不行啊
在action的方法里把 list放到request里
ServletActionContext.getRequest().setAttribute("list",list);
页面获取
<s:iterator value="#request.list">
<s:property value="username"/>
<s:property value="password"/>
</s:iterator>
list 的值看的是有值的 前台 <s:debug>出来的list和后台list的toString方法看的是同一个的
list = userMGManager.list(user.getUsername());
return "list";
}catch(Exception e){
e.printStackTrace();
throw new pageException(e.getMessage());
}
这个list方法是什么意思??
list源码:public List<StudentUser> list(String username) throws pageException {
String sql = "SELECT * FROM STUDENT_USER S WHERE S.USERNAME='" + username +"'";
Session session = cu.getSession();
List<StudentUser> list = new ArrayList();
try {
list = session.createSQLQuery(sql).list();
return list;
} catch (Exception e) {
e.printStackTrace();
throw new pageException("数据库执行错误,请联系管理员!");
} finally {
session.close();
}
}list也能接受到数据查询出来的值
ServletActionContext.getRequest().setAttribute("list",list);
页面获取
<s:iterator value="#request.list">
<s:property value="username"/>
<s:property value="password"/>
</s:iterator> 这样还有问题吗?