2017-04-22 8 views
1

私はSpring mvcとthymeleafを使用しています。 入力フィールドを含むフォームを使用しています。私のコントローラは空のフィールドを受け入れたいと思っています。それは空の文字列と言う問題。 私はこれを試しました空の文字列春のフォーム入力

@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

どこ?どの行?完全なスタックトレースを表示します。または、ちょうど読んで:[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

これは私のせいで、parametrsはdouble.thanks –

関連する問題