Skip to content
Advertisement

Save drop-down list value to database in Struts 2 with Hibernate

I want to save the selected value of drop-down list into the database.

First index.jsp is loaded. From index.jsp, we can go to register.jsp when we click register URL of index.jsp.

struts.xml:

<action name="registerAction" class="action.RegisterAction" method="populateSelect">
        <result name="success" >register.jsp</result>
     
    </action>
    <action name="register" class="action.RegisterAction" method="execute">
        <result name="success" >login.jsp</result>

    </action>

index.jsp:

  <s:url id="url" action="registerAction">
</s:url>
  <s:a href="%{url}">Register</s:a>

register.jsp:

   <s:form action="registerAction" method="execute">
     <s:select label="Select Date of Month" key="Month List" name="months" headerKey="0" headerValue="--Select--" list="allMonths" listKey="id" listValue="name"/>
 <s:submit value="Register"/>
 </s:form>

Action class is:

public class RegisterAction extends ActionSupport {

    String name, pwd, email, address, months;

    int phno;

    
    List<Month> allMonths = new ArrayList<Month>();
    List<User> users = new ArrayList<User>();
    UserDao udao = new UserDao();


public List<Month> getAllMonths() {
    return allMonths;
}

public void setAllMonths(List<Month> allMonths) {
    this.allMonths = allMonths;
}

public String getMonths() {
    return months;
}

public void setMonths(String months) {
    this.months = months;
}

public List<User> getUsers() {
    return users;
}

public void setUsers(List<User> users) {
    this.users = users;
}


public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getName() {
    return name;
}

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

public int getPhno() {
    return phno;
}

public void setPhno(int phno) {
    this.phno = phno;
}

public String getPwd() {
    return pwd;
}

public void setPwd(String pwd) {
    this.pwd = pwd;
}

    public RegisterAction() {
    }
    

    public String execute() throws Exception {
        User u = new User();
        u.setName(name);
        u.setEmail(email);
        u.setAddress(address);
        u.setPhno(phno);
        u.setPwd(pwd);
        System.out.println("Hi der "+months);

        u.setMonths(months);
        udao.addUser(u);
        return "success";
    }

    public String listAllUsers() {
        users = udao.getUsers();
        System.out.println("In Action, " + users);
        return "success";
    }


    public String populateSelect() {
        allMonths = udao.getMonths();
        System.out.println("In constructor " + allMonths);
        return "success";
    }
}

the drop down list is actually only one of the form fields. there are other fields also in the form.

All values other than month field can be entered into database. for month field, the value being entered is null.

I think the value of drop-down is not being taken.

Advertisement

Answer

I would suggest to change the months member variable to a Map like this:

private Map<Integer, String> months = new HashMap<Integer, String>();

Then, implement the Preparable interface in you action, and initialize the map with something like this:

public void prepare() throws Exception {
    String[] monthNames = new DateFormatSymbols().getMonths();
    int i = 1;
    for (String monthName : monthNames) {
        months.put(i++, monthName);
    }
}

Of course, you need to add a getter and a setter for months

Also, add an private Integer month member variable in your action, that will hold the selected month by the user (again, with getters and setters)

Then, use the following s:select tag in your JSP:

<s:select label="Select Date of Month" name="month" headerKey="0"
          headerValue="--Select--" list="months"/>

So now in your execute method, the month member variable should hold the selected month. 0 should be no selection, 1 should be January, etc.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement