<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
<html>
<head><title>TestForm</title></head>
<body>
<html:form action="test">
Enter name:
<html:text property="name" value="name"/>
<br>
Enter number:
<html:text property="number" value="100"/>
<br>
<html:submit value="Send"/>
<br>
<html:errors/>
</html:form>
</body>
</html>
Struts Validator
может у кого есть готовый пример который достаточно скапировать на сервер и он будет работать? хочу посмотреть где и что
А ты в struts-config.xml в теге action параметр input написал?
да писал
Код:
struts-config.xml
Код:
<struts-config>
<form-beans>
<form-bean name="TestForm" type="TestForm"/>
</form-beans>
<action-mappings>
<action path="test" name="TestForm" validate="true" input="/index.jsp" />
</action-mappings>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/org/apache/struts/validator/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
<form-beans>
<form-bean name="TestForm" type="TestForm"/>
</form-beans>
<action-mappings>
<action path="test" name="TestForm" validate="true" input="/index.jsp" />
</action-mappings>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/org/apache/struts/validator/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
validation.xml
Код:
<form-validation>
<formset >
<form name="TestForm">
<field property="name" depends="required, mask, maxlength">
<var>
<var-name>mask</var-name>
<var-value>^[A-ZА-Я][a-z A-ZА-Яа-я]+$</var-value>
</var>
<var>
<var-name>maxlength</var-name>
<var-value>20</var-value>
</var>
<arg key="Field name" position="0" name="required" resource="false"/>
<arg key="Field name" position="0" name="mask" resource="false"/>
<arg key="Field name" position="0" name="maxlength" resource="false"/>
</field>
<field property="number" depends="required, integer">
<arg key="Field number" position="0" name="required" resource="false"/>
<arg key="Field number" position="0" name="integer" resource="false"/>
</field>
</form>
</formset>
</form-validation>
<formset >
<form name="TestForm">
<field property="name" depends="required, mask, maxlength">
<var>
<var-name>mask</var-name>
<var-value>^[A-ZА-Я][a-z A-ZА-Яа-я]+$</var-value>
</var>
<var>
<var-name>maxlength</var-name>
<var-value>20</var-value>
</var>
<arg key="Field name" position="0" name="required" resource="false"/>
<arg key="Field name" position="0" name="mask" resource="false"/>
<arg key="Field name" position="0" name="maxlength" resource="false"/>
</field>
<field property="number" depends="required, integer">
<arg key="Field number" position="0" name="required" resource="false"/>
<arg key="Field number" position="0" name="integer" resource="false"/>
</field>
</form>
</formset>
</form-validation>
код класса формы
Код:
public class TestForm extends ValidatorForm {
protected String name;
protected int number;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
protected String name;
protected int number;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
моя версия стратса: 1.3.0
большое спасибо за пример теперь намного быстрее разбирусь, что на что влияет
Код:
StandardManager[/struts]: Seeding random number generator class java.security.SecureRandom
StandardManager[/struts]: Seeding of random number generator has been completed
ContextConfig[/struts]: Added certificates -> request attribute Valve
StandardWrapper[/struts:default]: Loading container servlet default
Parse Error at line 10 column 68: Element type "plug-in" must be declared.
org.xml.sax.SAXParseException: Element type "plug-in" must be declared.
..........
Parse Error at line 14 column 17: The content of element type "struts-config" must match "(data-sources?,form-beans?,global-forwards?,action-mappings?)".
org.xml.sax.SAXParseException: The content of element type "struts-config" must match "(data-sources?,form-beans?,global-forwards?,action-mappings?)".
..........
register('-//Apache Software Foundation//DTD Struts Configuration 1.0//EN', 'jar:file:E:/Documents and Settings/z/jbproject/struts/struts/WEB-INF/lib/struts.jar!/org/apache/struts/resources/struts-config_1_0.dtd'
register('-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN', 'jar:file:E:/Documents and Settings/z/jbproject/struts/struts/WEB-INF/lib/struts.jar!/org/apache/struts/resources/web-app_2_2.dtd'
register('-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN', 'jar:file:E:/Documents and Settings/z/jbproject/struts/struts/WEB-INF/lib/struts.jar!/org/apache/struts/resources/web-app_2_3.dtd'
resolveEntity('-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN', 'http://java.sun.com/dtd/web-app_2_3.dtd')
Resolving to alternate DTD 'jar:file:E:/Documents and Settings/z/jbproject/struts/struts/WEB-INF/lib/struts.jar!/org/apache/struts/resources/web-app_2_3.dtd'
Call org.apache.struts.action.ActionServlet.addServletMapping(action/java.lang.String,*.do/java.lang.String)
Call org.apache.struts.action.ActionServlet.addServletMapping(debugjsp/java.lang.String,*.jsp/java.lang.String)
StandardWrapper[/struts:invoker]: Loading container servlet invoker
WebappLoader[]: Deploying class repositories to work directory E:\Documents and Settings\z\jbproject\struts\Tomcat\work
WebappLoader[]: Reloading checks are enabled for this Context
StandardManager[]: Seeding random number generator class java.security.SecureRandom
StandardManager[]: Seeding of random number generator has been completed
ContextConfig[]: Added certificates -> request attribute Valve
StandardWrapper[:default]: Loading container servlet default
StandardWrapper[:invoker]: Loading container servlet invoker
HttpConnector[8083] Starting background thread
StandardWrapperValve[debugjsp]: Servlet.service() for servlet debugjsp threw exception
javax.servlet.ServletException
javax.servlet.ServletException
StandardManager[/struts]: Seeding of random number generator has been completed
ContextConfig[/struts]: Added certificates -> request attribute Valve
StandardWrapper[/struts:default]: Loading container servlet default
Parse Error at line 10 column 68: Element type "plug-in" must be declared.
org.xml.sax.SAXParseException: Element type "plug-in" must be declared.
..........
Parse Error at line 14 column 17: The content of element type "struts-config" must match "(data-sources?,form-beans?,global-forwards?,action-mappings?)".
org.xml.sax.SAXParseException: The content of element type "struts-config" must match "(data-sources?,form-beans?,global-forwards?,action-mappings?)".
..........
register('-//Apache Software Foundation//DTD Struts Configuration 1.0//EN', 'jar:file:E:/Documents and Settings/z/jbproject/struts/struts/WEB-INF/lib/struts.jar!/org/apache/struts/resources/struts-config_1_0.dtd'
register('-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN', 'jar:file:E:/Documents and Settings/z/jbproject/struts/struts/WEB-INF/lib/struts.jar!/org/apache/struts/resources/web-app_2_2.dtd'
register('-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN', 'jar:file:E:/Documents and Settings/z/jbproject/struts/struts/WEB-INF/lib/struts.jar!/org/apache/struts/resources/web-app_2_3.dtd'
resolveEntity('-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN', 'http://java.sun.com/dtd/web-app_2_3.dtd')
Resolving to alternate DTD 'jar:file:E:/Documents and Settings/z/jbproject/struts/struts/WEB-INF/lib/struts.jar!/org/apache/struts/resources/web-app_2_3.dtd'
Call org.apache.struts.action.ActionServlet.addServletMapping(action/java.lang.String,*.do/java.lang.String)
Call org.apache.struts.action.ActionServlet.addServletMapping(debugjsp/java.lang.String,*.jsp/java.lang.String)
StandardWrapper[/struts:invoker]: Loading container servlet invoker
WebappLoader[]: Deploying class repositories to work directory E:\Documents and Settings\z\jbproject\struts\Tomcat\work
WebappLoader[]: Reloading checks are enabled for this Context
StandardManager[]: Seeding random number generator class java.security.SecureRandom
StandardManager[]: Seeding of random number generator has been completed
ContextConfig[]: Added certificates -> request attribute Valve
StandardWrapper[:default]: Loading container servlet default
StandardWrapper[:invoker]: Loading container servlet invoker
HttpConnector[8083] Starting background thread
StandardWrapperValve[debugjsp]: Servlet.service() for servlet debugjsp threw exception
javax.servlet.ServletException
javax.servlet.ServletException
может это из-за различных версий, попробую ещё с 1.3, скачаю
Может, и я ошибся.
В Пн-Вт проверю еще разок на работе. Надеюсь будет еще актуально ;)
чуть-что скинте архив
Я в идее работал.
На томкате деплоил.
Имя валидирует :)
спасибо
Код:
Web-inf/validator-rules.xml
Web-inf/validation.xml
classes/resources/MessageResources.properties
Web-inf/validation.xml
classes/resources/MessageResources.properties
в struts-config.xml я прописал
Код:
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml"/>
<set-property property="pathnames"
value="/WEB-INF/validation.xml"/>
</plug-in>
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml"/>
<set-property property="pathnames"
value="/WEB-INF/validation.xml"/>
</plug-in>
и
Код:
<message-resources parameter="resources.MessageResources"/>
при запуске (я пользуюсь JBuilder 9.0), выдаёт ошибки
Код:
Parse Error at line 37 column 68: Element type "plug-in" must be declared.
org.xml.sax.SAXParseException: Element type "plug-in" must be declared.
org.xml.sax.SAXParseException: Element type "plug-in" must be declared.
Код:
Parse Error at line 43 column 62: Element type "message-resources" must be declared.
org.xml.sax.SAXParseException: Element type "message-resources" must be declared.
org.xml.sax.SAXParseException: Element type "message-resources" must be declared.
Код:
Parse Error at line 44 column 17: The content of element type "struts-config" must match "(data-sources?,form-beans?,global-forwards?,action-mappings?)".
org.xml.sax.SAXParseException: The content of element type "struts-config" must match "(data-sources?,form-beans?,global-forwards?,action-mappings?)".
org.xml.sax.SAXParseException: The content of element type "struts-config" must match "(data-sources?,form-beans?,global-forwards?,action-mappings?)".
Я использую Struts 1.0.2
Parse Error at line 44 column 17: The content of element type "struts-config" must match "(data-sources?,form-beans?,global-forwards?,action-mappings?)".
org.xml.sax.SAXParseException: The content of element type "struts-config" must match "(data-sources?,form-beans?,global-forwards?,action-mappings?)".
[/quote]
О!!!!!!!!
1. Твой парсер говорит, что у тебя не в том порядке стоят элементы struts-configa может быть, поэтому, он и не может найти plug-in и message-resources?!
2. Он еще говорит, что у тебя не знает никаких других элементов, кроме "(data-sources?,form-beans?,global-forwards?,action-mappings?)"
3.
Цитата:
Я использую Struts 1.0.2
В версии 1.3.5 точно есть плагины и ресурсы ;), а в 1.0.2 их, судя по всему, нет.
я кроме form-beans ,action-mappings, других пораметров не указывал, так что нарушение порядко быть и не может, а когда отключаю плагины и ресуры то насчёт порядкаошибок не бывает