2013年2月14日  情人节 晴

Struts 2学习 第11part 简单数据验证

实例:Struts2_SimpleDataValiation

http://localhost:8080/Struts2_SimpleDataValiation/

显示index.jsp页面如下:

Struts 2 第11part 简单数据验证_Struts 2

点击添加用户:进入以下页面

Struts 2 第11part 简单数据验证_Struts 2_02

点击[Debug]会在该页面下面出现调试信息

Struts 2 第11part 简单数据验证_Struts 2_03

以上就是整个校验所显示出来的效果

先看效果,再说明问题

我们要知道的东西是,上面的效果是如何呈现的,通过什么样的方式来实现数据校验?

index.jsp代码:

  1. <?xml version="1.0" encoding="GB18030" ?> 
  2. <%@ page language="java" contentType="text/html; charset=GB18030" 
  3.     pageEncoding="GB18030"%> 
  4.  
  5. <%   
  6. String path = request.getContextPath();  
  7. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  8. %> 
  9.  
  10. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  11. <html xmlns="http://www.w3.org/1999/xhtml"> 
  12. <head> 
  13. <meta http-equiv="Content-Type" content="text/html; charset=GB18030" /> 
  14. <base href="<%=basePath %>"/> 
  15. <title>Insert title here</title> 
  16. </head> 
  17. <body> 
  18. 使用addFieldError方法和s:fieldError标签简单处理数据校验  
  19. <a href="user/user!add?name=a">添加用户</a> 
  20.       
  21. </body> 
  22. </html> 

点击链接“添加用户”来到struts.xml文件

  1. <?xml version="1.0" encoding="UTF-8" ?> 
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd"> 
  5.  
  6. <struts> 
  7.     <constant name="struts.devMode" value="true" /> 
  8.     <package name="user" extends="struts-default" namespace="/user"> 
  9.         <action name="user" class="com.wwj.struts2.user.action.UserAction"> 
  10.             <result>/user_add_success.jsp</result> 
  11.             <result name="error">/user_add_error.jsp</result> 
  12.         </action> 
  13.     </package> 
  14. </struts> 

找到命名空间user下的action:user,找到UserAction类:

  1. package com.wwj.struts2.user.action;  
  2.  
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.  
  5. public class UserAction extends ActionSupport {  
  6.     private String name;  
  7.     public String add() {  
  8.         if(name==null || !name.equals("admin")){  
  9.             this.addFieldError("name""name is error");  
  10.             return ERROR;  
  11.         }  
  12.         return SUCCESS;  
  13.     }  
  14.     public String getName() {  
  15.         return name;  
  16.     }  
  17.     public void setName(String name) {  
  18.         this.name = name;  
  19.     }  
  20. }  
  21.  
  22.      

通过action属性name来接收参数,name=a,动态调用add的方法,通过判断name的值是否为null,或者不等于admin,调用addFieldError方法来进行校验处理。

这时返回ERROR,回到struts.xml中,显示user_add_error.jsp页面,

这时来看看user_add_error.jsp后台代码:

  1. <?xml version="1.0" encoding="GB18030" ?> 
  2. <%@ page language="java" contentType="text/html; charset=GB18030" 
  3.     pageEncoding="GB18030"%> 
  4.     <%@taglib uri="/struts-tags" prefix="s" %> 
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  6. <html xmlns="http://www.w3.org/1999/xhtml"> 
  7. <head> 
  8. <meta http-equiv="Content-Type" content="text/html; charset=GB18030" /> 
  9. <title>Insert title here</title> 
  10. </head> 
  11. <body> 
  12.     User add Error!  
  13.     <s:fielderror fieldName="name" theme="simple"></s:fielderror> 
  14.     <br/> 
  15.     <s:property value="errors.name[0]"/> 
  16.     <s:debug></s:debug> 
  17. </body> 
  18. </html> 

这里要说明的是,用到s标签库,要声明<%@taglib uri="/struts-tags" prefix="s"%>

在这个页面使用了其中三个标签,<s:fielderror />、<s:property/>、<s:debug/>

第一个是将错误信息显示出来,第二个标签是将属性名为errors的值显示出来,第三个标签是进行调试,会将值栈和栈上下文显示出来。具体效果在上面可以看到。