reegz
May 4th, 2008, 01:37 PM
Hi everyone
I need some help on a problem that I have.
I have a form which will accept input(entity id) and upon the user hitting the 'search' button will perform a database query. If results are found (of type <Entity>), they are added onto the command object and then the controller will forward to the original jsp and display the results. The user clicks the next button and then the controller adds some info from the command object to the session and forwards to the next page.
The problem is that when I try pulling the entity id from the command object, I get a NullPointerException.
I've figured that since the id is not displayed on the results page it is not added to the <Entity> object on the command object.
My question then is, how do I ensure that the data on the object remains there without having to add it to the page (as a hidden input type). Is it possible to bind the object to the controller for the duration of the users session?
Here are my classes/configs..
The problem happens in the 'next' method. When I try getting the entity from the command object... NPE is thrown
public class CaptureIdController extends MultiActionController {
private EntityDao dao;
protected BindException bindObject(HttpServletRequest request,
Object command, Validator validator) throws Exception {
ServletRequestDataBinder binder = createBinder(request, command);
binder.bind(request);
BindException errors = new BindException(command,
getCommandName(command));
if (validator.supports(command.getClass())) {
ValidationUtils.invokeValidator(validator, command, errors);
}
return errors;
}
public ModelAndView back(HttpServletRequest request,
HttpServletResponse response, CaptureIdForm command) {
ModelAndView mv = new ModelAndView("/capture_id");
try {
command.setEntityDetails(null);
command.setPruId("");
} catch (Exception e) {
//
}
mv.addObject("captureIdForm", command);
return mv;
}
public ModelAndView findEntity(HttpServletRequest request,
HttpServletResponse response, CaptureIdForm command) {
ModelAndView mv = new ModelAndView("/capture_id");
BindException errors = null;
try {
errors = bindObject(request, command, new CaptureIdValidator());
if (!errors.hasErrors()) {
Entity tmpEntity = dao.getEntityDetails(command.getId());
if (tmpEntity != null) {
command.setEntityDetails(tmpEntity);
} else {
errors.reject("errors.id.invalid");
}
}
mv.addObject("errors", errors);
mv.addObject("captureIdForm", command);
} catch (Exception e) {
if (errors != null) {
errors.reject("errors.unspecified");
}
}
return mv;
}
public ModelAndView next(HttpServletRequest request, HttpServletResponse response,
CaptureIdForm command) throws Exception {
request.getSession().setAttribute("id",
command.getEntityDetails().getEntityNo());
return new ModelAndView(new RedirectView("CaptureDetails.do"));
}
public void setDao(EntityDao dao) {
this.dao = dao;
}
}
heres an extract from my springapp-servlet.xml. only the relevant bits are added.
<bean id="captureIdController"
class="CaptureIdController">
<property name="methodNameResolver">
<ref bean="methodNameResolver" />
</property>
<property name="dao">
<ref bean="entityDao"/>
</property>
</bean>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlH andlerMapping">
<property name="mappings">
<props>
<prop key="CaptureId.do">captureIdController</prop>
<prop key="CaptureDetails.do">captureDetailsController</prop>
</props>
</property>
</bean>
Would appreciate any advise on this issue.
thanks
I need some help on a problem that I have.
I have a form which will accept input(entity id) and upon the user hitting the 'search' button will perform a database query. If results are found (of type <Entity>), they are added onto the command object and then the controller will forward to the original jsp and display the results. The user clicks the next button and then the controller adds some info from the command object to the session and forwards to the next page.
The problem is that when I try pulling the entity id from the command object, I get a NullPointerException.
I've figured that since the id is not displayed on the results page it is not added to the <Entity> object on the command object.
My question then is, how do I ensure that the data on the object remains there without having to add it to the page (as a hidden input type). Is it possible to bind the object to the controller for the duration of the users session?
Here are my classes/configs..
The problem happens in the 'next' method. When I try getting the entity from the command object... NPE is thrown
public class CaptureIdController extends MultiActionController {
private EntityDao dao;
protected BindException bindObject(HttpServletRequest request,
Object command, Validator validator) throws Exception {
ServletRequestDataBinder binder = createBinder(request, command);
binder.bind(request);
BindException errors = new BindException(command,
getCommandName(command));
if (validator.supports(command.getClass())) {
ValidationUtils.invokeValidator(validator, command, errors);
}
return errors;
}
public ModelAndView back(HttpServletRequest request,
HttpServletResponse response, CaptureIdForm command) {
ModelAndView mv = new ModelAndView("/capture_id");
try {
command.setEntityDetails(null);
command.setPruId("");
} catch (Exception e) {
//
}
mv.addObject("captureIdForm", command);
return mv;
}
public ModelAndView findEntity(HttpServletRequest request,
HttpServletResponse response, CaptureIdForm command) {
ModelAndView mv = new ModelAndView("/capture_id");
BindException errors = null;
try {
errors = bindObject(request, command, new CaptureIdValidator());
if (!errors.hasErrors()) {
Entity tmpEntity = dao.getEntityDetails(command.getId());
if (tmpEntity != null) {
command.setEntityDetails(tmpEntity);
} else {
errors.reject("errors.id.invalid");
}
}
mv.addObject("errors", errors);
mv.addObject("captureIdForm", command);
} catch (Exception e) {
if (errors != null) {
errors.reject("errors.unspecified");
}
}
return mv;
}
public ModelAndView next(HttpServletRequest request, HttpServletResponse response,
CaptureIdForm command) throws Exception {
request.getSession().setAttribute("id",
command.getEntityDetails().getEntityNo());
return new ModelAndView(new RedirectView("CaptureDetails.do"));
}
public void setDao(EntityDao dao) {
this.dao = dao;
}
}
heres an extract from my springapp-servlet.xml. only the relevant bits are added.
<bean id="captureIdController"
class="CaptureIdController">
<property name="methodNameResolver">
<ref bean="methodNameResolver" />
</property>
<property name="dao">
<ref bean="entityDao"/>
</property>
</bean>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlH andlerMapping">
<property name="mappings">
<props>
<prop key="CaptureId.do">captureIdController</prop>
<prop key="CaptureDetails.do">captureDetailsController</prop>
</props>
</property>
</bean>
Would appreciate any advise on this issue.
thanks