1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | package com.asprise.struts.form; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; public class OwnerForm extends ActionForm { private String email; private String greet = "Mr."; private String address; private int tel = 0; private String name; public ActionErrors validate( ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if (greet == null || greet.trim().equals("")) { errors.add("greet", new ActionError("error.greet")); } if (name == null || name.trim().equals("")) { errors.add("name", new ActionError("error.name")); } if (address == null || address.trim().equals("")) { errors.add("address", new ActionError("error.address")); } if (email == null || email.trim().equals("")) { errors.add("email", new ActionError("error.noEmail")); } else if (email.indexOf("@")==-1) { errors.add("email", new ActionError("error.wrongEmail")); } if (tel==0) { errors.add("tel", new ActionError("error.tel")); } return errors; } ... public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } ... } |
1 2 3 4 5 6 7 8 | errors.header=<h4>Validation Error(s)</h4><ul> errors.footer=</ul><hr> error.greet=<li>Choose your greet error.name=<li>Enter your name error.address=<li>Enter your address error.tel=<li>Enter your contact number error.wrongEmail=<li>Correct your email error.noEmail=<li>Enter your email |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class OwnerAction extends Action { public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { OwnerForm ownerForm = (OwnerForm) form; String greet = ownerForm.getGreet(); String name = ownerForm.getName(); request.setAttribute("name", name); request.setAttribute("greet", greet); // Forward control to the specified success target return (mapping.findForward("success")); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%> <html> <head> <meta name = "Generator" content = "Easy Struts Xslt generator for Eclipse (http://easystruts.sf.net)."> <title>Struts Form for ownerForm</title> </head> <body> <html:form action="/owner"> greet : <html:select property="greet"> <htmlption value=""></htmlption> <htmlption value="Mr.">Mr.</htmlption> <htmlption value="Miss">Miss</htmlption> <htmlption value="Mrs.">Mrs.</htmlption> </html:select><html:errors property="greet"/> name : <html:text property="name"/><html:errors property="name"/></br> address : <html:text property="address"/><html:errors property="address"/></br> email : <html:text property="email"/><html:errors property="email"/></br> tel : <html:text property="tel"/><html:errors property="tel"/></br> <html:submit/><html:cancel/> </html:form> <html:errors /> <body> </html> |
1 2 3 4 5 6 7 8 9 | <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <logic:present name="name" scope="request"> Thank you, <logic:present name="greet" scope="request"> <bean:write name="greet" scope="request"/> </logic:present> <bean:write name="name" scope="request"/> </logic:present> |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |