I'm new to Spring and I've the following problem:
I wrote a class that looks like this
public class A{
private Strategy strat;
private int value;
public void setStrat(Strategy s){ this.strat= s;}
public Strategy getStrat(){ return strat;}
public int getValue(){return value;}
public vois setValue(int value){
if(! strat.isValidValue(a)){throw Exception(...);}
this.value=value;
}
}
So I've created Strategy to check my Value field in A. This works great. But now I'm working with Spring.
I've created a form in a jsp page that looks like this:
<form method="POST" action="addA.page" commandName="A">
<input name="value" value="${A.value}"/>
<input name="Strat" value="${A.strat}"/>
<input type="submit" value="Save">
<input type="submit" value="Cancel">
</form>
My code on the server to handle this add looks like this:
@RequestMapping(value="/addA", method={RequestMethod.POST})
public String addA(@ModelAttribute("A") A a){
service.addA(a);
return "redirect:/overview.page";
}
Since A exists of an int value and a non primitive datatype Strategy, I figured I should use a convertor
public class StratConverter implements Converter<String,Strategy>{
@Override
public Strategy convert(String s) {
return StrategyFactory.createStrategy(s);
}
}
I configured my xml file to use the converter:
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="Converters.StratConverter"/>
</list>
</property>
</bean>
That's all I did. I hoped it would work, but I'm having a problem. I'm getting a HTTP Status 400 - Bad Request, when submitting the form. The form works great When I remove the Strategy field (from form and object, but if I do that I've no longer validation). Does anybody know what I'm doing wrong? And also what if I setup everything correctly, how do I tell Spring it should first instantiate the Strat field and after that the A field? Because if you do it the other way arround you'll get a nullpointer.
Thanks in advance
Aucun commentaire:
Enregistrer un commentaire