Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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 从jsp到action类获取列表对象值_Java_Jsp_Struts2 - Fatal编程技术网

Java 从jsp到action类获取列表对象值

Java 从jsp到action类获取列表对象值,java,jsp,struts2,Java,Jsp,Struts2,在JSP中迭代列表对象,其值来自正在正确显示的ViewAction类 以下是jps代码 java和Bean类代码如下 在action类列表中,对象名称为beanList 公共类ViewCheckboxAction扩展了ActionSupport{ HttpServletRequest=ServletActionContext.getRequest(); String viewData=“按rollno从学生订单中选择*”; 列表beanList; 公共列表getBeanList(){ 返回b

在JSP中迭代列表对象,其值来自正在正确显示的ViewAction类

以下是jps代码


java和Bean类代码如下

在action类列表中,对象名称为beanList

公共类ViewCheckboxAction扩展了ActionSupport{
HttpServletRequest=ServletActionContext.getRequest();
String viewData=“按rollno从学生订单中选择*”;
列表beanList;
公共列表getBeanList(){
返回beanList;
}  
公共无效列表(ArrayList beanList){
this.beanList=beanList;
}
公共字符串execute(){
beanList=新的ArrayList();
DbConnection db=新的DbConnection();
int计数器=0;
试一试{
语句st=db.getConnection().createStatement();
ResultSet res=st.executeQuery(视图数据);
while(res.next()){
计数器++;
Bean=新Bean(res.getInt(1),
res.getString(2),
res.getString(3));
添加(res.getString(“rollno”);
添加(bean);
}
}捕获(SQLE异常){
e、 printStackTrace();
}捕获(例外e){
e、 printStackTrace();
}最后{
试试{
db.removeConnection();
}捕获(SQLE异常){
e、 printStackTrace();
}
}
如果(计数器>0)
回归成功;
其他的
返回误差;
}   
}
豆子:

公共类Bean{
整数;
字符串名;
字符串位置;
公共Bean(int x、字符串y、字符串z){
rollnumber=x;
name=y;
位置=z;
}
接球手和接球手。。。
我需要从jsp到action的多个/单个更新表单字段值 类以执行更新的操作。但是列表(beanList) 值在action类中为Null。由于它为Null,我无法进行更新 活动 1) 在新的action类(EditAction.java)中,如何初始化list对象(beanList)? 这与我在ViewAction.java中声明的方法相同 2) Jsp系统是否正确?
请您在这方面提供帮助。提前感谢。

您必须使用类型转换,请在ViewCheckboxAction-Conversion.properties文件中提供以下配置:

KeyProperty_beanList=rollnumber
Element_beanList=Bean
CreateIfNull_beanList=true 
通过表单提交时,rollnumber用作beanList中Bean实例的KeyProperty。您可以对Key property字段使用任何其他属性。 name的值将设置为具有此特殊id的MyBean实例。 列表中没有为不可用的id值添加空值。这种方法避免了OutOfMemoryErrors的风险

<s:iterator value="beanList" id="bean">
    <tr> 
        <td>    
             <input type="checkbox" name="subCheckBox" />
        </td>   
        <td>                 
             <s:textfield name="beanList(%{bean.rollnumber}).rollnumber" value="%{rollnumber}" theme="simple"/>
        </td>
        <td>
            <s:textfield name="beanList(%{bean.rollnumber}).name" value="%{name}" theme="simple"/>
         </td>
         <td>
             <s:textfield name="beanList(%{bean.rollnumber}).location" value="%{location}" theme="simple"/>
          </td> 
   </tr>     
</s:iterator>


请参阅:

您必须使用类型转换,请在ViewCheckboxAction-Conversion.properties文件中提供以下配置:

KeyProperty_beanList=rollnumber
Element_beanList=Bean
CreateIfNull_beanList=true 
通过表单提交时,rollnumber用作beanList中Bean实例的KeyProperty。您可以对Key property字段使用任何其他属性。 name的值将设置为具有此特殊id的MyBean实例。 列表中没有为不可用的id值添加空值。这种方法避免了OutOfMemoryErrors的风险

<s:iterator value="beanList" id="bean">
    <tr> 
        <td>    
             <input type="checkbox" name="subCheckBox" />
        </td>   
        <td>                 
             <s:textfield name="beanList(%{bean.rollnumber}).rollnumber" value="%{rollnumber}" theme="simple"/>
        </td>
        <td>
            <s:textfield name="beanList(%{bean.rollnumber}).name" value="%{name}" theme="simple"/>
         </td>
         <td>
             <s:textfield name="beanList(%{bean.rollnumber}).location" value="%{location}" theme="simple"/>
          </td> 
   </tr>     
</s:iterator>


请参阅:

Bean
类添加一个默认无参数构造函数

默认的no-args构造函数是这样调用的,因为它是默认的:如果您没有指定any构造函数,它将自动创建

相反,如果您指定另一个构造函数,例如带有参数的构造函数,如,则不再自动创建无参数构造函数,并且如果需要,您可以显式声明它

Struts2需要无参数构造函数来创建bean


例如,您可以让一个bean的构造函数包含10个参数,并在JSP页面中只指定其中一个参数:Struts必须能够创建对象并设置单个字段(通过Setter)不考虑九个缺少的参数。

Bean
类中添加一个默认无参数构造函数

默认的no-args构造函数是这样调用的,因为它是默认的:如果您没有指定any构造函数,它将自动创建

相反,如果您指定另一个构造函数,例如带有参数的构造函数,如,则不再自动创建无参数构造函数,并且如果需要,您可以显式声明它

Struts2需要无参数构造函数来创建bean


例如,您可以让一个bean的构造函数包含10个参数,并在JSP页面中只指定其中一个参数:Struts必须能够创建对象并设置单个字段(通过Setter)不必关心丢失的九个参数。

非常感谢,它工作得很好:)为什么Bean类中不需要args构造函数?请帮助我。如果它工作了,你应该投票并接受答案。顺便说一句,我现在将进行编辑,让你知道发生了什么。哈,很好的捕获。在OP中甚至没有看到Bean类。谢谢@AleksandrM,nothing sp特别是TBH,我还是很感激