我想将下拉列表的选定值保存到数据库中。
首先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";
}
}
下拉列表实际上只是表单字段之一。表格中还有其他字段。
除月份字段外的所有值都可以输入数据库。对于月份字段,输入的值为null
。
我认为下拉的价值没有被采用。