我想將下拉列表的選定值保存到數據庫中。將下拉列表值保存到帶有Hibernate的Struts 2中的數據庫
首先加載了index.jsp
。從index.jsp
,點擊登錄URL index.jsp
的話可以去register.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類是:
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";
}
}
下拉列表其實只是形式的領域之一。還有其他領域的形式。 除月份字段以外的所有值都可以輸入到數據庫中。對於月份字段,輸入的值爲空。
我認爲下拉的價值沒有被採取。
什麼的日誌? User.months的映射是什麼?它是否正確地在'println'中打印了值?有沒有驗證? –
沒有驗證。正在打印控制檯中的nthng。我已經如上所述更改了代碼。上面的代碼值正在輸入到數據庫中,但月份的值爲空。 –
在行動中是否有一個公共二傳手?您是否已驗證請求中是否發送了「月」值,例如在您的JS /網絡控制檯中?不顯示與所述的System.out.println沿被印刷在console..But個月的值提到 –