Struts简单数据验证
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="validate" namespace="/" extends="struts-default"> <action name="*-*" class="com.wepull.action.{1}Action" method="{2}"> <result name="success" type="dispatcher">/{1}-{2}.jsp</result> <result name="error" >/error.jsp</result> </action> </package> </struts>
package com.wepull.action; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; public class ValidateAction extends ActionSupport{ private String name; @Override public String execute() throws Exception { System.out.println(name); if(name==null||"admin".equalsIgnoreCase(name)){ this.addFieldError("errorName", "名字为空或不不合法"); return ERROR; } return SUCCESS; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Validate-execute.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> </head> <body> 可以注册用户名:${name } </body> </html>
error.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> </head> <body> <s:fielderror name="errorName"></s:fielderror> <br> <br> <s:property value="errors.errorName"/> <br> <s:property value="errors.errorName[0]"/> <s:debug></s:debug> </body> </html>
相关推荐
yangkang 2020-11-09
lbyd0 2020-11-17
sushuanglei 2020-11-12
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
腾讯soso团队 2020-11-06
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
gaobudong 2020-11-04
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30