Spring Custom formatter not call

395 views Asked by At

I want to do get my form and save it on db. I got nested object in my form, so I create a custom formatter but it's seems to never be called.

My JSP :

<form:form method="POST" modelAttribute="user" action="administration-utilisateurs">
<div class="row">
    <%-- User CUID --%>
    <div class="large-2 columns">
        <label><spring:message code="cuid.lbl"/>
            <form:input path="cuid" type="text" readonly="true"/>
        </label>
    </div>

    <%-- User Surname and FirstName --%>
    <div class="large-6 columns">
        <label><spring:message code="surname"/> et <spring:message code="firstname"/>
            <form:input path="name" type="text" readonly="true"/>
            <form:input path="email" type="text" readonly="true"/>
        </label>
    </div>

    <%-- Default Application --%>
    <div class="large-4 columns end">
        <label>Application par défaut
            <form:select path="applicationDefault">
               <form:options items="${applications}" itemValue="id" itemLabel="name"/>
            </form:select>
         </label>
     </div>
</div>
<br>
<div class="row">
   <div class="large-offset-4 large-4 columns">
       <input type="submit" class="button" value="<spring:message code="register"/>" />
    </div>
</div>
</form:form>

My controller :

  @Controller
public class administrationUserController
{
    /* Debug */
    private final static Logger logger = LoggerFactory.getLogger(ApplicationServiceImpl.class);

    @Resource
    IApplicationService applicationService;
    /*
    ###############################
    #                             #
    #            GET              #
    #                             #
    ###############################
    */
    @RequestMapping(value="/administration-utilisateurs", method=RequestMethod.GET)
    public String setForm(ModelMap model)
    {
        User user = new User();
        Ldap ldap = new Ldap();

        /* Get applications from database */
        model.addAttribute("applications", applicationService.findAll());

        logger.debug("application list " + model.get("applications"));

        /* Get user informations with LDAP */
        user = ldap.chercheRessourceCUID("IMSY7472");

        model.addAttribute("user", user);

        return "administrationUser";
    }
    /*
    ###############################
    #                             #
    #           POST              #
    #                             #
    ###############################
    */

    @Autowired
    private IApplicationService applicationServices;

    @RequestMapping(value = "/administration-utilisateurs", method = RequestMethod.POST)
    public String getForm(@ModelAttribute(value = "user") User userTemp,
            BindingResult result, ModelMap model) {
        User userData = new User();
        Ldap ldap = new Ldap();

        logger.debug("Nom : " + userTemp.getName()
                + ", Prénom : "+ userTemp.getFirstName()
                + ", CUID : " + userTemp.getCuid()
                + ", Email : " + userTemp.getEmail()
                + ", Appli : " + userTemp.getApplicationDefault());

        logger.debug("Call LDAP");
        userTemp.setApplicationDefault((Application) model
                .get("applicationList"));

        logger.debug("New rechearch on LDAP");
        userData = ldap.chercheRessourceCUID(userTemp.getCuid());

        logger.debug("Set application defautlt");
        userData.setApplicationDefault(userTemp.getApplicationDefault());

        logger.debug("Nom : " + userData.getName() + ", Prénom : "
                + userData.getFirstName() + ", CUID : " + userData.getCuid()
                + ", Email : " + userData.getEmail() + ", Appli : "
                + userData.getApplicationDefault());
        return "administrationUser";
    }
}

My custom formatter :

@Component
public final class ApplicationFormatter implements Formatter<Application> {
    /* Debug */
    private final static Logger logger = LoggerFactory.getLogger(IApplicationService.class);

    @Autowired
    private IApplicationService applicationServices;

    @Override
    public String print(Application application, Locale arg1) {
        logger.debug("Dans PRINT application formatter");
        return application.getName().toString();
    }

    @Override
    public Application parse(String id, Locale arg1)
            throws PersistenceException,ParseException, IllegalArgumentException  {
        logger.debug("Dans PARSE application formatter");
        return applicationServices.getApplication(Long.parseLong(id));
        // Else you can just return a new object by setting some values
        // which you deem fit.
    }
}

My application context :

<!-- We use annotation in form -->
    <mvc:annotation-driven conversion-service="conversionService"/>

    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
         <property name="formatters">
            <set>
                <!-- <ref bean="applicationFormatter"/> -->
                <bean class="com.orange.OSAC.tools.ApplicationFormatter" />
            </set>
        </property>
    </bean>

Output :

    15:07:31.917 [http-nio-8080-exec-9] DEBUG c.o.O.s.ApplicationServiceImpl - Nom : Sarrat, Prénom : null, CUID : IMSY7472, Email : [email protected], Appli : null
15:07:31.918 [http-nio-8080-exec-9] DEBUG c.o.O.s.ApplicationServiceImpl - Call LDAP
15:07:31.918 [http-nio-8080-exec-9] DEBUG c.o.O.s.ApplicationServiceImpl - New rechearch on LDAP
15:07:31.953 [http-nio-8080-exec-9] DEBUG c.o.O.s.ApplicationServiceImpl - Set application defautlt
15:07:31.953 [http-nio-8080-exec-9] DEBUG c.o.O.s.ApplicationServiceImpl - Nom : Sarrat, Prénom : Marc, CUID : IMSY7472, Email : [email protected], Appli : null

I think there is a problem with my custom formatter because there is no debug in console .. so i guess my methods are not called.

I'm trying for 3 days and still not working. Can someone help me ?

ps: sorry for my english :/

0

There are 0 answers