Гость
Целевая тема:
Создать новую тему:
Автор:
Форумы / Java [игнор отключен] [закрыт для гостей] / validwhen в struts - не могу понять - где ошибка / 1 сообщений из 1, страница 1 из 1
20.04.2007, 12:28:03
    #34475619
whoneedshelp
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
validwhen в struts - не могу понять - где ошибка
Здравствуйте - проблема в следующем - хочу сделать проверку validwhen с очень простым правилом - сделал простейший пример - не могу понять где ошибся - в общем есть страничка calc.jsp
Код: plaintext
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.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>
<!DOCTYPE HTML  PUBLIC  "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Simple Java Struts Calculator</title>

<script language="javascript">
function resetControls() {
	document.getElementById("value1").value = "0";
	document.getElementById("value2").value = "0";
	document.getElementById("add").checked = false;
	document.getElementById("sub").checked = false;
	document.getElementById("mul").checked = false;
	document.getElementById("div").checked = false;
}
</script>
</head>
<body>
<h1>Struts Simple Calculator </h1>
<hr />
<html:form action="/calc" focus="value1">
	<h2>Input values</h2>
	<table>
		<tr>
			<td  class ="text_field_label">Value1</td>
			<td><html:text property="value1" size="5" styleId="value1" /></td>
		</tr>
		<tr>
			<td  class ="text_field_label">Value2</td>
			<td><html:text property="value2" size="5" styleId="value2" /></td>
		</tr>
		<tr>
			<td  class ="text_field_label">test</td>
			<td><html:text property="test" size="5" /></td>
		</tr>
	</table>
	<h2>Select operation</h2>
	<table>
		<tr>
			<td>
			<ul  class ="operations">
				<li><html:radio property="operation" value="add" styleId="add" />Addition</li>
				<li><html:radio property="operation" value="sub" styleId="sub" />Substruction</li>
				<li><html:radio property="operation" value="mul" styleId="mul" />Multiplication</li>
				<li><html:radio property="operation" value="div" styleId="div" />Division</li>
			</ul>
			</td>
			<td  class ="error"><html:errors property="operation" /></td>
		</tr>
	</table>
	<br />
	<html:submit value="Calculate" />
	<input type="button" onClick="javascript:resetControls()" value="Reset" />
</html:form>
<logic:messagesPresent>
	<div  class ="error">
	<h3>Errors</h3>
	<html:errors /></div>
</logic:messagesPresent>
</body>
</html>

связана с Action классом
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
 package  actions;

 import  javax.servlet.http.HttpServletRequest;
 import  javax.servlet.http.HttpServletResponse;

 import  org.apache.struts.action.Action;
 import  org.apache.struts.action.ActionForm;
 import  org.apache.struts.action.ActionForward;
 import  org.apache.struts.action.ActionMapping;

 public   class  CalcAction  extends  Action {
    public  ActionForward execute(ActionMapping actionMapping, ActionForm actionForm,
         HttpServletRequest request, HttpServletResponse response)  throws  Exception {
       return  actionMapping.findForward("self");
   }
}

форма связана с классом CalcValidatorForm
Код: plaintext
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.
 package  actionforms;

 import  org.apache.struts.validator.ValidatorForm;

 public   class  CalcValidatorForm  extends  ValidatorForm {
    private   int  value1;
    private   int  value2;
    private  String operation;
    private  String test;
   

    public   int  getValue1() {
       return  value1;
   }

    public   void  setValue1( int  value1) {
       this .value1 = value1;
   }

    public   int  getValue2() {
       return  value2;
   }

    public   void  setValue2( int  value2) {
       this .value2 = value2;
   }

    public  String getOperation() {
       return  operation;
   }

    public   void  setOperation(String operation) {
       this .operation = operation;
   }

    public  String getTest() {
       return  test;
   }

    public   void  setTest(String test) {
       this .test = test;
   }
}


файл настроек struts - struts-config.xml
Код: plaintext
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.
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE struts-config  PUBLIC  "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
	"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
	<form-beans>
		<form-bean type="actionforms.CalcValidatorForm" name="calc"/>
	</form-beans>
	
	<global-exceptions>
	</global-exceptions>
	
	<global-forwards>
		<forward name="welcome" path="/Welcome.do"/>
	</global-forwards>
	
	<action-mappings>
		<action path="/calc" scope="request" type="actions.CalcAction" name="calc" validate="true"
			input="/pages/calc.jsp">
			<forward path="/pages/calc.jsp" name="self"/>
		</action>
	</action-mappings>
	
	
	<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
	
	<message-resources parameter="resources.CalcResources"/>
	
	<plug-in className="org.apache.struts.tiles.TilesPlugin">
		
		<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml"/>
		<set-property property="moduleAware" value="true"/>
	</plug-in>
	
	
	<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
		<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
	</plug-in>
	
</struts-config>

правило валидации очень простое - применяю к полю value2
и validation.xml выглядит так
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">

<form-validation>
<global>
</global>

<formset>
<form name="calc">
<field property="value2" depends="required,validwhen">
<arg0 key="Zero divide1" resource="false" name="required"/>
<arg0 key="Zero divide2" resource="false" name="validwhen"/>
<var>
<var-name> test </var-name>
<var-value> ((operation != "div") or (*this* != 0)) </var-value>
</var>
</field>
</form>
</formset>




</form-validation>

проблема в том что валидатор выдает сообщение для required а не для validwhen. Не могу понять где ошибка
...
Рейтинг: 0 / 0
Форумы / Java [игнор отключен] [закрыт для гостей] / validwhen в struts - не могу понять - где ошибка / 1 сообщений из 1, страница 1 из 1
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]