Announcement Announcement Module
Collapse
No announcement yet.
how show formated date in the <form:input path="..." ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • how show formated date in the <form:input path="..." ?

    HTML Code:
    <fmt:formatDate value="${commandName.dateFrom}" patter="dd.MM.yyyy" />
    
    <form:input path="dateFrom" maxlength="10" readonly="${isReadOnly}" />
    OK. <fmt:formatDate ... show formated date, but I need to show formated date in the <form:input paht="..." element.How show formated date (e.g "dd.MM.yyyy") in the <form:input element?

  • #2
    RTFM and RTFM.

    Again you are still asking RTFM questions... Read the reference guide.

    Comment


    • #3
      Originally posted by Marten Deinum View Post
      RTFM and RTFM.

      Again you are still asking RTFM questions... Read the reference guide.
      OK, what about this:
      http://forum.springsource.org/showthread.php?t=77053

      It's very important for me.

      Comment


      • #4
        Originally posted by Marten Deinum View Post
        RTFM and RTFM.

        Again you are still asking RTFM questions... Read the reference guide.
        I red 3.8.2. Internationalization using MessageSources and
        5.3. Resolving codes to error messages
        from reference guid and I don't understand how e.g. Resolving codes to error messages can help me show formatted date in the jsp. Please help

        Comment


        • #5
          Judging from your questions I got a feeling you don't have a clue on what you are doing. Again RTM and take the tutorial

          Configure a PropertyEditor that is used to parse and display your fields.

          I might seem a bit harsh but the question (apart from the sql one) you are asking the last few weeks are basic RTM/tutorial questions.

          (You also have some post about error messages if I'm not mistaken I was mixing things up).

          Comment


          • #6
            Originally posted by Marten Deinum View Post
            Judging from your questions I got a feeling you don't have a clue on what you are doing. Again RTM and take the tutorial

            Configure a PropertyEditor that is used to parse and display your fields.

            I might seem a bit harsh but the question (apart from the sql one) you are asking the last few weeks are basic RTM/tutorial questions.

            (You also have some post about error messages if I'm not mistaken I was mixing things up).

            OK, I red tutorial and ....
            Code:
            public class DateValidator implements Validator {
            	/** Logger for this class and subclasses */
            	protected final Log logger = LogFactory.getLog(getClass());
            
            	@Override
            	public boolean supports(Class clazz) {
            		return UserView.class.equals(clazz);
            	}
            
            	@Override
            	public void validate(Object target, Errors errors) {
            		UserView userView = (UserView) target;
            		if (userView.getDateFrom() == null) {
            			errors.rejectValue("dateFrom", "incorrect.date", null,
            					"Incorrect date");
            		}
            	}
            }
            and all is work.Fine. BUT in my project I use VALANG for all my validations. And the question is: Why my custom function in valang not work.


            NullPointerException when try parse date in the valang custom function

            my config bean:
            HTML Code:
            	<bean id="customPropertyEditorRegistrar"
            		class="com.mycompany.myproject.domain.editors.CustomPropertyEditorRegistrar" />
            
            	<bean id="usersController" class="com.mycompany.myproject.web.controllers.UsersController">
            		<property name="validators" ref="userValidator" />
            		<constructor-arg>
            			<ref bean="customPropertyEditorRegistrar" />
            		</constructor-arg>
            	</bean>
            	
            my valang:
            HTML Code:
            	<bean id="userValidator" class="org.springmodules.validation.valang.ValangValidator">
            		<property name="customFunctions">
            			<map>
            				<entry key="userValidator" value="com.mycompany.myproject.domain.validators.UserValidator" />
            				<entry key="dateValidator" value="com.mycompany.myproject.domain.validators.DateValidator" />
            			</map>
            		</property>
            		<property name="valang">
            			<value>
            				<![CDATA[
            	                { lastName : ? HAS TEXT : 'Last name is required' : 'required.lastName' }
            	                { firstName : ? HAS TEXT : 'First name is required' : 'required.firstName' }
            	                { pin : ? HAS TEXT : 'PIN is required' : 'required.pin' } 
            	                { branchId : ? > 0 : 'Branch is required' : 'required.branch' }
            	                { login : ? HAS TEXT : 'Login is required' : 'required.login' }
            	                { password : ? HAS TEXT : 'Password is required' : 'required.password' }
                                       {dateFrom: dateValidator(?) = FALSE: 'Date is incorrect' : 'incorrect.date' }
                                       { login : ? HAS NO TEXT OR length(?) >30 OR userValidator(?) = FALSE: 'Login name already exist' : 'exist.login' }	                
            				]]>
            			</value>
            		</property>
            Code:
            my custom editor:
            public final class CustomPropertyEditorRegistrar implements
            		PropertyEditorRegistrar {
            
            	@Override
            	public void registerCustomEditors(PropertyEditorRegistry registry) {
            		registry.registerCustomEditor(java.util.Date.class,
            				new CustomDateEditor(new SimpleDateFormat("dd.MM.yyyy"), true));
            
            		registry.registerCustomEditor(Float.class, new CustomNumberEditor(
            				Float.class, true));
            	}
            }
            
            my controller:
            
            public class UsersController extends MultiActionController {
            	private final PropertyEditorRegistrar customPropertyEditorRegistrar;
            	
            	public UsersController(PropertyEditorRegistrar propertyEditorRegistrar) {
            		this.customPropertyEditorRegistrar = propertyEditorRegistrar;
            	}
            
            	@Override
            	protected void bind(HttpServletRequest request, Object command)
            			throws Exception {
            		UserView userView = (UserView) command;
            		ServletRequestDataBinder servletRequestDataBinder = createBinder(
            				request, userView);
            		customPropertyEditorRegistrar
            				.registerCustomEditors(servletRequestDataBinder);
            		servletRequestDataBinder.bind(request);
            		bindingResult = servletRequestDataBinder.getBindingResult();
            	}
            my validator:
            Code:
            public class DateValidator extends AbstractFunction {
            	private final Log logger = LogFactory.getLog(getClass());
            
            	public DateValidator(Function[] arguments, int line, int column) {
            		super(arguments, line, column);
            		definedExactNumberOfArguments(1);
            	}
            
            	@Override
            	public boolean isAutowireByName() {
            		return true;
            	}
            
            	@Override
            	protected Object doGetResult(Object arg0) throws Exception {
            		String date = getArguments()[0].getResult(arg0).toString();// throw NullPointerException
            		SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
            		try {
            			dateFormat.parse(date);
            		} catch (ParseException ex) {
            			return Boolean.TRUE;
            		}
            		return Boolean.FALSE;
            	}
            }
            but when I in my jsp enter invalid date then throw the next exception:
            Code:
            		SEVERE: nested exception is java.lang.NullPointerException at line 15, column 33
            org.springmodules.validation.valang.ValangException: nested exception is java.lang.NullPointerException at line 15, column 33
                    at org.springmodules.validation.valang.functions.FunctionTemplate.execute(FunctionTemplate.java:53)
                    at org.springmodules.validation.valang.functions.AbstractFunction.getResult(AbstractFunction.java:110)
                    at org.springmodules.validation.valang.predicates.GenericTestPredicate.evaluate(GenericTestPredicate.java:60)
                    at org.springmodules.validation.valang.predicates.BasicValidationRule.validate(BasicValidationRule.java:124)
                    at org.springmodules.validation.valang.ValangValidator.validate(ValangValidator.java:194)
                    at org.springframework.validation.ValidationUtils.invokeValidator(ValidationUtils.java:63)
                    at com.mycompany.myproject.web.controllers.UsersController.validate(UsersController.java:107)
                    at com.mycompany.myproject.web.controllers.UsersController.add(UsersController.java:171)
                    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                    at java.lang.reflect.Method.invoke(Method.java:597)
                    at org.springframework.web.servlet.mvc.multiaction.MultiActionController.invokeNamedMethod(MultiActionController.java:473)
                    at org.springframework.web.servlet.mvc.multiaction.MultiActionController.handleRequestInternal(MultiActionController.java:410)
                    at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
                    at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
                    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
                    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
                    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
                    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:511)
                    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
                    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
                    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
                    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
                    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
                    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
                    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
                    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
                    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
                    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
                    at java.lang.Thread.run(Thread.java:619)
            Caused by: java.lang.NullPointerException
                    at com.mycompany.myproject.domain.validators.DateValidator.doGetResult(DateValidator.java:35)
                    at org.springmodules.validation.valang.functions.AbstractFunction$1.execute(AbstractFunction.java:112)
                    at org.springmodules.validation.valang.functions.FunctionTemplate.execute(FunctionTemplate.java:51)

            Comment


            • #7
              Originally posted by Marten Deinum View Post
              RTFM and RTFM.

              Again you are still asking RTFM questions... Read the reference guide.
              OK now all work (via implements Validator)
              Code:
              public class DateValidator implements Validator {
              	/** Logger for this class and subclasses */
              	protected final Log logger = LogFactory.getLog(getClass());
              
              	@Override
              	public boolean supports(Class clazz) {
              		return UserView.class.equals(clazz);
              	}
              
              	@Override
              	public void validate(Object target, Errors errors) {
              		UserView userView = (UserView) target;
              		if (userView.getDateFrom() == null) {
              			errors.reject("typeMismatch.dateFrom");
              		}
              	}
              }
              And because when I enter invalid date Spring show message:
              Code:
              SEVERE: [Field error in object 'commandName' on field 'dateFrom':
               rejected value [45.67.2009]; codes [typeMismatch.commandName.dateFrom,
              typeMismatch.dateFrom,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable:
              codes [commandName.dateFrom,dateFrom]; arguments []; default message [dateFrom]];
               default message [Failed to convert property value of type
              [java.lang.String] to required type [java.util.Date] for property 'dateFrom'; nested exception is java.lang.IllegalArgumentException: Could
              not parse date: 
              Unparseable date: "45.67.2009"], Field error in object 'commandName' on field 'password': rejected value []; codes [required
              .password.commandName.password,required.password.password,required.password.java.lang.String,required.password]; arguments []; default messa
              ge [Password is required],
               Error in object 'commandName': codes [typeMismatch.dateFrom.commandName,typeMismatch.dateFrom]; arguments []; def
              ault message [null]]
              I create in my messageBundle the next :
              typeMismatch.dateFrom=Incorrect date

              And when I enter in the jsp invalid date then show my custom message: Incorrect date

              Thank you very much

              Comment


              • #8
                Originally posted by a_subscriber View Post
                OK, I red tutorial and ....
                Code:
                public class DateValidator implements Validator {
                	/** Logger for this class and subclasses */
                	protected final Log logger = LogFactory.getLog(getClass());
                
                	@Override
                	public boolean supports(Class clazz) {
                		return UserView.class.equals(clazz);
                	}
                
                	@Override
                	public void validate(Object target, Errors errors) {
                		UserView userView = (UserView) target;
                		if (userView.getDateFrom() == null) {
                			errors.rejectValue("dateFrom", "incorrect.date", null,
                					"Incorrect date");
                		}
                	}
                }
                and all is work.Fine. BUT in my project I use VALANG for all my validations. And the question is: Why my custom function in valang not work.


                NullPointerException when try parse date in the valang custom function

                my config bean:
                HTML Code:
                	<bean id="customPropertyEditorRegistrar"
                		class="com.mycompany.myproject.domain.editors.CustomPropertyEditorRegistrar" />
                
                	<bean id="usersController" class="com.mycompany.myproject.web.controllers.UsersController">
                		<property name="validators" ref="userValidator" />
                		<constructor-arg>
                			<ref bean="customPropertyEditorRegistrar" />
                		</constructor-arg>
                	</bean>
                	
                my valang:
                HTML Code:
                	<bean id="userValidator" class="org.springmodules.validation.valang.ValangValidator">
                		<property name="customFunctions">
                			<map>
                				<entry key="userValidator" value="com.mycompany.myproject.domain.validators.UserValidator" />
                				<entry key="dateValidator" value="com.mycompany.myproject.domain.validators.DateValidator" />
                			</map>
                		</property>
                		<property name="valang">
                			<value>
                				<![CDATA[
                	                { lastName : ? HAS TEXT : 'Last name is required' : 'required.lastName' }
                	                { firstName : ? HAS TEXT : 'First name is required' : 'required.firstName' }
                	                { pin : ? HAS TEXT : 'PIN is required' : 'required.pin' } 
                	                { branchId : ? > 0 : 'Branch is required' : 'required.branch' }
                	                { login : ? HAS TEXT : 'Login is required' : 'required.login' }
                	                { password : ? HAS TEXT : 'Password is required' : 'required.password' }
                                           {dateFrom: dateValidator(?) = FALSE: 'Date is incorrect' : 'incorrect.date' }
                                           { login : ? HAS NO TEXT OR length(?) >30 OR userValidator(?) = FALSE: 'Login name already exist' : 'exist.login' }	                
                				]]>
                			</value>
                		</property>
                Code:
                my custom editor:
                public final class CustomPropertyEditorRegistrar implements
                		PropertyEditorRegistrar {
                
                	@Override
                	public void registerCustomEditors(PropertyEditorRegistry registry) {
                		registry.registerCustomEditor(java.util.Date.class,
                				new CustomDateEditor(new SimpleDateFormat("dd.MM.yyyy"), true));
                
                		registry.registerCustomEditor(Float.class, new CustomNumberEditor(
                				Float.class, true));
                	}
                }
                
                my controller:
                
                public class UsersController extends MultiActionController {
                	private final PropertyEditorRegistrar customPropertyEditorRegistrar;
                	
                	public UsersController(PropertyEditorRegistrar propertyEditorRegistrar) {
                		this.customPropertyEditorRegistrar = propertyEditorRegistrar;
                	}
                
                	@Override
                	protected void bind(HttpServletRequest request, Object command)
                			throws Exception {
                		UserView userView = (UserView) command;
                		ServletRequestDataBinder servletRequestDataBinder = createBinder(
                				request, userView);
                		customPropertyEditorRegistrar
                				.registerCustomEditors(servletRequestDataBinder);
                		servletRequestDataBinder.bind(request);
                		bindingResult = servletRequestDataBinder.getBindingResult();
                	}
                my validator:
                Code:
                public class DateValidator extends AbstractFunction {
                	private final Log logger = LogFactory.getLog(getClass());
                
                	public DateValidator(Function[] arguments, int line, int column) {
                		super(arguments, line, column);
                		definedExactNumberOfArguments(1);
                	}
                
                	@Override
                	public boolean isAutowireByName() {
                		return true;
                	}
                
                	@Override
                	protected Object doGetResult(Object arg0) throws Exception {
                		String date = getArguments()[0].getResult(arg0).toString();// throw NullPointerException
                		SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
                		try {
                			dateFormat.parse(date);
                		} catch (ParseException ex) {
                			return Boolean.TRUE;
                		}
                		return Boolean.FALSE;
                	}
                }
                but when I in my jsp enter invalid date then throw the next exception:
                Code:
                		SEVERE: nested exception is java.lang.NullPointerException at line 15, column 33
                org.springmodules.validation.valang.ValangException: nested exception is java.lang.NullPointerException at line 15, column 33
                        at org.springmodules.validation.valang.functions.FunctionTemplate.execute(FunctionTemplate.java:53)
                        at org.springmodules.validation.valang.functions.AbstractFunction.getResult(AbstractFunction.java:110)
                        at org.springmodules.validation.valang.predicates.GenericTestPredicate.evaluate(GenericTestPredicate.java:60)
                        at org.springmodules.validation.valang.predicates.BasicValidationRule.validate(BasicValidationRule.java:124)
                        at org.springmodules.validation.valang.ValangValidator.validate(ValangValidator.java:194)
                        at org.springframework.validation.ValidationUtils.invokeValidator(ValidationUtils.java:63)
                        at com.mycompany.myproject.web.controllers.UsersController.validate(UsersController.java:107)
                        at com.mycompany.myproject.web.controllers.UsersController.add(UsersController.java:171)
                        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                        at java.lang.reflect.Method.invoke(Method.java:597)
                        at org.springframework.web.servlet.mvc.multiaction.MultiActionController.invokeNamedMethod(MultiActionController.java:473)
                        at org.springframework.web.servlet.mvc.multiaction.MultiActionController.handleRequestInternal(MultiActionController.java:410)
                        at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
                        at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
                        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
                        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
                        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
                        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:511)
                        at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
                        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
                        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
                        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
                        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
                        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
                        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
                        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
                        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
                        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
                        at java.lang.Thread.run(Thread.java:619)
                Caused by: java.lang.NullPointerException
                        at com.mycompany.myproject.domain.validators.DateValidator.doGetResult(DateValidator.java:35)
                        at org.springmodules.validation.valang.functions.AbstractFunction$1.execute(AbstractFunction.java:112)
                        at org.springmodules.validation.valang.functions.FunctionTemplate.execute(FunctionTemplate.java:51)
                I found solution via valang. In the message bundle put e.g.:
                Code:
                typeMismatch.startDate=Start date is incorrect
                and... that is. Nothing more. Because in my controller I register custom editor

                Code:
                public final class CustomPropertyEditorRegistrar implements
                		PropertyEditorRegistrar {
                
                	@Override
                	public void registerCustomEditors(PropertyEditorRegistry registry) {
                		// It is expected that new PropertyEditor instances are created.
                		// Whenever you see a property of type Date.class, use this
                		// CustomDateEditor to convert the String from the request parameter
                		// into an instance of Date.class.
                		SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
                		sdf.setLenient(false); // else e.g. 56.34.2009 will be correct date
                		registry.registerCustomEditor(java.util.Date.class,
                				new CustomDateEditor(sdf, true));
                		registry.registerCustomEditor(Float.class, new CustomNumberEditor(
                				Float.class, true));
                		// you could register as many custom property editors as are required
                		// here...
                	}
                }
                the Spring automate find error message from message bundle. In this case Spring will be find typeMysmatch.startDate

                Comment


                • #9
                  Originally posted by a_subscriber View Post
                  HTML Code:
                  <fmt:formatDate value="${commandName.dateFrom}" patter="dd.MM.yyyy" />
                  
                  <form:input path="dateFrom" maxlength="10" readonly="${isReadOnly}" />
                  OK. <fmt:formatDate ... show formated date, but I need to show formated date in the <form:input paht="..." element.How show formated date (e.g "dd.MM.yyyy") in the <form:input element?

                  I understand how show custom error message in the <form:errors path="..".
                  OK. But how show formated NOT error message in the <form:input path="...". E.g. in the my <form:input element date show as "2009-10-23", but I need to show "23.10.2009".

                  Comment

                  Working...
                  X