日期:2014-05-16  浏览次数:20313 次

Struts2 <s:iterator ><s:if> <s:esle>标签使用和jstl标签循环

java代码

import java.io.Serializable;
@SuppressWarnings("all")
public class User implements Serializable{
?private int id;
?private String username;
?private String password;
?public int getId() {
??return id;
?}
?public void setId(int 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;
?}
}

?

Struts2 Action

java代码

public String list() throws Exception {
?? List list=new ArrayList();
?? User user=new User();
?? user.setPassword("admin123");
?? user.setUsername("张三");
?? list.add(user);
?? user=new User();
?? user.setPassword("admin123");
?? user.setUsername("李四");
?? list.add(user);
?? if(list!=null){
??? request.setAttribute("list", list);
??? return "userinfo";
?? }else{
??? return "input";
?? }
?}

jsp页面

?<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? <head>
???
??? <title>用户详情</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>
? <div align="center">
? jstl标签</br>
? ?<c:choose>
??? ?<c:when test="${empty list}">
???? ???没有数据!<br>
???? ?</c:when>
???? ?<c:otherwise>
???? ??<c:forEach items="${list}" var="ls" >
???? ?? 用户名:${ls["username"] }</br>
???? ??</c:forEach>
???? ?</c:otherwise>
???? </c:choose>
???? struts2标签</br>
?????? <s:iterator value="#request.list" var="ls" >
????????? <s:if test="#ls.username!=''"> <!--判断空串 -->
????? ?????????? <!--? 用户名:<s:property value="username"/></br>方法一 -->
??????? ??用户名:<s:property value="#ls.username"/><!--方法二 --></br>
???? ?? </s:if>
???? ?? <s:else>
???? ?? ?? 没有数据!<br>
???? ?? </s:else>
?????? </s:iterator>?
?
? </body>
</html>

?