Struts2----s标签库

来源:互联网 发布:linux网络配置文件 编辑:程序博客网 时间:2024/06/05 20:25

转自:http://blog.csdn.net/meaijojo/article/details/7536793

数据校验是在项目开发中不可缺少的一部分,用户登录时、密码验证时都需要,当然要做的首先是获得用户输入的内容,然后对内容进行验证,一般都是从数据库中读出然后校验,如果错误则显示提示信息,正确则进入用户主界面。

下面用一个简单小例子来说明下步骤:

1、index的表单

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%   
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  8. <html>  
  9. <head>  
  10. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  11. <base href="<%=basePath %>"/>  
  12. <title>Insert title here</title>  
  13. </head>  
  14. <body>  
  15. <h1>演示</h1>  
  16. <form action="user/user!check" method="post">  
  17. 姓名:<input type="text" name="user.name"></input>  
  18. <br/>  
  19. 年龄:<input type="text" name="user.age"></input>  
  20. <br/>  
  21. <input type="submit" value="提交"/>  
  22. </form>  
  23. </body>  
  24. </html>  
提交时会有两个变量--user.name 和user.age传到server,然后调用struts.xml文件配置中的对应Action

2、struts.xml配置

[html] view plaincopy
  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="front" namespace="/user" extends="struts-default">  
  9.   
  10.         <action name="user" class="com.myservice.web.UserAction">  
  11.             <result>/success.jsp</result>  
  12.             <result name="error">/error.jsp</result>  
  13.         </action>  
  14.     </package>  
  15. </struts>  
很明显-当返回success时调用success.jsp,error则调用error.jsp

3、Action中的check方法内容

[java] view plaincopy
  1. public String check(){  
  2.         System.out.println("name="+user.getName());  
  3.         System.out.println("age="+user.getAge());  
  4.         if(user.getName().equals("admin")&&user.getAge()==20){  
  5.             return SUCCESS;  
  6.         }else{  
  7.             this.addFieldError("name""name is error");  
  8.             this.addFieldError("name""name is too long");  
  9.             return ERROR;  
  10.         }  
  11.     }  
在这里我们调用了addFieldError方法

4、error.jsp页面

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@taglib uri="/struts-tags" prefix="s" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>Insert title here</title>  
  9. </head>  
  10. <body>  
  11. <h2>验证失败</h2>  
  12. <s:property value="errors.name[0]"/>  
  13. <br>  
  14. <s:property value="errors.name[1]"/>  
  15. <s:debug></s:debug>  
  16. </body>  
  17. </html>  
里面第三行是说明的添加了struts2的标签库,并且以s开头。

而倒数第四行和第六行是重点,errors.name[0]对应的就是我们在3中通过addFieldError方法,放入到name属性中的name is error,errors.name[1]则很明显是name is too long。倒数第三行是调试信息。

整个效果最后显示为:


更多0


原创粉丝点击