使用Hibernate将下拉列表值保存到Struts 2中的数据库

我想将下拉列表的选定值保存到数据库中。

首先加载index.jsp 。 从index.jsp ,当我们点击index.jsp注册URL时,我们可以去register.jsp

struts.xml

  register.jsp   login.jsp  

index.jsp

    Register 

register.jsp

      

动作类是:

 public class RegisterAction extends ActionSupport { String name, pwd, email, address, months; int phno; List allMonths = new ArrayList(); List users = new ArrayList(); UserDao udao = new UserDao(); public List getAllMonths() { return allMonths; } public void setAllMonths(List allMonths) { this.allMonths = allMonths; } public String getMonths() { return months; } public void setMonths(String months) { this.months = months; } public List getUsers() { return users; } public void setUsers(List 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"; } } 

下拉列表实际上只是表单字段之一。 表格中还有其他字段。 可以将月份字段以外的所有值输入数据库。 对于月份字段,输入的值为null。

我认为没有采取下拉的价值。

我建议将months成员变量更改为这样的Map

 private Map months = new HashMap(); 

然后,在您的操作中实现Preparable接口,并使用以下内容初始化地图:

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

当然,你需要在months添加一个getter和setter

此外,在您的操作中添加一个private Integer month成员变量,该变量将保留用户所选的月份(同样,使用getter和setter)

然后,在JSP中使用以下s:select标记:

  

所以现在在你的execute方法中,月成员变量应该保持选定的月份。 0应该没有选择,1应该是1月等。

index.jsp只需使用重定向到注册页面的代码即可

 <% response.sendRedirect("registerAction.action"); %> 

register.jsp ,表单通过表单action属性映射到registerAction 。 动作映射应该是

  register.jsp   register.jsp login.jsp  

此映射更改为操作类的register方法,该方法在您的代码中用于插入一个新的,用于提供的months值,该值应在执行操作之前填充。 input结果用于JSP中的表单标记。 login.jsp是未知的,但它将作为register操作的结果使用。 表格也应该重命名

     

行动的代码改变了

 private String months; //public getter and setter of months public String register() throws Exception { User u = new User(); u.setName(name); u.setEmail(email); u.setAddress(address); u.setPhno(phno); u.setPwd(pwd); System.out.println(months); u.setMonths(months); udao.addUser(u); return "success"; } private List allMonths; //public getter and setter of allMonths 

假设动作类实现Preparable ,最好一次在此处执行,因为如果validation(如果有的话)失败,则可以使用该列表。

 public void prepare() throws Exception { //populate allMonths //and set the value of months if you want it to be preselected. } 

并且操作应该扩展已经实现了execute方法的ActionSupport