此查询似乎为 Module_Code 插入 null 而不是下拉选项中的值
我是检票口的新手。我有一个名为 Students2modules 的表,其中包含 2 个字段:Stud_Username、Module_Code,它将学生链接到所修读的模块。我想注册一名学生参加特定模块。当用户选择一个学生时,只有学生尚未注册的模块应该出现:
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();
}
InformationPanel.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();
}
}
问题是 - onSubmit 中的插入语句 - '' 似乎为 Module_Code 插入,无论选择了什么模块代码我不知道为什么。学生用户名已插入,但模块代码未插入。非常感谢您的帮助,并对大量代码表示歉意(包括在内,以便您可以阅读我的课程的所有代码)。
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();
}
InformationPanel.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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些一般性评论:您的 UI 逻辑(Ajax,..)和“业务”逻辑(“为学生获取模块”)全部混合在一起。考虑通过引入“服务”层来将它们分开。另外,我不会像您在数据模型中那样使用学生姓名。如果学生改了名字怎么办?或者模块被重命名?请改用生成的 ID。
您使用 jdbc 的方式会打开您的应用程序以进行 sql 注入。 永远不要构建连接用户提供的输入的查询。为此使用准备好的语句(数据库的查询缓存也会喜欢这样;))
您的实际问题:您使用ajax。使用 Ajax,UI 中的更改不会推送到模型中。您可以正常获取学生姓名,因为您已将 AjaxFormComponentUpdatingBehavior 添加到了 DDC。
希望有帮助
编辑:查看您的个人资料,我发现您在这里提出了一些问题,但从未接受过其中任何一个的答案。 我强烈建议您这样做,否则人们会忽略您的问题。
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.