Fala galera…
to com o seguinte problema na minha app…
pretendo usar o mesmo JSP, mesma Action e mesmo Form tanto p/ fazer um Insert qto um Update de um registro…
a tela possui apenas o campo Name a ser validado…o Insert funciona normalmente, mas qdo tento mandar p/ fazer o Update, ocorre a validacao e diz q o campo eh requerido…
segue meus codigos…
minha Action:
ActionErrors errors = new ActionErrors();
ActionForward forward = new ActionForward(); // return value
NewActivityForm newActivity = (NewActivityForm)form;
if(request.getParameter("action").equals("save") && request.getParameter("id").equals("0")){
try{
FacadeActivity facadeA = FacadeActivity.getInstance();
if(!facadeA.exists(newActivity.getName())){
facadeA.create(newActivity.getName());
errors.add("msg",new ActionError("insert.success"));
saveErrors(request,errors);
forward = mapping.findForward("success");
}
else{
errors.add("msg",new ActionError("insert.error.exists"));
saveErrors(request,errors);
forward = mapping.findForward("success");
}
}
catch(SQLException e){
e.printStackTrace();
errors.add("msg",new ActionError("insert.error"));
saveErrors(request,errors);
forward = mapping.findForward("error");
}
}
else if(request.getParameter("action").equals("searchActivity")){
try{
FacadeActivity facadeA = FacadeActivity.getInstance();
Activity activity = facadeA.getActivity(Integer.parseInt((String)request.getParameter("id")));
newActivity.setId(activity.getId().intValue());
newActivity.setName(activity.getName());
forward = mapping.findForward("update");
}
catch(Exception e){
e.printStackTrace();
errors.add("msg",new ActionError("search.error"));
saveErrors(request,errors);
forward = mapping.findForward("error");
}
return forward;
}
else if(request.getParameter("action").equals("save") && !request.getParameter("id").equals("0")){
try{
FacadeActivity facadeA = FacadeActivity.getInstance();
if(!facadeA.exists(newActivity.getName())){
facadeA.update(Integer.parseInt((String)request.getParameter("id")), newActivity.getName());
errors.add("msg",new ActionError("update.success"));
saveErrors(request,errors);
forward = mapping.findForward("success");
}
else{
errors.add("msg",new ActionError("update.error.exists"));
saveErrors(request,errors);
forward = mapping.findForward("success");
}
}
catch(SQLException e){
e.printStackTrace();
errors.add("msg",new ActionError("update.error"));
saveErrors(request,errors);
forward = mapping.findForward("error");
}
}
// Finish with
return (forward);
meu Form:
private String name = null;
//get e set...
public void reset(ActionMapping mapping, HttpServletRequest request) {
// Reset values are provided as samples only. Change as appropriate.
name = null;
}
public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
// Validate the fields in your form, adding
// adding each error to this.errors as found, e.g.
if ((name == null) || (name.length() == 0)) {
errors.add("name", new org.apache.struts.action.ActionError("required.field"));
}
return errors;
}
qq ajuda eh mto bem-vinda…
valew