Witam, dopiero zacząłem się uczyć Spring i chciałem sobie napisać prostą aplikację webową która będzie obliczała BMI wartości z jsp przekazuje zwykłym form'em
<form method="get" action="${pageContext.request.contextPath}/bmi">
<table>
<tr>
<td>Waga (kg):</td>
<td><input name="weight" type="double" /></td>
</tr>
<tr>
<td>Wzrost (m):</td>
<td><input name="height" type="double" /></td>
</tr>
<tr>
<td></td>
<td><input value="Oblicz" type="submit" /></td>
</tr>
</table>
</form>
Kontroler w Spring
@RequestMapping(value="/bmi", method = RequestMethod.GET)
public ModelAndView calculateBmi(@ModelAttribute("bmi") ValueBmi vbmi ){
ModelAndView mv = new ModelAndView();
System.out.println("wzrost:" + vbmi.getHeight() + " waga: " + vbmi.getWeight());
result = vbmi.getWeight() / (vbmi.getHeight()*vbmi.getHeight());
result *=100;
result = Math.round(result);
result /= 100;
mv.addObject("result",result);
mv.addObject("height",vbmi.getHeight());
mv.addObject("weight",vbmi.getWeight());
return mv;
}
Klasa ValueBmi
public class ValueBmi {
private double height;
private double weight;
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
Chciałbym się dowiedzieć czy jest to dobrze napisane szczególnie kontroler i czy moge w taki sposób to wykonywać? Dlaczego jak forma zmienię na metodę post i w kontrolerze amienie na post to dostaję błąd?