html:errors标签使用(需配置资源文件ApplicationResources.properties)

来源:互联网 发布:如何成为淘宝粉丝 编辑:程序博客网 时间:2024/05/22 02:07

 

web.xml

Java代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">   
  3.   <servlet>   
  4.     <servlet-name>action</servlet-name>   
  5.     <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>   
  6.     <init-param>   
  7.       <param-name>config</param-name>   
  8.       <param-value>/WEB-INF/struts-config.xml</param-value>   
  9.     </init-param>   
  10.     <init-param>   
  11.       <param-name>debug</param-name>   
  12.       <param-value>3</param-value>   
  13.     </init-param>   
  14.     <init-param>   
  15.       <param-name>detail</param-name>   
  16.       <param-value>3</param-value>   
  17.     </init-param>   
  18.     <load-on-startup>0</load-on-startup>   
  19.   </servlet>   
  20.   <servlet-mapping>   
  21.     <servlet-name>action</servlet-name>   
  22.     <url-pattern>*.do</url-pattern>   
  23.   </servlet-mapping>   
  24.   <welcome-file-list>   
  25.     <welcome-file>index.jsp</welcome-file>   
  26.   </welcome-file-list>   
  27. </web-app>  

 

 

Struts-config.xml 

  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">   
  3.   
  4. <struts-config>   
  5.     <data-sources />   
  6.     <form-beans>   
  7.     <form-bean name="bookForm" type="example.form.BookForm" />   
  8.   
  9.     </form-beans>   
  10.   
  11.     <global-exceptions />   
  12.     <global-forwards />   
  13.     <action-mappings>   
  14.     <action   
  15.       attribute="bookForm"  
  16.       input="/book/book.jsp"  
  17.       name="bookForm"  
  18.       path="/book"  
  19.       scope="request"  
  20.       type="example.action.BookAction" >   
  21.       <forward name="success" path="/book/success.jsp" />   
  22.       <forward name="home" path="/book/book.jsp" />   
  23.     </action>   
  24.   
  25.   
  26.   
  27.   
  28.     </action-mappings>   
  29.   
  30.     <message-resources parameter="book.ApplicationResources" />   
  31. </struts-config>  

 

 

 

book/books.jsp

  1. <%@ page language="java" pageEncoding="UTF-8"%>   
  2. <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>   
  3. <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>   
  4.   
  5. <html>   
  6.     <head>   
  7.         <title>JSP for BookForm form</title>   
  8.     </head>   
  9.     <body>   
  10.         <!--   <SPAN style="BACKGROUND-COLOR: #ff0000"><SPAN style="BACKGROUND-COLOR: #ffffff"><html:errors header="errors.header" footer="errors.footer"  
  11.             prefix="errors.prefix" suffix="errors.suffix" /></SPAN></SPAN> -->  
  12.         <html:form action="/book">   
  13.             id : <html:text property="id" />   
  14.             <html:errors property="id" />   
  15.             <br />   
  16.             name : <html:text property="name" />   
  17.             <html:errors property="name" />   
  18.             <br />   
  19.             pwd : <html:password property="pwd" />   
  20.             <html:errors property="pwd" />   
  21.             <br />   
  22.             pwd2 : <html:password property="pwd2" />   
  23.             <html:errors property="pwd2" />   
  24.             <br />   
  25.             <html:submit />   
  26.             <html:cancel />   
  27.         </html:form>   
  28.     </body>   
  29. </html>  

 

book/success.jsp

  1. <%@ page language="java" pageEncoding="UTF-8"%>   
  2.   
  3. <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>   
  4. <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>   
  5. <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>   
  6. <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>   
  7.   
  8.   
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  10. <html:html lang="true">   
  11.   <head>   
  12.     <html:base />   
  13.        
  14.     <title>success.jsp</title>   
  15.   
  16.     <meta http-equiv="pragma" content="no-cache">   
  17.     <meta http-equiv="cache-control" content="no-cache">   
  18.     <meta http-equiv="expires" content="0">       
  19.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">   
  20.     <meta http-equiv="description" content="This is my page">   
  21.     <!--   
  22.     <link rel="stylesheet" type="text/css" href="styles.css">   
  23.     -->   
  24.   
  25.   </head>   
  26.      
  27.   <body>   
  28.     <bean:write name="book" property="id" scope="request"/>   
  29.     <bean:write name="book" property="name" scope="request"/>   
  30.     <bean:write name="book" property="pwd" scope="request"/>   
  31.     <bean:write name="book" property="pwd2" scope="request"/>   
  32.   </body>   
  33. </html:html>  

 

 

 

book.ApplicationResources.properties

  1. bookForm.id.required=id is required   
  2. bookForm.name.required=name is required   
  3. bookForm.pwd.required=pwd is required   
  4. bookForm.pwd2.required=pwd2 is required   
  5. errors.header=   
  6. errors.footer=   
  7. errors.prefix=<font color="red">*   
  8. errors.suffix=</font>  

 

 

 

book.Utils.java

  1. package book;   
  2.   
  3. public class Utils {   
  4.     public static boolean isEmpty(String msg) {   
  5.         if (msg == null || msg.equals("")) {   
  6.             return true;   
  7.         } else {   
  8.             return false;   
  9.         }   
  10.     }   
  11. }  

 

 

 

example.action.BookAction.java

 

  1. /*  
  2.  * Generated by MyEclipse Struts  
  3.  * Template path: templates/java/JavaClass.vtl  
  4.  */  
  5. package example.action;   
  6.   
  7. import javax.servlet.http.HttpServletRequest;   
  8. import javax.servlet.http.HttpServletResponse;   
  9. import org.apache.struts.action.Action;   
  10. import org.apache.struts.action.ActionErrors;   
  11. import org.apache.struts.action.ActionForm;   
  12. import org.apache.struts.action.ActionForward;   
  13. import org.apache.struts.action.ActionMapping;   
  14. import org.apache.struts.action.ActionMessages;   
  15.   
  16. import example.form.BookForm;   
  17.   
  18. /**  
  19.  * MyEclipse Struts Creation date: 10-07-2008  
  20.  *   
  21.  * XDoclet definition:  
  22.  *   
  23.  * @struts.action path="/book" name="bookForm" input="/book/book.jsp"  
  24.  *                scope="request" validate="true"  
  25.  */  
  26. public class BookAction extends Action {   
  27.     /*  
  28.      * Generated Methods  
  29.      */  
  30.   
  31.     /**  
  32.      * Method execute  
  33.      *   
  34.      * @param mapping  
  35.      * @param form  
  36.      * @param request  
  37.      * @param response  
  38.      * @return ActionForward  
  39.      */  
  40.     public ActionForward execute(ActionMapping mapping, ActionForm form,   
  41.             HttpServletRequest request, HttpServletResponse response) {   
  42.         BookForm bookForm = (BookForm) form;   
  43.         ActionMessages errors = bookForm.validate(mapping, request);   
  44.         if (!errors.isEmpty()) {   
  45.             <SPAN style="BACKGROUND-COLOR: #ff0000"><SPAN style="BACKGROUND-COLOR: #ffffff">saveMessages(request, errors);</SPAN></SPAN>   
  46.             return mapping.findForward("home");   
  47.         }   
  48.         request.setAttribute("book", bookForm);   
  49.         return mapping.findForward("success");   
  50.     }   
  51. }  

 

 

example.form.BookForm.java

  1. package example.form;   
  2.   
  3. import javax.servlet.http.HttpServletRequest;   
  4.   
  5. import org.apache.struts.action.ActionErrors;   
  6. import org.apache.struts.action.ActionForm;   
  7. import org.apache.struts.action.ActionMapping;   
  8. import org.apache.struts.action.ActionMessage;   
  9.   
  10. import book.Utils;   
  11.   
  12. public class BookForm extends ActionForm {   
  13.   
  14.     private static final long serialVersionUID = 1L;   
  15.   
  16.     private String id;   
  17.     private String name;   
  18.     private String pwd;   
  19.     private String pwd2;   
  20.   
  21.     public String getId() {   
  22.         return id;   
  23.     }   
  24.   
  25.     public void setId(String id) {   
  26.         this.id = id;   
  27.     }   
  28.   
  29.     public String getName() {   
  30.         return name;   
  31.     }   
  32.   
  33.     public void setName(String name) {   
  34.         this.name = name;   
  35.     }   
  36.   
  37.     public String getPwd() {   
  38.         return pwd;   
  39.     }   
  40.   
  41.     public void setPwd(String pwd) {   
  42.         this.pwd = pwd;   
  43.     }   
  44.   
  45.     public String getPwd2() {   
  46.         return pwd2;   
  47.     }   
  48.   
  49.     public void setPwd2(String pwd2) {   
  50.         this.pwd2 = pwd2;   
  51.     }   
  52.   
  53.     @Override  
  54.     public void reset(ActionMapping mapping, HttpServletRequest request) {   
  55.         this.id = null;   
  56.         this.name = null;   
  57.         this.pwd = null;   
  58.         this.pwd2 = null;   
  59.     }   
  60.   
  61.     @Override  
  62.     public ActionErrors validate(ActionMapping mapping,   
  63.             HttpServletRequest request) {   
  64.         ActionErrors errors = new ActionErrors();   
  65.         if (Utils.isEmpty(this.id)) {   
  66.             errors.add("id"new ActionMessage("bookForm.id.required"));   
  67.         }   
  68.         if (Utils.isEmpty(this.name)) {   
  69.             errors.add("name"new ActionMessage("bookForm.name.required"));   
  70.         }   
  71.         if (Utils.isEmpty(this.pwd)) {   
  72.             errors.add("pwd"new ActionMessage("bookForm.pwd.required"));   
  73.         }   
  74.         if (Utils.isEmpty(this.pwd2)) {   
  75.             errors.add("pwd2"new ActionMessage("bookForm.pwd2.required"));   
  76.         }   
  77.         return errors;   
  78.     }   
  79.   
  80. }  

 

 

 

html:errors注意点:

1.<html:errors/>

2.saveErrors()

3.资源文件

 

 

原创粉丝点击