Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.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
Struts2动态添加、删除页面中的对象列表_Struts2 - Fatal编程技术网

Struts2动态添加、删除页面中的对象列表

Struts2动态添加、删除页面中的对象列表,struts2,Struts2,我试图在Struts2上构建一个页面,向数据库表中添加值。问题是,页面必须允许用户向数据库表输入几行。当用户单击submit时,它必须向数据库表读写行。用户可以在页面中添加或删除行 所以我尝试在页面上呈现列表值。Java代码如下所示: List<Testimate> estimates; private String removeIndex; public String execute() throws Exception { estimates = new ArrayLi

我试图在Struts2上构建一个页面,向数据库表中添加值。问题是,页面必须允许用户向数据库表输入几行。当用户单击submit时,它必须向数据库表读写行。用户可以在页面中添加或删除行

所以我尝试在页面上呈现列表值。Java代码如下所示:

List<Testimate> estimates;
private String removeIndex;

public String execute() throws Exception {
    estimates = new ArrayList<Testimate>();
    for(int i = 0; i < INITIAL_ESTIMATES; i++)
        estimates.add(new Testimate());
    return INPUT;
}
public String add() {
    estimates.add(new Testimate());
    System.out.println(estimates.size());
    return INPUT;
}

public String remove() {
    estimates.remove(Integer.parseInt(getRemoveIndex()));
    System.out.println(estimates.size() + " " + getRemoveIndex());


    return INPUT;
    }   

setRemoveIndex()
{    
$('input[name=“removeIndex”]').val(removeIndex);
返回true;
}
当我单击“添加估算”时,它会将元素添加到“估算”列表中。它能正确打印尺寸。但当我单击“删除此估算”时,它不会更改“估算”列表。但它显示列表的大小减少了一个。当我再次单击时,大小根本没有改变。它没有得到任何修改

你能告诉我这个密码有什么问题吗。我可能对这个框架的工作原理有很大的误解。如果您有任何问题或澄清,请直接提问

更新:

我用JSP上的以下行解决了我的问题。但问题仍然存在,为什么我不能在我的行动中这样做

<s:iterator value="estimates" var="estimate" status="status">
   <s:if test="#status.index != removeIndex">

我试过你的代码,它对我来说很好。虽然我不了解你的
Testimate
类的内部内容,所以代替
列表评估
我选择了
列表评估。这是我的工作代码

动作类
列出估计值;
私人内部远程索引;
//有接球手和二传手
公共字符串execute()引发异常
{
估计值=新的ArrayList();
对于(int i=0;i<5;i++){
加上(“+i”);
}
回归成功;
}
公共字符串删除(){
删除(getRemoveIndex());
System.out.println(estimates.size()+“”+getRemoveIndex());
回归成功;
}
JSP

函数setRemoveIndex(val)
{    
警报(val);
document.getElementById(“removeIndex”).value=val;
//$('input[name=“removeIndex”]').val(removeIndex);
document.myform.action=“remove.action”;
document.myform.submit();
返回true;
}
Struts.xml

/示例/HelloWorld.jsp

上面的代码完美地删除了列表值,我也检查了页面刷新,并且没有返回值。

请参阅下面的代码,该代码按预期对我有效:

行动类

package com.mycompany;

import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author james
 */
public class EstimateAction extends ActionSupport {

    private List<Testimate> estimates;
    private String removeIndex;
    private static final int INITIAL_ESTIMATES = 10;

    public List<Testimate> getEstimates() {
        return estimates;
    }

    public void setEstimates(List<Testimate> estimates) {
        this.estimates = estimates;
    }

    public String getRemoveIndex() {
        return removeIndex;
    }

    public void setRemoveIndex(String removeIndex) {
        this.removeIndex = removeIndex;
    }

    public String execute() throws Exception {
        estimates = new ArrayList<Testimate>();
        for (int i = 0; i < INITIAL_ESTIMATES; i++) {
            estimates.add(new Testimate());
        }
        return INPUT;
    }

    public String add() {
        estimates.add(new Testimate());
        System.out.println(estimates.size());
        return INPUT;
    }

    public String remove() {
        estimates.remove(Integer.parseInt(getRemoveIndex()));
        System.out.println(estimates.size() + " " + getRemoveIndex());


        return INPUT;
    }
}
package.com.mycompany;
导入com.opensymphony.xwork2.ActionSupport;
导入java.util.ArrayList;
导入java.util.List;
/**
*
*@作者詹姆斯
*/
公共类EstimateAction扩展了ActionSupport{
私人名单估计数;
私有字符串删除索引;
私人静态最终int初始估计=10;
公开名单{
回报估计;
}
公共预算(列出预算){
这个。估计数=估计数;
}
公共字符串getRemoveIndex(){
返回远程索引;
}
公共void setRemoveIndex(字符串removeIndex){
this.removeIndex=removeIndex;
}
公共字符串execute()引发异常{
估计值=新的ArrayList();
对于(int i=0;i<初始估计;i++){
添加(新Testimate());
}
返回输入;
}
公共字符串add(){
添加(新Testimate());
System.out.println(estimates.size());
返回输入;
}
公共字符串删除(){
remove(Integer.parseInt(getRemoveIndex());
System.out.println(estimates.size()+“”+getRemoveIndex());
返回输入;
}
}
struts.xml

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

<struts>
    <!-- Configuration for the default package. -->
    <constant name="struts.devMode" value="true" />
    <package name="default" extends="struts-default">
        <action name="showEstimates" class="com.mycompany.EstimateAction">
            <result name="input">/estimatePage.jsp</result>
        </action>
        <action name="removeEstimates" class="com.mycompany.EstimateAction" method="remove">
            <result name="input">/estimatePage.jsp</result>
        </action>
    </package>
</struts>

/estimatePage.jsp
/estimatePage.jsp
estimatePage.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
        <script type="text/javascript">
            function setRemoveIndex(removeIndex)
            {
                document.myForm.action = "removeEstimates.action";    //First set the form to navigate to the specific action
                $('input[name="removeIndex"]').val(removeIndex);
                //alert("Here");
                document.myForm.submit();                             //Submit the form with the action attribute change
                return false;                                         //Cancel the form submission which was triggered earlier
            }
        </script>
    </head>
    <body>
        <s:form name="myForm" onsubmit="setRemoveIndex()">
            <s:submit action="add" cssClass="ButtonSmall" value="Add estimate" />
            <s:hidden name="removeIndex"/>
            <table>
                <s:iterator value="estimates" var="estimate" status="status">
                    <tr>
                        <td><s:textfield name="estimates[%{#status.index}].name"cssClass="product" /></td>
                        <td><s:textfield name="estimates[%{#status.index}].price"cssClass="product" /></td>
                        <td><s:textfield name="estimates[%{#status.index}].date"cssClass="product" /></td>
                        <td><s:textfield name="estimates[%{#status.index}].attr"cssClass="product" /></td>
                        <td><s:submit action="remove" cssClass="ButtonSmall" value="Remove this estimate" onclick="return setRemoveIndex(%{#status.index})"/>
                        </td>
                    </tr>
                </s:iterator>
            </table>
        </s:form>
    </body>
</html>

JSP页面
函数setRemoveIndex(removeIndex)
{
document.myForm.action=“removeEstimates.action”;//首先设置表单以导航到特定操作
$('input[name=“removeIndex”]').val(removeIndex);
//警报(“此处”);
document.myForm.submit();//提交操作属性更改的表单
return false;//取消先前触发的表单提交
}

删除项目后,是否尝试在remove()中刷新Hibernate会话?我认为您的更改没有提交。

您能再详细描述一下吗
onclick=“removeIndex=%{#status.index}”/
为了找出单击了哪个提交,它将索引号保存到一个变量中。然后它会从列表中删除第n项你用调试器试过了吗?到底发生了什么事?我刚刚调试过它,由于没有hibernate类的任何源文件,所以找不到有用的东西。但是根据你的代码,它与hibernate没有任何关系,我在你的action类中看不到这样的调用,在这种情况下,很难告诉你任何事情。我的Testimate类是由Hibernate生成的模型类。我的代码看起来完全一样,只是列表类型是。你认为这会导致问题吗?@batbaatar我不认为如果你将POJO/Model从hibernate中分离出来,那么它肯定会产生问题。除了到数据库表的映射之外,我不知道它是如何连接到hibernate的。这个
<body>
  <script>
  function setRemoveIndex(val)
   {    
       alert(val);
       document.getElementById("removeIndex").value=val;
       //$('input[name="removeIndex"]').val(removeIndex);
       document.myform.action="remove.action";
       document.myform.submit();
        return true;
   }
</script>
<s:form theme="simple" id="myform" name="myform">
<s:submit action="CEST02_add" cssClass="ButtonSmall" value="Add estimate" />
<s:hidden name="removeIndex" id="removeIndex"/>
<table>
<s:iterator value="estimates" var="estimate" status="status">
<tr>
   <td><s:textfield name="estimates[%{#status.index}]"cssClass="product" /></td>
   <td>
   <s:submit action="remove" cssClass="ButtonSmall" value="Remove this estimate" onclick="return setRemoveIndex('%{#status.index}')"/>
   </td>
</tr>
</s:iterator>
</table>
</s:form>
</body>
 <action name="remove" class="example.HelloWorld" method="remove">
     <result>/example/HelloWorld.jsp</result>
 </action>
package com.mycompany;

import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author james
 */
public class EstimateAction extends ActionSupport {

    private List<Testimate> estimates;
    private String removeIndex;
    private static final int INITIAL_ESTIMATES = 10;

    public List<Testimate> getEstimates() {
        return estimates;
    }

    public void setEstimates(List<Testimate> estimates) {
        this.estimates = estimates;
    }

    public String getRemoveIndex() {
        return removeIndex;
    }

    public void setRemoveIndex(String removeIndex) {
        this.removeIndex = removeIndex;
    }

    public String execute() throws Exception {
        estimates = new ArrayList<Testimate>();
        for (int i = 0; i < INITIAL_ESTIMATES; i++) {
            estimates.add(new Testimate());
        }
        return INPUT;
    }

    public String add() {
        estimates.add(new Testimate());
        System.out.println(estimates.size());
        return INPUT;
    }

    public String remove() {
        estimates.remove(Integer.parseInt(getRemoveIndex()));
        System.out.println(estimates.size() + " " + getRemoveIndex());


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

<struts>
    <!-- Configuration for the default package. -->
    <constant name="struts.devMode" value="true" />
    <package name="default" extends="struts-default">
        <action name="showEstimates" class="com.mycompany.EstimateAction">
            <result name="input">/estimatePage.jsp</result>
        </action>
        <action name="removeEstimates" class="com.mycompany.EstimateAction" method="remove">
            <result name="input">/estimatePage.jsp</result>
        </action>
    </package>
</struts>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
        <script type="text/javascript">
            function setRemoveIndex(removeIndex)
            {
                document.myForm.action = "removeEstimates.action";    //First set the form to navigate to the specific action
                $('input[name="removeIndex"]').val(removeIndex);
                //alert("Here");
                document.myForm.submit();                             //Submit the form with the action attribute change
                return false;                                         //Cancel the form submission which was triggered earlier
            }
        </script>
    </head>
    <body>
        <s:form name="myForm" onsubmit="setRemoveIndex()">
            <s:submit action="add" cssClass="ButtonSmall" value="Add estimate" />
            <s:hidden name="removeIndex"/>
            <table>
                <s:iterator value="estimates" var="estimate" status="status">
                    <tr>
                        <td><s:textfield name="estimates[%{#status.index}].name"cssClass="product" /></td>
                        <td><s:textfield name="estimates[%{#status.index}].price"cssClass="product" /></td>
                        <td><s:textfield name="estimates[%{#status.index}].date"cssClass="product" /></td>
                        <td><s:textfield name="estimates[%{#status.index}].attr"cssClass="product" /></td>
                        <td><s:submit action="remove" cssClass="ButtonSmall" value="Remove this estimate" onclick="return setRemoveIndex(%{#status.index})"/>
                        </td>
                    </tr>
                </s:iterator>
            </table>
        </s:form>
    </body>
</html>