我想将下拉列表的选定值保存到数据库中。
首先加载index.jsp
。从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";
}
}
下拉列表实际上只是表单字段之一。表格中还有其他字段。 可以将月份字段以外的所有值输入数据库。对于月份字段,输入的值为空。
我认为没有采取下拉的价值。
答案 0 :(得分:1)
在index.jsp
中,只需使用重定向到注册页面的代码
<% response.sendRedirect("registerAction.action"); %>
register.jsp
中的表单通过表单registerAction
属性映射到action
。动作映射应该是
<action name="registerAction" class="action.RegisterAction" >
<result name="success">register.jsp</result>
</action>
<action name="register" class="action.RegisterAction" method="register">
<result name="input">register.jsp</result>
<result name="success">login.jsp</result>
</action>
此映射更改为操作类的register
方法,该方法在您的代码中用于插入一个新的,使用提供的months
值,该值应在执行操作之前填充。 input
结果用于JSP中的表单标记。 login.jsp
未知,但它将作为register
操作的结果使用。表格也应该重命名
<s:form action="register">
<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>
操作代码更改
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<Month> allMonths;
//public getter and setter of allMonths
假设动作类实现Preparable
,最好立即执行此操作,因为如果验证(如果有)失败,则可以使用该列表。
public void prepare() throws Exception {
//populate allMonths
//and set the value of months if you want it to be preselected.
}
并且操作应该扩展已经实施ActionSupport
方法的execute
。
答案 1 :(得分: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);
}
}
当然,您需要为months
此外,在您的操作中添加private Integer month
成员变量,该变量将保留用户所选的月份(同样,使用getter和setter)
然后,在JSP中使用以下s:select
标记:
<s:select label="Select Date of Month" name="month" headerKey="0"
headerValue="--Select--" list="months"/>
现在在execute
方法中,月成员变量应该保留选定的月份。 0应该没有选择,1应该是1月等等。