Struts2 控制标签(三) 输出List中String字符串的内容

1.新建Action类
package com.wl.control.tags;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class ShowListString extends ActionSupport {

    private List listString=new ArrayList();
    
    public List getListString() {
        return listString;
    }

    public void setListString(List listString) {
        this.listString = listString;
    }

    @Override
    public String execute() throws Exception {

        listString.add("张三");
        listString.add("李四");
        listString.add("王五");
        return "showliststring";
    }

}

2.新建JSP文件
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
  <body>
     <s:iterator value="listString" var="studentName">
         <s:property value="studentName"/><br>
     </s:iterator>
  </body>
</html>

3.修改struts.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
   <package name="Struts2.1" extends="struts-default">
       <action name="showliststring" class="com.wl.control.tags.ShowListString">
           <result name="showliststring">/controlTags/showliststring.jsp</result>
       </action>
   </package>
</struts>

4.运行结果:

Struts2 控制标签(三) 输出List中String字符串的内容
引用

你可能感兴趣的:(java,apache,jsp,xml,struts)