2017-04-22 48 views
1

我使用春天mvc和百里香。 我正在使用包含輸入字段的表單。我希望我的控制器能夠接受空的字段。它表示空字符串的問題。 我試着用這個空的字符串在春天輸入形式

@InitBinder  
    public void initBinder(WebDataBinder binder) { 
     binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); 
    } 

,但它不工作。

的html頁面:

<form th:action="@{/ajouterUniversite}" method="post" id="ajouter" style="display: none"> 

             <div> 

              <label>nom:</label> 
              <input type="text" name="nom" /> 

             </div> 
             <hr/> 
             <label>région:</label> 
             <input type="text" name="region" /> 
             <hr/> 

             <b>Choisir les branches dans cette université</b> 
             <div> 
               <div class="checkbox"> 
                 <label><input type="checkbox" value="" onchange="document.getElementById('math').style.display='block'"/>mathématique</label> 
               </div> 
                <div class="checkbox"> 
                 <label><input type="checkbox" value="" onchange="document.getElementById('science').style.display='block'"/>Science</label> 
                </div> 
                <div class="checkbox "> 
                 <label><input type="checkbox" value="" onchange="document.getElementById('info').style.display='block'" />Info</label> 
               </div> 
               <div class="checkbox "> 
                 <label><input type="checkbox" value="" onchange="document.getElementById('lettre').style.display='block'" />Lettre</label> 
               </div> 
               <div class="checkbox "> 
                 <label><input type="checkbox" value="" onchange="document.getElementById('eco').style.display='block'" />Eco</label> 
               </div> 
             </div> 


             <div id="math" style="display: none" > 

              <label >entrer le score du bac math :</label> 
              <input type="number" name="scoreMath" min="120"/> 

             </div> 
             <hr/> 


             <div id="science" style="display: none" > 

              <label >entrer le score du bac science :</label> 
              <input type="number" name="scoreScience" min="95" /> 

             </div> 
             <hr/> 


             <div id="info" style="display: none"> 

              <label >entrer le score du bac info :</label> 
              <input type="number" name="scoreInfo" min="65" /> 

             </div> 
             <hr/> 
             <div id="lettre" style="display: none"> 

              <label >entrer le score du bac lettre :</label> 
              <input type="number" name="scoreLettre" min="50" /> 

             </div> 
             <hr/> 

             <div id="eco" style="display: none"> 

              <label >entrer le score du bac eco :</label> 
              <input type="number" name="scoreEco" min="55" /> 

             </div> 

             <button type="submit" class="btn btn-primary">Save</button> 

             </form> 

控制器:

@Controller

public class BanqueController { 
    @Autowired 
    private IOrient or; 





    @InitBinder 
     public void initBinder(WebDataBinder binder) { 

     binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); 


     } 

    @RequestMapping("/operations") 
    public String index() 
    { 
     return "comptes"; 
    } 





    @RequestMapping("/authentifier")  
    public String authentifier() 
    { 

    return "authentifier"; 

    } 

    @RequestMapping("/pageAdmin") 
    public String pageAdmin(Model model, String uname,String psw) 

    { 
     if (uname.equals("admin") && psw.equals("admin")) return "admin"; 
     else 
      { 
      model.addAttribute("exception","vérifier les paramètres !!"); 
      return "authentifier"; 
      } 



    } 

    @RequestMapping(value="/ajouterUniversite",method=RequestMethod.POST) 
    public String ajoutUniver(Model model,String nom,String region,String scoreMath,String scoreScience,String scoreEco,String scoreInfo,String scoreLettre) 
    { 

     model.addAttribute("ok","cv"); 
     Universite u=new Universite(nom,region); 
     or.ajouterUniversite(nom, region, Double.parseDouble(scoreMath), Double.parseDouble(scoreScience), Double.parseDouble(scoreInfo), Double.parseDouble(scoreLettre), Double.parseDouble(scoreEco)); 
     return "admin"; 

    } 
} 

實體: Universite.class

@Entity 
public class Universite implements Serializable { 
    @Id @GeneratedValue 
    private Long id_universite; 
    private String nom; 
    private String region; 
    public Universite() { 
     super(); 
    } 
    public Universite(String nom, String region) { 
     super(); 
     this.nom = nom; 
     this.region = region; 
    } 



    public String getNom() { 
     return nom; 
    } 
    public void setNom(String nom) { 
     this.nom = nom; 
    } 
    public String getRegion() { 
     return region; 
    } 
    public void setRegion(String region) { 
     this.region = region; 
    } 
    public Long getId_universite() { 
     return id_universite; 
    } 
    public void setId_universite(Long id_universite) { 
     this.id_universite = id_universite; 
    } 



} 

Score.java:

@Entity 
public class Score { 
@Id @GeneratedValue 
private Long code_score; 

private double scoreMath; 

private double scoreScience; 

private double scoreInfo; 

private double scoreEco; 

private double scoreLettre; 
@ManyToOne 
@JoinColumn(name="code_universite") 
private Universite universite; 



public double getScoreMath() { 
    return scoreMath; 
} 
public void setScoreMath(double scoreMath) { 
    this.scoreMath = scoreMath; 
} 
public double getScoreScience() { 
    return scoreScience; 
} 
public void setScoreScience(double scoreScience) { 
    this.scoreScience = scoreScience; 
} 
public double getScoreInfo() { 
    return scoreInfo; 
} 
public void setScoreInfo(double scoreInfo) { 
    this.scoreInfo = scoreInfo; 
} 
public double getScoreEco() { 
    return scoreEco; 
} 
public void setScoreEco(double scoreEco) { 
    this.scoreEco = scoreEco; 
} 
public double getScoreLettre() { 
    return scoreLettre; 
} 
public void setScoreLettre(double scoreLettre) { 
    this.scoreLettre = scoreLettre; 
} 
public Universite getUniversite() { 
    return universite; 
} 
public void setUniversite(Universite universite) { 
    this.universite = universite; 
} 
public Score() { 
    super(); 
} 
public Score(double scoreMath, double scoreScience, double scoreInfo, double scoreEco, double scoreLettre, 
     Universite universite) { 
    super(); 
    this.scoreMath = scoreMath; 
    this.scoreScience = scoreScience; 
    this.scoreInfo = scoreInfo; 
    this.scoreEco = scoreEco; 
    this.scoreLettre = scoreLettre; 
    this.universite = universite; 
} 
public Long getCode_score() { 
    return code_score; 
} 
public void setCode_score(Long code_score) { 
    this.code_score = code_score; 
} 




} 

錯誤是:

Whitelabel Error Page 

This application has no explicit mapping for /error, so you are seeing this as a fallback. 

Sat Apr 22 08:19:00 CEST 2017 
There was an unexpected error (type=Internal Server Error, status=500). 
empty String 
+1

沒有在日誌? –

+0

java.lang.NullPointerException:null –

+0

Where?什麼線?顯示完整的堆棧跟蹤。或者只是閱讀:[什麼是NullPointerException,以及如何解決它?](http://stackoverflow.com/q/218384/5221149) – Andreas

回答

1

而不是直接訪問參數使用@RequestParam註釋。 閱讀官方文檔here

更新你的方法參數:

@RequestMapping(value="/ajouterUniversite",method=RequestMethod.POST) 
    public String ajoutUniver(Model model,@RequestParam(required = false) String nom,@RequestParam(required = false) String region,@RequestParam(required = false) String scoreMath, @RequestParam(required = false) String scoreScience, @RequestParam(required = false) String scoreEco, @RequestParam(required = false) String scoreInfo,@RequestParam(required = false) String scoreLettre) 
    { 

    model.addAttribute("ok","cv"); 
    Universite u=new Universite(nom,region); 
    or.ajouterUniversite(nom, region, Double.parseDouble(scoreMath), Double.parseDouble(scoreScience), Double.parseDouble(scoreInfo), Double.parseDouble(scoreLettre), Double.parseDouble(scoreEco)); 
    return "admin"; 
} 
+0

這是我的錯,參數應該被解析爲double.thanks –