四、struts学习之model接收参数和提示错误信息对象

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>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值