</pre>本章需要前面的action知识<p></p><p></p><pre name="code" class="java">public String check(){
if(teacher.getUsername().toString()!="admin"){
this.addFieldError("username", "username is error");
return "success";
}
return "error";
}
假若传过来的参数不为admin就有username is error
在JSP页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'teacher_add_success.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is teacher_check_success page. <br>
<a href="test2/Teacher_error">去到teacher失败的页面</a>
<br>
标签 :<p>fielderror</p>
<s:fielderror fieldName="username" /><br>
标签 property
<s:property value="errors.username{0}"/>
<br>
<s:debug></s:debug>
</body>
</html>
知识点1:
引入<%@taglib uri="/struts-tags" prefix="s" %>才能使用jsp的标签 s开头的
<s:fielderror fieldName="username" /> 该标签输出 fielderror 的值
<s:debug></s:debug>显示debug
效果图如下:
上图是debug 还有 fieldErrors的效果图。
Struts ValueStack Debug(一个值栈) 可以用来查看调试数据。。
我们在这里可以看到 errors 还有fieldErrors 他这里的类型是Map类型
要想提取,要以errors.username来提取