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


i want save selected value of drop-down list database.

first index.jsp loaded. index.jsp, can go register.jsp when 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 1 of form fields. there other fields in form. values other month field can entered database. month field, value being entered null.

i think value of drop-down not being taken.

i suggest change months member variable map this:

private map<integer, string> months = new hashmap<integer, string>(); 

then, implement preparable interface in action, , initialize map this:

public void prepare() throws exception {     string[] monthnames = new dateformatsymbols().getmonths();     int = 1;     (string monthname : monthnames) {         months.put(i++, monthname);     } } 

of course, need add getter , setter months

also, add private integer month member variable in action, hold selected month user (again, getters , setters)

then, use following s:select tag in jsp:

<s:select label="select date of month" name="month" headerkey="0"           headervalue="--select--" list="months"/> 

so in execute method, month member variable should hold selected month. 0 should no selection, 1 should january, etc.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -