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>
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標簽簡單處理數據校驗添加用戶
<%@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!