Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: How get Date object after convert

  1. #1
    Join Date
    Aug 2009
    Posts
    167

    Default How get Date object after convert

    my config. bean:
    HTML Code:
    	<bean id="dateEditor"
    		class="org.springframework.beans.propertyeditors.CustomDateEditor">
    		<constructor-arg>
    			<bean class="java.text.SimpleDateFormat">
    				<constructor-arg value="dd.MM.yyyy" />
    			</bean>
    		</constructor-arg>
    		<constructor-arg value="true" />
    	</bean>
    
    	<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
    		<property name="customEditors">
    			<map>
    				<entry key="java.util.Date">
    					<ref local="dateEditor" />
    				</entry>
    			</map>
    		</property>
    	</bean>
    
    
    	<bean id="usersController" class="com.mycompany.myproject.web.controllers.UsersController">
    		<property name="dateEditor" ref="dateEditor" />
    	</bean>
    my controller:
    Code:
    public class UsersController extends MultiActionController {
    	private CustomDateEditor dateEditor;
    
    public void setDateEditor(CustomDateEditor dateEditor) {
    		this.dateEditor = dateEditor;
    	}
    
    @Override
    	protected void bind(HttpServletRequest request, Object command)
    			throws Exception {
    		ServletRequestDataBinder servletRequestDataBinder = createBinder(
    				request, command);
    		servletRequestDataBinder.registerCustomEditor(java.util.Date.class,
    				"dateFrom", dateEditor);
    		servletRequestDataBinder.registerCustomEditor(java.util.Date.class,
    				"dateTo", dateEditor);
    		servletRequestDataBinder.bind(request);
    		bindingResult = servletRequestDataBinder.getBindingResult();
    	}
    
    public ModelAndView add(HttpServletRequest request,
    			HttpServletResponse response, UserView userView) throws Exception {
     // Here I need Date objects from fields "dateFrom" and "dateTo"
    ...
    No errors, but how I can get Date objects in my method "add" ?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    CustomEditorConfigurer is only used for reading/parsing the application context NOT for your controllers. Next to that NEVER inject a PropertyEditor, PropertyEditors are by design not thread safe, create a new one in your controller.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Aug 2009
    Posts
    167

    Default

    Quote Originally Posted by Marten Deinum View Post
    CustomEditorConfigurer is only used for reading/parsing the application context NOT for your controllers. Next to that NEVER inject a PropertyEditor, PropertyEditors are by design not thread safe, create a new one in your controller.
    How get date object after parse?

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    They are part of your form object (assumingbly UserView). Simply get them...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Aug 2009
    Posts
    167

    Default

    Quote Originally Posted by Marten Deinum View Post
    They are part of your form object (assumingbly UserView). Simply get them...
    When then the date not insert to DB?
    my controller:
    Code:
    public class UsersController extends MultiActionController {
    @Override
    	protected void bind(HttpServletRequest request, Object command)
    			throws Exception {
    		UserView userView = (UserView) command;
    		ServletRequestDataBinder servletRequestDataBinder = createBinder(
    				request, userView);
    		servletRequestDataBinder.registerCustomEditor(Date.class,
    				new CustomDateEditor(new SimpleDateFormat("dd.MM.yyyy"),
    						true));
    		servletRequestDataBinder.bind(request);
    		bindingResult = servletRequestDataBinder.getBindingResult();
    		logger.info("dateFrom="+userView.getDateFrom());
    	}
    log:
    Code:
    Aug 31, 2009 11:40:08 AM com.mycompany.myproject.web.controllers.UsersController bind
    INFO: dateFrom=Sun Aug 30 00:00:00 EEST 2009
    my dao obejct:
    Code:
    public class JdbcUserDao extends SimpleJdbcDaoSupport implements UserDao {
    private static final String USER_INSERT = "INSERT INTO users "
    			+ "(FIRSTNAME,LASTNAME,PIN,EMAIL,MOBILE,BRANCH_ID,LOGIN,PASSWORD,CPNL,"
    			+ "DISABLED_PERMANENTLY,DISABLED_FROM,DISABLED_TO,PLC,NIL) "
    			+ "VALUES (:firstName,:lastName,:pin,:email,:mobile,:branch,:login,:password,:cpnl,"
    			+ ":disabledPermanently,:dateFrom,:dateTo,:plc,:nil)";
    @Override
    	public void add(User user) {
    		SqlParameterSource parameterSource = new BeanPropertySqlParameterSource(
    				user);
    		Branch branch = (Branch) parameterSource.getValue("branch");
    		Map<String, Object> parameters = new HashMap<String, Object>();
    		parameters.put("id", user.getId());
    		parameters.put("firstName", user.getFirstName());
    		parameters.put("lastName", user.getLastName());
    		parameters.put("pin", user.getPin());
    		parameters.put("email", user.getEmail());
    		parameters.put("mobile", user.getMobile());
    		parameters.put("branch", branch.getId());
    		parameters.put("login", user.getLogin());
    		parameters.put("password", user.getPassword());
    		parameters.put("cpnl", user.getCpnl());
    		parameters.put("disabledPermanently", user.getDisabledPermanently());
    		parameters.put("dateFrom", user.getDateFrom());
    		parameters.put("dateTo", user.getDateTo());
    		parameters.put("plc", user.getPlc());
    		parameters.put("nil", user.getNil());
    		parameterSource = new MapSqlParameterSource(parameters);
                         logger.info("dateFrom=" + user.getDateFrom());
    		logger.info("dateTo=" + user.getDateTo())
    		getSimpleJdbcTemplate().update(USER_INSERT, parameterSource);
    log:
    Code:
    Aug 31, 2009 11:40:08 AM md.deeplace.sas.repository.JdbcUserDao add
    INFO: dateFrom=Sun Aug 30 00:00:00 EEST 2009
    Aug 31, 2009 11:40:08 AM md.deeplace.sas.repository.JdbcUserDao add
    INFO: dateTo=Mon Aug 31 00:00:00 EEST 2009

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    I would think something is wrong with your binding (as I stated earlier you might want to read a book instead of asking 4 questions a day...). Check your types (are both types class and used to configure binding the same Date class (java.util or javax.sql) ). Are your fields properly bound from/to the jsp.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Aug 2009
    Posts
    167

    Default

    Quote Originally Posted by Marten Deinum View Post
    CustomEditorConfigurer is only used for reading/parsing the application context NOT for your controllers. Next to that NEVER inject a PropertyEditor, PropertyEditors are by design not thread safe, create a new one in your controller.
    Where I get more information about thread safe?

    This solution is a thread safe?
    bean:
    HTML Code:
    	<bean id="dateFormat" class="java.text.SimpleDateFormat">
    		<constructor-arg value="dd.MM.yyyy" />
    	</bean>
    
    	<bean id="usersController" class="com.mycompany.myproject.web.controllers.UsersController">
    		<property name="dateFormat" ref="dateFormat" />
    	</bean>
    my controller:
    Code:
    public class UsersController extends MultiActionController {
    	private DateFormat dateFormat;
    public void setDateFormat(DateFormat dateFormat) {
    		this.dateFormat = dateFormat;
    	}
    
    @Override
    	protected void bind(HttpServletRequest request, Object command)
    			throws Exception {
    		UserView userView = (UserView) command;
    		ServletRequestDataBinder servletRequestDataBinder = createBinder(
    				request, userView);
    		servletRequestDataBinder.registerCustomEditor(Date.class,
    				new CustomDateEditor(dateFormat, true));
    		servletRequestDataBinder.bind(request);
    		bindingResult = servletRequestDataBinder.getBindingResult();
    	}

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    This solution is a thread safe?
    No. Formatters aren't threadsafe either, so you really need to construct those objects inside your controller.

    Information regarding thread safety, i suggest the JavaBeans specification or just learn the implementation. Classes which keep state are basically not thread safe.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9
    Join Date
    Aug 2009
    Posts
    167

    Default

    Quote Originally Posted by Marten Deinum View Post
    No. Formatters aren't threadsafe either, so you really need to construct those objects inside your controller.

    Information regarding thread safety, i suggest the JavaBeans specification or just learn the implementation. Classes which keep state are basically not thread safe.
    OK here the threadsafe solution:
    Code:
    @Override
    	protected void bind(HttpServletRequest request, Object command)
    			throws Exception {
    		UserView userView = (UserView) command;
    		ServletRequestDataBinder servletRequestDataBinder = createBinder(
    				request, userView);
    		servletRequestDataBinder.registerCustomEditor(Date.class,
    				new CustomDateEditor(new SimpleDateFormat("dd.MM.yyyy"),
    						true));
    		servletRequestDataBinder.bind(request);
    		bindingResult = servletRequestDataBinder.getBindingResult();
    	}
    And if I have 50 controllers I need to every controller write servletRequestDataBinder.registerCustomEditor(Date .class,
    new CustomDateEditor(new SimpleDateFormat("dd.MM.yyyy").
    .
    But suppose that tomorrow I need to change date pattern from dd.MM.yyyy to MM-dd-yyyy. And I need to change and recompile 50 controllers!!! I think it's not good. The ideal solution is to make change ONLY IN ONE PLACE without recompile my code

  10. #10
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Sigh... As I suggested about 5 times, read a book, read the reference guide... Instead you are complaining, asking x questions a day, sometimes the same questions which doesn't really help you getting more help from the community.

    Again I suggest the reference guide, scroll down to section 5.4.2.1.1.

    Also instead of using the old, hard to get working MultiActionController I suggest a annotation based controller which is easier to use/configure.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •