Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用struts2和jsp显示ArrayList_Java_Struts2_Struts - Fatal编程技术网

Java 使用struts2和jsp显示ArrayList

Java 使用struts2和jsp显示ArrayList,java,struts2,struts,Java,Struts2,Struts,我正在努力学习struts2,所以这是一个非常基本的问题 我有一个页面input_database.jsp,和它对应的类input_database.java 在类文件中,我有一个带有一个mutator和一个accessor的字符串数组列表。我想在.jsp文件中显示它 我一直试图用a来做这件事,但我认为我做了一些根本错误的事情 下面是我一直试图在jsp文件中使用的代码。arraylist是一个名为query\u data的字符串的私有列表。我的最终目标是显示一个由字符串组成的arraylist来

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

我有一个页面input_database.jsp,和它对应的类input_database.java

在类文件中,我有一个带有一个mutator和一个accessor的字符串数组列表。我想在.jsp文件中显示它

我一直试图用a来做这件事,但我认为我做了一些根本错误的事情

下面是我一直试图在jsp文件中使用的代码。arraylist是一个名为query\u data的字符串的私有列表。我的最终目标是显示一个由字符串组成的arraylist来显示我的select语句,但我需要先找出一些简单的字符串。 如果有人知道我做错了什么,或者可以告诉我一个我忽略的教程,那就太棒了

谢谢

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

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

</s:iterator>



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;
    }

}
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>
<?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
web.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>
<?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>

支柱2
org.apache.struts2.dispatcher.FilterDispatcher
支柱2
/*
30
index.jsp
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;
    }

}
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>
<?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
web.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>
<?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>

支柱2
org.apache.struts2.dispatcher.FilterDispatcher
支柱2
/*
30
index.jsp
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>

在此处插入标题

如果您想在您的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>

//值为Y'r POJO字段

如果您想在您的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>

//值为Y'r POJO字段
是一个工作示例(Netbeans 6.9项目),说明了如何迭代数组或对象列表

另外,如何提交表单,以便在提交时重新创建对象列表

只需解析引用就可以开始了。

是一个工作示例(Netbeans 6.9项目),说明了如何迭代数组或对象列表

另外,如何提交表单,以便在提交时重新创建对象列表


只需解析引用并开始操作。

在属性标记中给出要显示的列表属性。 假设您有一个Field in action类
列出用户名
,那么您可以按以下方式使用它。其中用户类具有属性用户名

    <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>
列出整数;

在属性标记中,给出要显示的列表属性。 假设您有一个Field in action类
列出用户名
,那么您可以按以下方式使用它。其中用户类具有属性用户名

    <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>
列出整数;

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


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

我无法实现上述功能。不过,我确实找到了解决办法。使用内联java,我认为称为JavaBeans,通过一个单独的数据库类使其工作。不过,我确实找到了解决办法。使用内联java,我认为称为java beans,通过一个单独的数据库类使其工作。嘿,Sumit,欢迎使用Stackoverflow:)如果你能在答案中添加更多细节,那就太好了,这样,阅读内容和知识较少的人也能理解它,并对将来的人有用。嘿,Sumit,欢迎使用Stackoverflow:)如果您可以在答案中添加更多细节,这样阅读内容和知识较少的人也可以理解它,并且对将来的人也有帮助,那就太好了。此解决方案很有效。我在JSP中不可见的方法中启动了ArrayList。在全局启动和getter/setter之后,它工作正常。谢谢:)这个解决方案有效。我在JSP中不可见的方法中启动了ArrayList。在全局启动和getter/setter之后,它工作正常。谢谢:)