我如何使用 struts 2 将组合框值提取到我的 DAO 中?
我有一张简单的用户注册表。 它包括姓名、年龄、性别和姓名。 城市。 但这里的城市来自城市主表和城市主表。 将用户显示为组合框。 但是,当我尝试通过 UserDAO 存储完整的用户注册信息时,如何访问当前选定的城市值? 我的用户注册jsp 表单包含:
<s:form action="UserAction" >
<s:textfield name="name" label="User Name" />
<s:textfield name="age" label="Age" />
<s:radio name="sex" label="Sex" list="{'M','F'}" />
<s:select list="cities" key="cities.name" listValue="name">
</s:select>
<s:submit />
</s:form>
现在我正在调用UserAction 的execute() 方法。 它包含:
public String execute() throws Exception {
UserDAO.insert(user);
return SUCCESS;
}
我的 DAO 的插入方法有以下代码:
public static void insert(UserBean daoUb) throws Exception {
Connection daoConn = null;
CityBean cb=new CityBean();
cb=daoUb.getCity();
System.out.println("cb = "+cb);
daoConn = connectionUtil.getConnection();
Statement daoSt = daoConn.createStatement();
String str = "INSERT user (name,age,sex,city) VALUES('" + daoUb.getName() + "'," + daoUb.getAge() + ",'" + daoUb.getSex() + "','"+cb.getName()+"')";
daoSt.executeUpdate(str);
//daoConn.close();
}
如果有人有任何建议,请建议我。 我被这个问题困住了......
I have one simple User Registration form. It includes name, age, sex & city. But here city is coming form city master table & displaying user as a combo box. But when i am trying to store full user registration information through my UserDAO, how can i access that current selected city value ? My User Registration jsp form contains :
<s:form action="UserAction" >
<s:textfield name="name" label="User Name" />
<s:textfield name="age" label="Age" />
<s:radio name="sex" label="Sex" list="{'M','F'}" />
<s:select list="cities" key="cities.name" listValue="name">
</s:select>
<s:submit />
</s:form>
Now i am calling UserAction's execute() method. It contains :
public String execute() throws Exception {
UserDAO.insert(user);
return SUCCESS;
}
My DAO's insert method has following code :
public static void insert(UserBean daoUb) throws Exception {
Connection daoConn = null;
CityBean cb=new CityBean();
cb=daoUb.getCity();
System.out.println("cb = "+cb);
daoConn = connectionUtil.getConnection();
Statement daoSt = daoConn.createStatement();
String str = "INSERT user (name,age,sex,city) VALUES('" + daoUb.getName() + "'," + daoUb.getAge() + ",'" + daoUb.getSex() + "','"+cb.getName()+"')";
daoSt.executeUpdate(str);
//daoConn.close();
}
Plz suggest me, if anyone have any suggestion. I am stuck with this problem....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用与其他领域使用的类似方法来获取该信息。 您唯一需要做的就是为该字段命名。
为您的列表指定 Bean 中的确切名称,在您的例子中为 city。 它现在应该开始工作了。
You can get that using similar method what you are using for other fields. The only thing you need to do is give that field a name.
Give your list the exact name what you have in your bean, in your case city. It should start working now.
Vinegar 建议像这样修改你的 jsp
这假设你的 bean 具有你已经实现的城市属性的 getter 和 setter。
What Vinegar suggests is to modify your jsp like this
This assumes that your bean has getters and setters for a city property which you have already implemented.