使用struts2和jsp显示ArrayList

发布于 2024-10-06 10:17:29 字数 527 浏览 2 评论 0原文

我正在尝试学习struts2,所以这是一个非常基本的问题。

我有一个页面 input_database.jsp 及其对应的类 input_database.java

在类文件中我有一个带有变元和访问器的字符串数组列表。我想将它显示在我的 .jsp 文件中,

我一直在尝试使用 a 来做到这一点,但我认为我正在做一些根本错误的事情。

这是我一直尝试在 jsp 文件中使用的代码。 arraylist 是一个名为 query_data 的私有字符串列表。我的最终目标是显示字符串数组列表的数组列表来显示我的 select 语句,但我需要首先找出一些简单的字符串。 如果有人知道我做错了什么,或者可以向我指出我忽略的教程,那就太棒了,

谢谢

<s:iterator value="query_data" id="something">

            <s:property value="something"/><br />

</s:iterator>

I am trying to learn struts2, so this is a pretty basic question.

I have a page input_database.jsp, and its corresponding class input_database.java

in the class file I have an arraylist of strings with a mutator and an accessor. I want to display it inside my .jsp file

I've been trying to use a to do it, but I think I'm doing something fundamentally wrong.

here's the code I've been trying to use in the jsp file. the arraylist is a private list of strings called query_data. my ultimate goal is to display an arraylist of arraylists of strings to display my select statement, but I need to figure out some simple strings first.
If anyone knows what I'm doing wrong, or can point me to a tutorial that I've overlooked that'd be awesome

thanks

<s:iterator value="query_data" id="something">

            <s:property value="something"/><br />

</s:iterator>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小草泠泠 2024-10-13 10:17:29

Display.java

import java.sql.Date;
import java.util.ArrayList;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class Display extends ActionSupport{

    private static final long serialVersionUID = 1L;    
    List<PhoneBean> list = null;

    public List<PhoneBean> getList() {
        return list;
    }
    public void setList(List<PhoneBean> list) {
        this.list = list;
    }

    public String execute() throws Exception{
        list = new ArrayList<PhoneBean>();

        PhoneBean bean = new PhoneBean();
        bean.setName("juan dela cruz");
        bean.setAge(17);
        bean.setBirthDate(Date.valueOf("1987-1-1"));
        bean.setContactNumber("12345");
        list.add(bean);

        bean = new PhoneBean();
        bean.setName("john cruise");
        bean.setAge(14);
        bean.setBirthDate(Date.valueOf("1988-2-2"));
        bean.setContactNumber("67890");
        list.add(bean);

        return SUCCESS;
    }

}

PhoneBean.java

import java.sql.Date;
public class PhoneBean {
    private String name = null;
    private int age = 0;
    private Date birthDate = null;
    private String contactNumber = null;

    public String getContactNumber() {
        return contactNumber;
    }
    public void setContactNumber(String contactNumber) {
        this.contactNumber = contactNumber;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Date getBirthDate() {
        return birthDate;
    }
    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }
}

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="default" extends="struts-default">
        <action name="Display" class="phoneBook.Display">
             <result>/display.jsp</result>
        </action>
    </package>
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

display.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:iterator status="stat" value="list">
<s:property value="name"/>     <s:property value="age"/>  
<s:property value="birthDate"/>  <s:property value="contactNumber"/>
</s:iterator>
</body>
</html>

Display.java

import java.sql.Date;
import java.util.ArrayList;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class Display extends ActionSupport{

    private static final long serialVersionUID = 1L;    
    List<PhoneBean> list = null;

    public List<PhoneBean> getList() {
        return list;
    }
    public void setList(List<PhoneBean> list) {
        this.list = list;
    }

    public String execute() throws Exception{
        list = new ArrayList<PhoneBean>();

        PhoneBean bean = new PhoneBean();
        bean.setName("juan dela cruz");
        bean.setAge(17);
        bean.setBirthDate(Date.valueOf("1987-1-1"));
        bean.setContactNumber("12345");
        list.add(bean);

        bean = new PhoneBean();
        bean.setName("john cruise");
        bean.setAge(14);
        bean.setBirthDate(Date.valueOf("1988-2-2"));
        bean.setContactNumber("67890");
        list.add(bean);

        return SUCCESS;
    }

}

PhoneBean.java

import java.sql.Date;
public class PhoneBean {
    private String name = null;
    private int age = 0;
    private Date birthDate = null;
    private String contactNumber = null;

    public String getContactNumber() {
        return contactNumber;
    }
    public void setContactNumber(String contactNumber) {
        this.contactNumber = contactNumber;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Date getBirthDate() {
        return birthDate;
    }
    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }
}

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="default" extends="struts-default">
        <action name="Display" class="phoneBook.Display">
             <result>/display.jsp</result>
        </action>
    </package>
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

display.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:iterator status="stat" value="list">
<s:property value="name"/>     <s:property value="age"/>  
<s:property value="birthDate"/>  <s:property value="contactNumber"/>
</s:iterator>
</body>
</html>
祁梦 2024-10-13 10:17:29

在属性标签中给出您要显示的列表属性。
假设您在操作类 List中有一个归档; userNames 那么你可以通过以下方式使用它。其中 User 类有一个属性 userName。

    <s:iterator var="i" step="1" value="userNames">
        <s:property value="userName" ></s:property>
    </s:iterator>

或者
如果它是一个简单的 arrayList 那么你可以使用如下

List<Integer> integers;

    <s:iterator var="i" step="1" value="integers">
        <s:property></s:property>
    </s:iterator>

In property tag give the list attribute which you want to display.
suppose you have a filed in action class List<User> userNames then you can use it in the following manner. where User class has a property userName.

    <s:iterator var="i" step="1" value="userNames">
        <s:property value="userName" ></s:property>
    </s:iterator>

or
if it is a simple arrayList then you can use as follows

List<Integer> integers;

    <s:iterator var="i" step="1" value="integers">
        <s:property></s:property>
    </s:iterator>
第几種人 2024-10-13 10:17:29
 <s:iterator  var="i" step="1" value="arraylistName">
 <s:property value="fieldName">
 </s:iterator>

ArrayList“arrayListName”应该在struts操作中有setter和getter。
ArrayList 是某个对象,fieldName 是对象包含的属性名称。就像 Car 是对象,speed 是它的属性。

 <s:iterator  var="i" step="1" value="arraylistName">
 <s:property value="fieldName">
 </s:iterator>

ArrayList "arrayListName" should have setter and getter in struts action.
ArrayList would be of some object ,fieldName is the name of the attribute contain by object.Like Car is the object and speed is its attribute.

风情万种。 2024-10-13 10:17:29

如果你想在 jsp 页面中使用列表,那么你的迭代器但在此之前检查你的列表不为空......

<s:iterator value="customerList" status="userStatus">

    <tr>
        <td><s:property value="customerId" /></td>   // Value is Y'r POJO Field
        <td><s:property value="name" /></td>
        <td><s:property value="address" /></td>
    </tr>

</s:iterator>

if you want to use a list in you'r jsp page then your iterator but before that check your list is not empty....

<s:iterator value="customerList" status="userStatus">

    <tr>
        <td><s:property value="customerId" /></td>   // Value is Y'r POJO Field
        <td><s:property value="name" /></td>
        <td><s:property value="address" /></td>
    </tr>

</s:iterator>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文