我想在页面加载时填充我的文本字段值和下拉值。当用户点击提交时,文本字段和下拉列值应绑定到模型(Person类)。
我该怎么办?
public class Person {
private String name;
private String food;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setFood(String food) {
this.food = food;
}
public String getFood() {
return food;
}
}
public class PersonAction extends ActionSupport implements ModelDriven {
private Map<String,String> foodList = new HashMap()<String,String>;
private String name = "What is your name?"
private Person person;
public String execute() {
foodList.put("IC","Ice Cream");
foodList.put("CA","Cake");
return SUCCESS;
}
public Object getModel() {
return person;
}
}
<s:form action="addPerson" >
<s:textfield name="name">
<s:select list="foodList" name="" />
<s:submit type="button" name="submit" />
</s:form>
答案 0 :(得分:2)
您应该在标签中使用name属性:select和s:textfield
<s:textfield name="name">
<s:select list="foodList" name="food" />
这样,该字段将在操作中与getModel()。name和getModel()。food绑定。
要显示“您的名字是什么”文字,您有两种选择:
在操作类中,将人名设置为“您的名字是什么”。
public String execute() {
person = new Person();
person.setName("What is your name?");
foodList.put("IC","Ice Cream");
foodList.put("CA","Cake");
return SUCCESS;
}
或者您可以使用占位符标记属性默认显示“您的名字”文字。
<s:textfield name="name" placeholder="What is your name?">
我编辑了使用modelDriven实现的操作的答案。