1.jsp相关代码
<s:form action="demo/demoActionadd">
<s:textfield label="name" name="student.name"></s:textfield>
<s:password label=" " name="student.password"></s:password>
<s:submit label="提交"></s:submit>
</s:form>
2.建立相关model类
public class Student {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String toString() {
return "姓名:"+getName()+"密码:"+getPassword();
}
}
3. 在action中建立相关属性
public class DemoAction extends ActionSupport{
private String name;
private String password;
private Student student;
private Map request;
private Map session;
private Map appliction;
public String add() {
request = (Map) ActionContext.getContext().get("request");
session = ActionContext.getContext().getSession();
appliction = ActionContext.getContext().getApplication();
String returnString = ERROR;
if (student == null) {
this.addFieldError("student", "用户对象为空");
} else if (student.getName() == null) {
this.addFieldError("student", "用户名为空");
} else if (student.getPassword() == null
|| "".equals(student.getPassword())) {
this.addFieldError("student", "密码为空");
} else {
request.put("name", student.getName());
session.put("name", student.getName());
appliction.put("name", student.getName());
returnString = SUCCESS;
}
this.addFieldError("", "用户名为空");
return returnString;
}
。。。。
* @return the student
<span style="white-space:pre"> </span> */
<span style="white-space:pre"> </span>public Student getStudent() {
<span style="white-space:pre"> </span>return student;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>/**
<span style="white-space:pre"> </span> * @param student
<span style="white-space:pre"> </span> * the student to set
<span style="white-space:pre"> </span> */
<span style="white-space:pre"> </span>public void setStudent(Student student) {
<span style="white-space:pre"> </span>this.student = student;
<span style="white-space:pre"> </span>}
4.页面提示错误信息
<s:fielderror fieldName="student" theme="simple"/>
<s:fielderror filedName="name"></s:fielderror>
<br />
<s:property value="errors.name[0]"/>
<s:debug></s:debug>