1

I am using Spring mvc and thymeleaf. I am using a form which contains input fields. I would like that my controller accept empty fields. The problem that it says empty string. I tried with this

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

but it doesn't work.

the html page :

<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>

the controller :

@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";

    }
}

the entities: 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;
}




}

the error is :

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
3
  • 1
    Nothing in the logs? Commented Apr 22, 2017 at 7:36
  • java.lang.NullPointerException: null Commented Apr 22, 2017 at 7:39
  • Where? What line? Show full stacktrace. Or just read: What is a NullPointerException, and how do I fix it? Commented Apr 22, 2017 at 8:10

1 Answer 1

1

Instead of directly accessing parameters use @RequestParam annotation. Read official documentation here

Update your method parameters to:

@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";
}
Sign up to request clarification or add additional context in comments.

1 Comment

that is my fault,the parametrs should be parsed into double.thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.