commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From javaant <arun.kis...@hurix.com>
Subject partial bean submission with SimpleFormController
Date Fri, 27 Jul 2007 15:16:38 GMT

hi ,
I m struck up in a problem and need  help badly......pls try to reply it as
soon as possible.....please.........
my senarion is i hav 2 controllers(PersonController1.java &
PersonController2.java,both extending SimpleFormController).
one bean class(PersonData.java).
two jsps (person1.jsp,person2.jsp).
these two jsps dont use all the properties of PersonData.java.
person1.java uses 1st,last name property and person2.jsp uses email and pwd.


now i want only server side validation....so i wrote the following
validator.xml


			"validator.xml"
			---------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC 
    "-//Apache Software Foundation//DTD Commons Validator Rules
Configuration 1.1//EN" 
    "http://jakarta.apache.org/commons/dtds/validator_1_1.dtd">

<form-validation>
 <formset>

        <form name="personData">
			<field property="firstName" depends="required">
				<arg0 key="person.first.name" />
			</field>
			<field property="lastName" depends="required">>
				<arg0 key="person.last.name" />
			</field>
			<field property="email" depends="required">
				<arg0 key="person.email" />
			</field>
           		 <field property="password" depends="required">
				<arg0 key="person.password" />
			</field>
	 		<field property="verifyPassword" depends="validwhen">
				<arg0 key="person.password.not.matching" />
				<var>
					<var-name>test</var-name>
					<var-value>(*this* == password)</var-value>
				</var>
			</field>
        </form>
    </formset>
</form-validation>


but now the problem is.....even if i m able to do the server-side validation
for both the jsps i am not able to submit the 
form because in my 1st form person1.jsp i m not giving values for email and
password but to pass validation test it requires these
two values also(according to my validator.xml)and same for other jsp.
now as the command class is same form both jsps i hav to use
formname="personData" 
for both jsps for server side validation....but by doing so my  any form
will not be submitted.
i m struck up in this please tell me the solution.......is it possible if i
develop some logic in my respective controller
 to supress the validation for properties that are not entered.........if
yes then please 
give me the required logic....or else any other way to solve the problem...


				validator-servlet.xml
				---------------------
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

    <!--================================= Message sources
======================================-->

    <bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages"/>
	</bean>



    <!--================================== View Resolvers
=======================================-->

	<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>



    <!--================================ Handler mappings
=====================================-->

	<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="/person1.html">personController1</prop>
				<prop key="/person2.html">personController2</prop>
				<prop key="/validator.js">jsValidatorController</prop>
			</props>
		</property>
	</bean>

<!--
		 By default, if no handler mapping can be found in the context, the
DispatcherServlet creates 
		 a BeanNameUrlHandlerMapping   < can't set priority lower than other two
handlers > 
	 -->
	<bean id="beanHandlerMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
		<property name="order">
			<value>1</value>
		</property>
	</bean>

    <!--=================================== Controllers
========================================-->

    <bean id="jsValidatorController"
class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>

 

		<bean id="personController1"
class="org.springmodules.samples.validation.commons.web.PersonController1">
		<property name="formView" value="person1"/>
		<property name="successView" value="success"/>
		<property name="validator" ref="beanValidator"/>
        <property name="commandName" value="person"/>
        <property name="commandClass"
value="org.springmodules.samples.validation.commons.web.PersonData"/>
		
        </bean>



<bean id="personController2"
class="org.springmodules.samples.validation.commons.web.PersonController2">
		<property name="formView" value="person2"/>
		<property name="successView" value="success"/>
		<property name="validator" ref="beanValidator"/>
        <property name="commandName" value="person"/>
        <property name="commandClass"
value="org.springmodules.samples.validation.commons.web.PersonData"/>
</bean>


    


    <!--=================================== Commons Validator
=====================================-->

    
    <bean id="beanValidator"
class="org.springmodules.validation.commons.DefaultBeanValidator">
	
		<property name="validatorFactory" ref="validatorFactory"/>
	</bean>

	

    <bean id="validatorFactory"
class="org.springmodules.validation.commons.DefaultValidatorFactory">
		<property name="validationConfigLocations">
			<list>
				<value>/WEB-INF/validator-rules.xml</value>
				<value>/WEB-INF/validator.xml</value>
			</list>
		</property>
	</bean>


</beans>
				Person1.jsp
				-----------
<%@ page session="true" %>

<%@ include file="/WEB-INF/jsp/taglibs.jsp" %>

<c:set var="ctx" value="${rc.contextPath}"/>

<html>

<script type="text/javascript" src="<c:url
value="/validator.js"/>"></script>

<validator:javascript formName="personData" staticJavascript="false"
xhtml="true" cdata="false"/>

<body>

<h3>Personal informations1</h3>

<spring:bind path="person.*">
    
        <c:out value="${status.value}"/>
        
    
</spring:bind>

<form action="" method="post" onsubmit="return validatePersonData(this);">

        <table>
        
            <tr>
                <td><fmt:message key="person.first.name"/></td>
                <td>
                    <spring:bind path="person.firstName">
                        <input type="text" name="firstName" value="<c:out
value="${status.value}"/>" size="15" maxlength="60"/>
                        <c:out value="${status.errorMessage}"/>
                    </spring:bind>
                </td>
            </tr>
            <tr>
                <td><fmt:message key="person.last.name"/></td>
                <td>
                    <spring:bind path="person.lastName">
                        <input type="text" name="lastName" value="<c:out
value="${status.value}"/>" size="15" maxlength="60"/>
                        <c:out value="${status.errorMessage}"/>
                    </spring:bind>
                </td>
            </tr>
            
            

        </table>

        <br/>

        <input type="submit" value="<fmt:message key="form.submit"/>"/>
	
    </form>

</body>

</html>

				person2.jsp
				-----------
<%@ page session="true" %>

<%@ include file="/WEB-INF/jsp/taglibs.jsp" %>

<c:set var="ctx" value="${rc.contextPath}"/>

<html>

<script type="text/javascript" src="<c:url
value="/validator.js"/>"></script>



<body>

<h3>Personal informations2</h3>

<spring:bind path="person.*">
    
        <c:out value="${status.value}"/>
        
    
</spring:bind>

<form action="person2.html" name="personData" method="post" >

        <table>
        		<tr>
                <td><fmt:message key="person.email"/></td>
                <td>
                    <spring:bind path="person.email">
                        <input type="text" name="email" value="<c:out
value="${status.value}"/>" size="15" maxlength="60"/>
                        <c:out value="${status.errorMessage}"/>
                    </spring:bind>
                </td>
            </tr>
           
            
           
            <tr>
                <td><fmt:message key="person.password"/></td>
                <td>
                    <spring:bind path="person.password">
                        <input type="password" name="password" value="<c:out
value="${status.value}"/>" size="15" maxlength="60"/>
                        <c:out value="${status.errorMessage}"/>
                    </spring:bind>
                </td>
            </tr>
            <tr>
                <td><fmt:message key="person.verify.password"/></td>
                <td>
                    <spring:bind path="person.verifyPassword">
                        <input type="password" name="verifyPassword"
value="<c:out value="${status.value}"/>" size="15" maxlength="60"/>
                        <c:out value="${status.errorMessage}"/>
                    </spring:bind>
                </td>
            </tr>

        </table>

        <br/>

        <input type="submit" value="<fmt:message key="form.submit"/>"/>

    </form>

</body>

</html>

					PersonController1.java
					-------------------------
package org.springmodules.samples.validation.commons.web;

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

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class PersonController1 extends SimpleFormController {
	
	protected ModelAndView showForm(
			HttpServletRequest request, HttpServletResponse response, BindException
errors)
			throws Exception {
		System.out.println("-------------INSIDE SHOWFORM----------------");
		System.out.println("$$$$$$$$"+getSuccessView());
		System.out.println("%%%%%%%%"+errors);
		
		//return new ModelAndView(super.getSuccessView());
	return super.showForm(request, response, errors);
	}
	
	protected ModelAndView onSubmit(HttpServletRequest request,
    		HttpServletResponse response, Object command, BindException errors)
throws Exception { 
		PersonData person=((PersonData)command);
		System.out.println(person.getFirstName());
		request.getSession().setAttribute("person",person);
		System.out.println("--------INSIDE ONSUBMIT------------------");
    	System.out.println("@@@@@@@"+getSuccessView());
    	System.out.println("#######"+errors);
    	if(person.getFirstName()!=""&&person.getLastName()!=""){
    		
    	System.out.println("in if");
        return super.onSubmit(request, response, command, errors);
    	}
    	System.out.println("in else");
        return super.onSubmit(request, response, command, errors);
    }
	


	
	
	
}

	
	
						PersonController2
						-----------------
package org.springmodules.samples.validation.commons.web;

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

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class PersonController2 extends SimpleFormController {
	protected ModelAndView onSubmit(HttpServletRequest request,
    		HttpServletResponse response, Object command, BindException errors)
throws Exception { 
    	System.out.println("$$$$$$$$"+getSuccessView());
    	System.out.println("%%%%%%%%"+errors);
    		
        return super.onSubmit(request, response, command, errors);
    }

protected ModelAndView showForm(
		HttpServletRequest request, HttpServletResponse response, BindException
errors)
		throws Exception {
	System.out.println("-------------INSIDE SHOWFORM----------------");
	System.out.println("$$$$$$$$"+getSuccessView());
	System.out.println("%%%%%%%%"+errors);
	
	//return new ModelAndView(super.getSuccessView());
return super.showForm(request, response, errors);
}
}

				PersonData.java
				----------------
package org.springmodules.samples.validation.commons.web;


public class PersonData {
	private String firstName;
	private String lastName;
	private String email;
	private String password;
	private String verifyPassword;

	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getVerifyPassword() {
		return verifyPassword;
	}
	public void setVerifyPassword(String verifyPassword) {
		this.verifyPassword = verifyPassword;
	}
}
	


	
	
	

	
					

-- 
View this message in context: http://www.nabble.com/partial-bean-submission-with-SimpleFormController-tf4151563.html#a11810216
Sent from the Commons - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message