2013年2月14日 情人节 晴
Struts 2学习 第11part 简单数据验证
实例:Struts2_SimpleDataValiation
http://localhost:8080/Struts2_SimpleDataValiation/
显示index.jsp页面如下:
点击添加用户:进入以下页面
点击[Debug]会在该页面下面出现调试信息
以上就是整个校验所显示出来的效果
先看效果,再说明问题
我们要知道的东西是,上面的效果是如何呈现的,通过什么样的方式来实现数据校验?
index.jsp代码:
- <?xml version="1.0" encoding="GB18030" ?>
- <%@ page language="java" contentType="text/html; charset=GB18030"
- pageEncoding="GB18030"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
- <base href="<%=basePath %>"/>
- <title>Insert title here</title>
- </head>
- <body>
- 使用addFieldError方法和s:fieldError标签简单处理数据校验
- <a href="user/user!add?name=a">添加用户</a>
- </body>
- </html>
点击链接“添加用户”来到struts.xml文件
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <constant name="struts.devMode" value="true" />
- <package name="user" extends="struts-default" namespace="/user">
- <action name="user" class="com.wwj.struts2.user.action.UserAction">
- <result>/user_add_success.jsp</result>
- <result name="error">/user_add_error.jsp</result>
- </action>
- </package>
- </struts>
找到命名空间user下的action:user,找到UserAction类:
- package com.wwj.struts2.user.action;
- import com.opensymphony.xwork2.ActionSupport;
- public class UserAction extends ActionSupport {
- private String name;
- public String add() {
- if(name==null || !name.equals("admin")){
- this.addFieldError("name", "name is error");
- return ERROR;
- }
- return SUCCESS;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
通过action属性name来接收参数,name=a,动态调用add的方法,通过判断name的值是否为null,或者不等于admin,调用addFieldError方法来进行校验处理。
这时返回ERROR,回到struts.xml中,显示user_add_error.jsp页面,
这时来看看user_add_error.jsp后台代码:
- <?xml version="1.0" encoding="GB18030" ?>
- <%@ page language="java" contentType="text/html; charset=GB18030"
- pageEncoding="GB18030"%>
- <%@taglib uri="/struts-tags" prefix="s" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
- <title>Insert title here</title>
- </head>
- <body>
- User add Error!
- <s:fielderror fieldName="name" theme="simple"></s:fielderror>
- <br/>
- <s:property value="errors.name[0]"/>
- <s:debug></s:debug>
- </body>
- </html>
这里要说明的是,用到s标签库,要声明<%@taglib uri="/struts-tags" prefix="s"%>
在这个页面使用了其中三个标签,<s:fielderror />、<s:property/>、<s:debug/>
第一个是将错误信息显示出来,第二个标签是将属性名为errors的值显示出来,第三个标签是进行调试,会将值栈和栈上下文显示出来。具体效果在上面可以看到。