Announcement Announcement Module
Collapse
No announcement yet.
JSP + Rest +ManyToOne = error 400 Bad Request Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JSP + Rest +ManyToOne = error 400 Bad Request

    Hi to all,

    I'm trying to submit a form for an entity that has a ManyToOne relationship.

    When I click to submit I receive error 400 Bad Request, but when I comment in my JSP the part of relationship the submit works ok. The attribute path in the form is pointing correctly to the relationship

    Here part of my Entity:

    Code:
    @Entity
    @Table(schema="sgn", name="prospect")
    public class Prospect implements Serializable {
    	private static final long serialVersionUID = 1L;
    
    ...
    //bi-directional many-to-one association to TipoProposta
    	@ManyToOne
    	@JoinColumn(name="id_tipo_proposta", insertable=false, updatable=false)
    	private TipoProposta tipoProposta;
    ...
    Here the part of Controller

    Code:
    @Controller
    @RequestMapping("/Demanda/**")
    public class ProspectController {
    
    @RequestMapping(value = "/Demanda/teste", method = RequestMethod.POST)
    	public String create(@ModelAttribute("prospect") Prospect prospect) {
    
    		prospectRepository.save(prospect);
    		return "redirect:/Demanda/list";
    	}
    
    @InitBinder
    	protected void initBinder(WebDataBinder binder) throws Exception {
    		binder.registerCustomEditor(List.class, "tipoProposta",
    				new CustomCollectionEditor(List.class) {
    					protected Object convertElement(Object element) {
    						if (element instanceof TipoProposta) {
    							return element;
    						}
    						if (element instanceof String) {
    							return tipoPropostaRepository.findOne(Long
    									.valueOf(element.toString()));
    						}
    						return null;
    					}
    				});
    	}
    Here the part of JSP

    Code:
    <c:url value="/Demanda/teste" var="teste"/>
    		<form:form method="POST" commandName="prospect" action="${teste}" cssClass="form-horizontal" name="form_prospect_registro" acceptCharset="UTF-8">
    			<form:hidden path="id" />
    ...
    <form:select id="selectTipoProposta" cssClass="span2" name="proposta_tipo" path="tipoProposta">
    		        		<c:forEach items="${listaTipoProposta}" var="tp" varStatus="i">
          						<c:choose>
    				       			 <c:when test="${tp.id == prospectHasTipoProposta[i.index].id}">
    				            		<form:option selected="selected" value="${tp.id}">
    				                		<c:out value="${tp.descricao}"/>
    				           			</form:option>
            						</c:when>
    
    	        					<c:otherwise>
    						            <form:option value="${tp.id}">
    						                <c:out value="${tp.descricao}"/>
    						           	</form:option>
    	        					</c:otherwise>
        						</c:choose>
          					</c:forEach>
    		        	</form:select>
    ...
    Please, can someone help me?

  • #2
    Why do people keep writing those ugly constructs?! Read the documentation...

    Code:
    <form:select cssClass="span2" path="tipoProposta" items="listaTipoProposata" itemValue="id" itemLabel="descricao"/>
    This (from the top of my head) should more or less do what you want. Your id and/or name attribute breaks proper binding.

    Also I don't really see how this is related to REST as there isn't much REST in your sample... Just a normal plain form submit...

    Comment


    • #3
      Thanks for your help.

      I also tried to do in this way, but I get the same error. Please look what happenes when I submit the page http://imm.io/109NB

      Comment


      • #4
        I know what happens en the problem, AFAIK, is in the definition of your select element. Not sure if my version works as I typed it from the top of my head.

        Enable DEBUG or TRACE logging on your application and check what is happening.

        Comment

        Working...
        X