Extra row appear in my jsp page after the Submit button is pressed

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Extra row appear in my jsp page after the Submit button is pressed

albert kao-2
I am debugging my test program.
When my test page http://localhost:8080/Struts2Example/updatePerson
appeared in the browser, it displayed
A
B
C
in its own textfield and a Submit button, which is expected.

However, after I edited the textfields and pressed the Submit button.
The following appeared in the browser.
Name :A
Name :Aa
Name :Bb
Name :Cc

I do not understand why an extra row (Name :A) appeared in the top with the
rest of the names which I just entered.

The following source codes is my test program.
updatePerson.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>Update Person</title>
    </head>
    <body>
      <s:form action="changePerson">
         <s:iterator value="persons" status="stat" var="person">
              <s:textfield value="%{#person.name}"
name="persons[%{#stat.count}].name"/><br/>
         </s:iterator>
         <s:submit value="Submit"/>
      </s:form>
    </body>
</html>
PersonAction


updatePersonResult.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>Update Person Result</title>
    </head>
    <body>
         <s:iterator value="persons" status="stat" var="person">
            <b>Name :</b><s:property value="%{#person.name}" /><br>
         </s:iterator>
    </body>
</html>


Person.java:
public class Person {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}



PersonAction.java:
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class PersonAction extends ActionSupport {
    private List<Person> persons;

    public PersonAction () {
        persons = new ArrayList<Person>();

        int alpha = 65;
        for (int i = 0; i < 3 ; i++) {
            Person person = new Person();
            person.setId(i);
            person.setName(String.valueOf((char)alpha++));
            persons.add(person);
        }
    }

    public List<Person> getPersons() {
        return persons;
    }

    public void setPersons(List<Person> persons) {
        this.persons = persons;
    }

    //Initial Load method
    @Override
    public String execute() {
        return SUCCESS;
    }

    //Function that handles the form submit
    public String updatePerson() {

        for(Person person : persons) {
            System.out.println(person.getId() + ":" + person.getName());
        }

        return SUCCESS;
    }
}




struts.xml:
<action name="updatePerson" class="com.common.action.PersonAction">
  <result>pages/updatePerson.jsp</result>
</action>

<action name="changePerson" class="com.common.action.PersonAction">
  <result name="success">pages/updatePersonResult.jsp</result>
</action>
Reply | Threaded
Open this post in threaded view
|

Re: Extra row appear in my jsp page after the Submit button is pressed

Yasser Zamani-2


On 3/12/2018 6:03 AM, albert kao wrote:
>               <s:textfield value="%{#person.name}"
> name="persons[%{#stat.count}].name"/><br/>

I guess using #stat.index (instead of #stat.count) solves this issue.
Count will be 1,2 and 3 but Index will be 0,1 and 2 which is same as
that index that java uses.

Regards.

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Extra row appear in my jsp page after the Submit button is pressed

albert kao-2
Problem is fixed.
Thanks!

On Mon, Mar 12, 2018 at 1:36 AM, Yasser Zamani <[hidden email]>
wrote:

>
>
> On 3/12/2018 6:03 AM, albert kao wrote:
> >               <s:textfield value="%{#person.name}"
> > name="persons[%{#stat.count}].name"/><br/>
>
> I guess using #stat.index (instead of #stat.count) solves this issue.
> Count will be 1,2 and 3 but Index will be 0,1 and 2 which is same as
> that index that java uses.
>
> Regards.
>