一尘不染

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

jsp

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

首先index.jsp被加载。单击的注册URL,从index.jsp可以转到。register.jsp``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>

动作类是:

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";
    }
}

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

我认为下拉菜单的价值并未得到体现。


阅读 262

收藏
2020-06-10

共1个答案

一尘不染

我建议将months成员变量更改为Map如下形式:

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

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

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

当然,您需要为添加一个getter和setter months

另外,private Integer month在您的操作中添加一个成员变量,该变量将保留用户选择的月份(同样,具有getter和setter方法)

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

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

因此,现在在您的execute方法中,month成员变量应保存所选的月份。0应该没有选择,1应该是一月,依此类推。

2020-06-10