This query seems to insert null for Module_Code instead of value in dropdown choice
I'm new to wicket. I have a table called students2modules with 2 fields: Stud_Username,Module_Code which links students to modules taken. I want to register a student as taking a particular module. When the user selects a student, only the modules that student is not yet registered for should come up:
package myapp.project;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
public class AddStud2Mod extends BasePage {
private List students =new ArrayList();
private List modules=new ArrayList();
private ModalWindow message;
private InformationPanel panel;
private DropDownChoice studChoice;
private DropDownChoice moduleChoice;
public AddStud2Mod(){
super();
Form stud2mod = new Form("addstud2mod");
add(stud2mod);
try{
DB db=new DB();
String query="select Stud_Username from students;";
System.out.println(query);
db.connect();
ResultSet rs=db.execSQL(query);
while(rs.next()){
String studentUname=rs.getString("Stud_Username");
students.add(studentUname);
}
db.close();
}
catch(Exception e){
e.printStackTrace();
}
studChoice = new DropDownChoice("studentChoice",new Model(),students);
moduleChoice = new DropDownChoice("modChoice",new Model(),modules);
studChoice.add(new AjaxFormComponentUpdatingBehavior("onchange")
{
@Override
protected void onUpdate(AjaxRequestTarget target)
{
try{
DB db=new DB();
db.connect();
String query="select distinct Module_Code from modules where Module_Code NOT IN(Select Module_Code from students2modules where Stud_Username="+"'"+getUserName()+"');";
System.out.println(query);
ResultSet rs=db.execSQL(query);
while(rs.next()){
String moduleNotRegistered =rs.getString("Module_Code");
modules.add(moduleNotRegistered);
}
moduleChoice.setChoices(modules);
}
catch(Exception e){
e.printStackTrace();
}
target.addComponent(moduleChoice);
}
});
final FeedbackPanel feedback=new FeedbackPanel("msgs");
feedback.setOutputMarkupId(true);
add(feedback);
final InvalidInputIndicator codeLabel = new InvalidInputIndicator("codeLabel",moduleChoice);
codeLabel.setOutputMarkupId(true);
stud2mod.add(codeLabel);
final InvalidInputIndicator studLabel = new InvalidInputIndicator("usernameLabel",studChoice);
studLabel.setOutputMarkupId(true);
stud2mod.add(studLabel);
AjaxButton ok=new AjaxButton("confirm"){
public void onSubmit(AjaxRequestTarget target,Form form){
try{
DB db=new DB();
db.connect();
String query= "Insert into students2modules Values('"+getUserName()+"'"+",'"+moduleChoice.getDefaultModelObjectAsString()+"')";
System.out.println(query);
db.updateSQL(query);
modules.clear();
db.close();
}
catch(Exception e){
e.printStackTrace();
}
Inf开发者_如何转开发ormationPanel.add2ModuleMessage();
message.show(target);
}
protected void onError(AjaxRequestTarget target,Form form){
target.addComponent(feedback);
target.addComponent(studLabel);
target.addComponent(codeLabel);
}
};
stud2mod.add(ok);
Button cancel=new Button("cancel"){
public void onSubmit(){
AddStud2Mod lecturer=new AddStud2Mod();
setResponsePage(lecturer);
}
};
stud2mod.add(cancel);
studChoice.setRequired(false);
studChoice.setOutputMarkupId(true);
moduleChoice.setRequired(false);
moduleChoice.setOutputMarkupId(true);
stud2mod.add(moduleChoice);
stud2mod.add(studChoice);
message=new ModalWindow("InformationDialog");
add(message);
panel=new InformationPanel(message.getContentId(),message);
message.setContent(panel);
message.setCssClassName(ModalWindow.CSS_CLASS_BLUE);
message.setTitle("Important Information");
message.setResizable(false);
message.setInitialHeight(150);
message.setInitialWidth(150);
message.setWindowClosedCallback(new ModalWindow.WindowClosedCallback() {
public void onClose(AjaxRequestTarget target) {
AddStud2Mod as2mod =new AddStud2Mod();
setResponsePage(as2mod);
}
});
}
private String getUserName(){
return studChoice.getDefaultModelObjectAsString();
}
}
Here is the problem-the insert statement in the onSubmit- '' seems to get inserted for Module_Code, no matter what module code has been selected and I have no idea why. The student username is inserted ok but not the module code. Thank you so much for your help and sorry for the large chunks of code(included so you can read all my code for the class).
A few general remarks: you have UI logic (Ajax,..) and 'bussiness' logic ('get modules for student') all mixed up. Consider to separete them by introducing a 'service' layer. Also, i would NOT use the student name as you do in your data model. What if a student changes the name? Or a modul gets renamed? Use generated ID's instead.
The way you use jdbc opens your app to sql injections. Never ever build your queries concatinating user provided input. Use prepared statements for this (the query cache of your DB will like this too ;))
Your actual problem: You use ajax. With Ajax, the changes in the UI do not get pushed into the Model. You get the student name ok because you have an AjaxFormComponentUpdatingBehavior added to the DDC.
hope that helps
Edit: looking at your profile, i see that you have asked a few questions here, but never accepted an answer for any of these. I strongly suggest you do so, otherwise people will just ignore your questions.
精彩评论