Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC - Problem with form:select returning null. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC - Problem with form:select returning null.

    Hey guys, here is the problem:

    My 'form:select' returns null.

    My bean Origem has a TipoOrigem. And I use a Select to fill this value. The rest of the form works.

    Any tips?


    Here are the codes:

    My form:

    HTML Code:
    <c:url value="/origem" var="listar" />
    <form:form class="form-horizontal" name="form_origem_registro" commandName="origem" action="${listar}" method="POST">
        	<form:hidden path="id" />
        <fieldset>
            <legend>Cadastrar Projeto - Nova Origem</legend>
            <div class="row-fluid show-grid">
    		<hr />
    		    <div class="control-group">
    		      <label class="control-label" for="selectOrigemDemanda">Origem da Demanda</label>
    		    </div>    
    		
    		  <div class="span4"><!-- right column -->
    		    <div class="control-group">
    		      <label class="control-label" for="selectTipoOrigem">Tipo de Origem</label>
    		      <div class="controls">
    		        <form:select path="tipoOrigem" id="selectTipoOrigem" name="origem_tipo">
    		        	<form:option value="" label="--- Selecione ---"/>
    		        	<form:options required="true" items="${listaTipoOrigem}" itemLabel="descricao" itemValue="id"/>
    		        </form:select>
    		      </div>
    		     </div>
    		    </div>
    		    <div class="control-group">
    		      <label class="control-label" for="inputDescricaodaDemanda">Descrição da Origem</label>
    		      <div class="controls">
    		        <form:input type="text" id="inputDescricaodaOrigem" placeholder="Descrição da Origem" path="descricao"></form:input>
    		      </div>
    		    </div>
    		</div><!-- show-grid-open -->
    
        <div class="form-actions">
          <button type="submit" class="btn btn-primary">Salvar mudanças</button>
          <button type="button" class="btn">Cancelar</button>
        </div>
          
        </fieldset>
      </form:form>
    My Controller:

    Code:
    @Controller
    @RequestMapping(value = "/origem/**")
    public class OrigemController {
    	@Autowired
    	private OrigemRepository origemRepository;
    	
    	@Autowired
    	private TipoOrigemRepository tipoOrigemRepository;
    	
    	@RequestMapping(value = "/origem", method = RequestMethod.GET)
    	public String list(ModelMap modelMap) {
    		List<Origem> resultados = origemRepository.findAll();
    			modelMap.addAttribute("origemLista", resultados);
    		return "origem/list";
    	}
    
    	@RequestMapping(value = "/origem/{id}", method = RequestMethod.DELETE)
    	public String delete(@PathVariable("id") Long id) {
    		origemRepository.delete(origemRepository.findOne(id));
    		return "redirect:/origem";
    
    	}
    
    	@RequestMapping(value = "/origem/form", method = RequestMethod.GET)
    	public String form(ModelMap modelMap) {
    		List<TipoOrigem> listaTpOrigem = tipoOrigemRepository.findAll();
    		modelMap.addAttribute("origem", getOrigem());
    		modelMap.addAttribute("listaTipoOrigem", listaTpOrigem);
    		return "origem/form";
    	}
    	
    	@RequestMapping(value = "/origem/{id}", method = RequestMethod.GET)
    		public String update(@PathVariable("id") Long id, ModelMap modelMap) {
    		modelMap.addAttribute("origem", origemRepository.findOne(id));
    		return "origem/form";
    	}
    
    	@RequestMapping(value = "/origem", method = RequestMethod.POST)
    	public String create(@ModelAttribute("origem") @Valid Origem origem, BindingResult result, Model model) {
    		System.out.println("++======Persistindo A Origem=========");
    		System.out.println(origem.getDescricao());
    		
    		System.out.println(origem.getTipoOrigem().getDescricao()); // <<<<<< RETURNS NULL!
    		
    //		tipoOrigemRepository.save(origem.getTipoOrigem());
    		origemRepository.save(origem);
    		return "redirect:/origem";
    	}
    	
    	@ModelAttribute("origem")
    	public Origem getOrigem(){
    		return new Origem();
    	}
    	@ModelAttribute("tipoOrigem")
    	public TipoOrigem getTipoOrigem(){
    		return new TipoOrigem();
    	}
    
    //	public void setTipoOrigem(TipoOrigem tipoOrigem) {
    //		this.tipoOrigem = tipoOrigem;
    //	}
    	
    }
    My Formatter:

    Code:
    @Component
    public class TipoOrigemFormatter  implements Formatter<TipoOrigem>{
    
    	private TipoOrigemService tipoOrigemService = null;
    	@Autowired
    	public void OrigemTypeFormatter(TipoOrigemService tipoOrigemService) {
    		this.tipoOrigemService = tipoOrigemService;
    	}
    
    	@Override
    	public String print(TipoOrigem tipoOrigem, Locale locale) {
    		System.out.println("FORMAAAATTTEEEEERRR");
    		return tipoOrigem.getDescricao();
    	}
    
    	@Override
    	public TipoOrigem parse(String id, Locale locale) throws ParseException {
    		System.out.println("***************************************************");
    		System.out.println("***************************************************");
    		System.out.println("************Passei no formatter!!!!****************");
    		System.out.println("***************************************************");
    		System.out.println("***************************************************");
    		
    		TipoOrigem tipoOrigem = this.tipoOrigemService.findById(Long.parseLong(id));
    		
    		return tipoOrigem;
    	}
    }
    My ApplicationContext:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:mvc="http://www.springframework.org/schema/mvc"
    	xsi:schemaLocation="http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
    		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.2.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    
    	<!-- Activates scanning of @Autowired -->
    	<context:annotation-config />
    
    	<!-- Activates scanning of @Repository and @Service -->
    	<!-- <context:component-scan base-package="br.com.fourlinux.sgn.core"> -->
    	<!-- <context:include-filter type="aspectj" expression="br.com.fourlinux.sgn.core.*.*" 
    		/> -->
    	<!-- </context:component-scan> -->
    	
    	<mvc:annotation-driven/>
    	
    	<context:component-scan base-package="br.com.fourlinux.sgn.core.repository" />
    
    	<context:component-scan base-package="br.com.fourlinux.sgn.core.services" />
    
    	<jpa:repositories base-package="br.com.fourlinux.sgn.core.repository"
    		entity-manager-factory-ref="entityManagerFactory" />
    
    	<bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator" />
    
    	<!-- Database -->
    	<bean id="datasource"
    		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="org.postgresql.Driver" />
    		<property name="url" value="${db.url}" />
    		<property name="username" value="${db.username}" />
    		<property name="password" value="${db.password}" />
    	</bean>
    
    	<!-- Alterar para JTA quando for para produção! -->
    	<!-- <jee:jndi-lookup jndi-name="java:/sgn-ds" id="datasource" resource-ref="true"/> -->
    
    	<!-- Entity Manager -->
    	<bean id="entityManagerFactory"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="dataSource" ref="datasource" />
    		<property name="persistenceUnitName" value="sgn-core" />
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="showSql" value="true" />
    				<property name="generateDdl" value="true" />
    			</bean>
    		</property>
    		<property name="packagesToScan" value="br.com.fourlinux.sgn.core.model" />
    		<property name="jpaDialect">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    		</property>
    	</bean>
    
    	<bean id="entityManagerFactoryJbpm"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="persistenceUnitName" value="org.jbpm.task" />
    	</bean>
    
    	<!-- Transaction Manager -->
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    	</bean>
    
    	<!-- Carregar os dados application.properties na memória -->
    	<context:property-placeholder location="classpath:application.properties" />
    
    	<context:component-scan base-package="br.com.fourlinux.sgn.core.util.formatters" />
    
    	<bean id="conversionService"
    		class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    		<property name="formatters">
    			<set>
    				<ref bean="tipoOrigemFormatter" />
    			</set>
    		</property>
    	</bean>
    
    	<mvc:annotation-driven conversion-service="conversionService" />
    	<tx:annotation-driven />
    
    </beans>
    Thanks in advance!
Working...
X