2014-03-18 164 views
0

我想發佈一些變量到我的控制器,然後輸入到數據庫。但我不斷收到錯誤,指出請求在語法上不正確。請求語法不正確

這裏是表格。

<form:form action="addLink" method="POST" commandName="link" > 
    <table id="productFinderTable"> 
     <tbody align="left"> 
      <tr align="left"> 
       <td colspan="2">Gender:<input style="width: 798px;" type="number" name="gender" value="1" /></td> 
      </tr> 
      <tr align="left"> 
       <td colspan="2">Garment:<input style="width: 798px;" type="number" name="garment" value="1" /></td> 
      </tr> 
      <tr align="left"> 
       <td colspan="2">Product Class:<input style="width: 798px;" type="number" name="productclass" value="1" /></td> 
      </tr> 
      <tr align="left"> 
       <td colspan="2">Sport:<input style="width: 798px;" type="text" name="sport" value="1" /></td> 
      </tr> 
      <tr align="left"> 
       <td colspan="2">Link:<input style="width: 798px;" type="text" name="link" value="http://www.google.ie" /></td> 
      </tr> 
      <tr align="left"> 
       <td colspan="2"><input type="submit" value="Submit" /></td> 
      </tr> 
     </tbody> 
    </table> 
    </form:form> 

實體鏈接

package com.wlgore.webapp.fab.pfsa.db; 

import java.io.Serializable; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.Table; 
import javax.validation.constraints.NotNull; 

@Entity 
@Table(name = "LINK") 
public class Link implements Serializable { 
    private static final long serialVersionUID = 1L; 

    @Id 
    @Column 
    private String hash; 

    @Column 
    @NotNull 
    private String link; 

    @Column 
    @NotNull 
    private Integer garment; 

    @Column 
    @NotNull 
    private Integer productclass; 

    @Column 
    @NotNull 
    private Integer gender; 

    @Column 
    private String sport; 

    /** 
    * @return the link 
    */ 
    public String getLink() { 
     return link; 
    } 

    /** 
    * @param link the link to set 
    */ 
    public void setLink(String link) { 
     this.link = link; 
    } 

    /** 
    * @return the garment 
    */ 
    public Integer getGarment() { 
     return garment; 
    } 

    /** 
    * @param garment the garment to set 
    */ 
    public void setGarment(Integer garment) { 
     this.garment = garment; 
    } 

    /** 
    * @return the productclass 
    */ 
    public Integer getProductclass() { 
     return productclass; 
    } 

    /** 
    * @param productclass the productclass to set 
    */ 
    public void setProductclass(Integer productclass) { 
     this.productclass = productclass; 
    } 

    /** 
    * @return the gender 
    */ 
    public Integer getGender() { 
     return gender; 
    } 

    /** 
    * @param gender the gender to set 
    */ 
    public void setGender(Integer gender) { 
     this.gender = gender; 
    } 

    /** 
    * @return the sport 
    */ 
    public String getSport() { 
     return sport; 
    } 

    /** 
    * @param sport the sport to set 
    */ 
    public void setSport(String sport) { 
     this.sport = sport; 
    } 

    /** 
    * @return the hash 
    */ 
    public String getHash() { 
     return hash; 
    } 

    /** 
    * @param hash the hash to set 
    */ 
    public void setHash(String hash) { 
     this.hash = hash; 
    } 

} 

控制器

@RequestMapping(value = "/addLink", method = RequestMethod.POST) 
public @ResponseBody String addNewCommand(@ModelAttribute("link") Link link, BindingResult result)throws Exception{ 

     /*** 
     * Here we receive the relvant variables 1.Gender 2.Garment 3.Product 
     * Class 4.Sport 5.Link 
     * */ 

     try { 
      if (result.hasErrors()) { 
       throw new Exception("Invalid post!"); 
      } else { 
       String hash = HashFunction.gen(8); 
       Link linkExists = dao.getById(hash); 
       while (null != linkExists) { 
        hash = HashFunction.gen(8); 
        linkExists = dao.getById(hash); 
       } 
      System.out.println(hash); 
      link.setHash(hash); 
      dao.create(link); 
      return new ObjectMapper().writeValueAsString("Test Message"); 
      } 
     } catch (Exception e) { 
      return new ObjectMapper().writeValueAsString(new ErrorJSON(e)); 
     } 
    } 
+0

將您的日誌級別設置爲DEBUG並檢查日誌。 –

+0

您可以發佈確切的錯誤以及生成的SQL(在配置中將'hibernate.show_sql'設置爲'true',或者參見[here](http://www.javalobby.org/java/forums/t44119)。 HTML))? –

+0

它適合我。 –

回答

-1

我不認爲@ModelAttribute這裏是正確的註釋。要使用BindingResult工作,您還需要在表單bean上使用@Valid

+0

他們會在哪裏使用'@ RequestParam'? –

+0

不通。不過,你需要'@ Valid'來爲'BindingResult'自動填充。 –