1

I want to set values for Set elements of login from JSP .

JSP page:

  <form action="Registered" method="post"> 
        <div class="form-group">
            <label>Company Name</label>
            <s:textfield name="name" value="%{name}" id="name"/>
        </div>
        <div class="form-group">
            <label>Address</label>
            <s:textarea name="address" value="%{address}" id="address"/>
        </div>
        <div class="form-group">
            <label>User Name</label>
            <s:textfield name="logins[0].userName" value="%{logins[0].userName}" id="userName"/>
        </div>
        <div class="form-group">
            <label>User Id</label>
            <s:textfield name="logins[0].userId" value="%{logins[0].userId}" id="userId"/>
        </div>
        <div class="form-group">
            <label>Mail Id</label>
            <s:textfield name="logins[0].userMailid" value="%{logins[0].userMailid}" id="userMailid"/>
        </div>

Pojo Classes:

public class Client  implements java.io.Serializable {
  private Set logins = new HashSet(0);
  //getter and setter
 }



 public class Login implements java.io.Serializable {

    private Long id;
    private Client client;
    private String userId;
    private String userName;
    private String userMailid;
 }

Action Class:

public String register() {
        Client cl = new Client();
        System.out.println(cl.getName() + " " + cl.getAddress());
  }

I want to set values of set in to my Action class for Client and Login.

How to do this?

Roman C
  • 49,761
  • 33
  • 66
  • 176
xrcwrn
  • 5,339
  • 17
  • 68
  • 129
  • you want to get value from textfield or you want set default value in text field? – Jimmysnn Jul 18 '14 at 11:27
  • User will fill form When form submitted it should assing in client and login fields – xrcwrn Jul 18 '14 at 11:29
  • See http://stackoverflow.com/q/23470075/1700321. – Aleksandr M Jul 18 '14 at 11:58
  • @AleksandrM http://stackoverflow.com/a/24191304/876739 Working for set. How can I do that with this code – xrcwrn Jul 18 '14 at 12:05
  • What is the requirement to use `Set`? A `List` could be a reasonable replacement. – Roman C Jul 18 '14 at 15:30
  • @RomanC While using Set it is better with Hibernate( I think). It create problem with list during fetch. If we use multiple list to fetch data it creates problem. So I would like to use Set insted of list – xrcwrn Jul 19 '14 at 03:26
  • @xrcwrn Do you have arguments against a `Set`? If you have some unknown problems with it explains nothing. You can elaborate on why you prefer sets over lists. – Roman C Jul 19 '14 at 06:51
  • @RomanC Ok Not good reasones but still i want to use `Set` because I want to use it. http://stackoverflow.com/a/24191304/876739 shows how to do that. I am not able to use according to my need. could you please tell me how can I modify my code so it should work – xrcwrn Jul 20 '14 at 10:28
  • @xrcwrn I have given you a ways to do that, see my [answer](http://stackoverflow.com/a/24849726/573032). – Roman C Jul 20 '14 at 10:58

2 Answers2

0

You can use a submit button like this:

 <form action="Registered" method="post"> 
            <div class="form-group">
                <label>Company Name</label>
                <s:textfield name="name" value="%{name}" id="name"/>
            </div>
            <div class="form-group">
                <label>Address</label>
                <s:textarea name="address" value="%{address}" id="address"/>
            </div>
            <div class="form-group">
                <label>User Name</label>
                <s:textfield name="logins[0].userName" value="%{logins[0].userName}" id="userName"/>
            </div>
            <div class="form-group">
                <label>User Id</label>
                <s:textfield name="logins[0].userId" value="%{logins[0].userId}" id="userId"/>
            </div>
            <div class="form-group">
                <label>Mail Id</label>
                <s:textfield name="logins[0].userMailid" value="%{logins[0].userMailid}" id="userMailid"/>
            </div>
          <input type="submit" value="Submit">
        </form>

EDIT

A nice tutorial

After make properties file like:

#Global messages
name= name
submit = Submit

I believe you has made Registered.jsp.

An you can run your project!

Jimmysnn
  • 583
  • 4
  • 8
  • 30
0

To bind fildes to Set you should put annotations on this property

public class Client  implements java.io.Serializable {

  @Element(value = Login.class)
  @Key(value = Long.class)
  @KeyProperty(value = "id") 
  @CreateIfNull(value = true)
  private Set logins = new HashSet(0);
  //getter and setter

  //now you need a property for login id, it should be initialized before JSP is populated
  private Long loginId;
  //getter and setter
}

now binding to the fields of the JSP change to

<s:textfield name="logins(%{loginId}).userName" id="userName"/>

the same for other fields that are bound to a set.

If you are using iterator tag to iterate through a set and you have an instance of Login pushed on top of the value stack then you can get its id instead of using loginId.

Roman C
  • 49,761
  • 33
  • 66
  • 176