欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Struts2 简单数据验证

程序员文章站 2022-05-06 22:53:40
1、struts.xml

1、struts.xml

<struts>
 <constant name="struts.devmode" value="false"> 
 <constant name="struts.enable.dynamicmethodinvocation" value="true">
 <constant name="struts.i18n.encoding" value="utf-8"></constant>
 <package name="user" extends="struts-default">                 
        <action name="user" class="com.wxh.action.useraction">
            <result>/useraddsuccess.jsp</result>
            <result name="error">/adderror.jsp</result>
        </action>         
  </package>   
</constant></constant></struts>

2、useraction.java
package com.wxh.action;
 
import com.opensymphony.xwork2.actionsupport;

public  class useraction extends actionsupport{

private static final long serialversionuid = 1l; 
		
	private string name;
	public string add(){
//逻辑判断
		if(name==null || !name.equals("admin")){
			this.addfielderror("name", "name is error");
			this.addfielderror("name", "name is too long");
			return error;//返回到result的name为“error”所指定的jsp页面。
		}
		return success;
	}
	public string getname() {
		return name;
	}
	public void setname(string name) {
		this.name = name;
	}	
}
3、index.jsp

使用addfielderror方法和s:fielderror标签简单处理数据校验添加用户
4、adderror.jsp
<%@taglib uri="/struts-tags" prefix="s" %><!--{cke_protected}{c}%3c!%2d%2d%e7%94%a8%e5%88%b0ognl%e8%a1%a8%e8%be%be%e5%bc%8f%e6%97%b6%e9%9c%80%e8%a6%81%e8%bf%99%e5%8f%a5%2d%2d%3e-->

user add error!