FuSsA
FuSsA

Reputation: 4297

Neither BindingResult nor plain target object for bean name 'XYZ' available as reques t attribute

class META_INF

@Entity
@Table(name = "META_INF")
@Inheritance(strategy = InheritanceType.JOINED)
public class META_INF {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(nullable = false)
    private Boolean locked;

    @ManyToOne
    private USER lockedBy;

    @JoinColumn(nullable = false)
    @ManyToOne
    private USER createBy;

    @Column(nullable = false)
    private Boolean active = true;

    private String version;

    @Column(nullable = false)
    private Boolean log;

    @JoinColumn(nullable = false, name = "CONTEXTE")
    @ManyToOne
    private PRM_CONTEXTE CONTEXTE;

    @DateTimeFormat(pattern = "dd-MM-yyyy hh:mm:ss")
    @Column(nullable = false, name = "INSIDE_CREATION_TS")
    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
    private LocalDate INSIDE_CREATION_TS;
}

class PIL_P_APPLCTN

@Entity
@Table(name = "PIL_P_APPLCTN")
public class PIL_P_APPLCTN extends META_INF {

    @NotBlank
    @Column(unique = true, nullable = false, name = "APPLCTN_CD")
    private String APPLCTN_CD;

    @NotBlank
    @Column(nullable = false, name = "APPLCTN_ID")
    private String APPLCTN_ID;

    @NotBlank
    @Column(nullable = false, name = "APPLCTN_DS")
    private String APPLCTN_DS;

    @NotNull
    @Digits(integer = 20, fraction = 0)
    @Column(nullable = false, name = "DECLG_IDNTFNT_NU")
    private BigDecimal DECLG_IDNTFNT_NU;
}

Controller AppController

@Controller
@RequestMapping("/")
@SessionAttributes("roles")
public class AppController {

    static final Logger logger = LoggerFactory.getLogger(AppController.class);

    @Autowired
    @Qualifier("pIL_P_APPLCTN_SERVICE")
    IService<PIL_P_APPLCTN> appService;

    @Autowired
    @Qualifier("pRM_CONTEXTE_SERVICE")
    IService<PRM_CONTEXTE> cntxtService;

    @Autowired
    MessageSource messageSource;
@RequestMapping(value = { "/addApp" }, method = RequestMethod.GET)
    public String newApp(ModelMap model) {
        PIL_P_APPLCTN application = new PIL_P_APPLCTN();
        List<PRM_CONTEXTE> cntxt = cntxtService.findAllOBJECTS();
        model.addAttribute("cntxt", cntxt);
        model.addAttribute("application", application);
        model.addAttribute("edit", false);
        model.addAttribute("loggedinuser", getFullName());
        return "formApp";
    }

    @RequestMapping(value = { "/addApp" }, method = RequestMethod.POST)
    public String saveApp(@Valid PIL_P_APPLCTN application, BindingResult result, ModelMap model) {

        if (result.hasErrors()) {
            return "formApp";
        }

        if (!appService.isObjectFieldUnique(application.getId(), application.getAPPLCTN_CD())) {
            FieldError app_cdError = new FieldError("application", "APPLCTN_CD", messageSource.getMessage("non.unique.APPLCTN_CD",
                    new String[] { application.getAPPLCTN_CD() }, Locale.getDefault()));
            result.addError(app_cdError);
            return "formApp";
        }

        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
        Date dt1 = new Date();
        String dt2 = formatter.format(dt1);
        Date date = null;
        LocalDate dateF = null;
        try {
            date = formatter.parse(dt2);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        dateF = new LocalDate(date);

        application.setCreateBy(userService.findBySSO(getPrincipal()));
        application.setINSIDE_CREATION_TS(dateF);
        application.setLocked(false);
        application.setLockedBy(null);
        application.setVersion("INITIAL");
        application.setLog(false);
        //application.setCONTEXTE(cntxtService.findById(application.getCONTEXTE().getId()));

        appService.saveOBJECT(application);
        model.addAttribute("loggedinuser", getFullName());
        // return "success";
        return "redirect:/appli";
    }
}

formApp.jsp

 <form:form method="POST" modelAttribute="application" class="form-horizontal form-label-left" >
                    <form:input type="hidden" path="id" id="id"/>


                      <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="active">ACTIVE</label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <div class="">                            
                              <form:checkbox id="active" path="active" name="active" class="js-switch"  />

                                <form:errors path="active"/>

                          </div>

                        </div>
                      </div>

                      <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="APPLCTN_CD">APPLCTN CD </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <form:input type="text" path="APPLCTN_CD" id="APPLCTN_CD" name="APPLCTN_CD" class="form-control col-md-7 col-xs-12"/>

                                <form:errors path="APPLCTN_CD"/>

                        </div>
                      </div>

                      <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="APPLCTN_ID">APPLCTN ID 
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <form:input type="text" path="APPLCTN_ID" id="APPLCTN_ID" name="APPLCTN_ID"   class="form-control col-md-7 col-xs-12"/>

                                <form:errors path="APPLCTN_ID"/>

                        </div>
                      </div>

                      <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="APPLCTN_DS">APPLCTN DS 
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <form:input type="text" path="APPLCTN_DS" id="APPLCTN_DS" name="APPLCTN_DS"   class="form-control col-md-7 col-xs-12"/>

                                <form:errors path="APPLCTN_DS"/>

                        </div>
                      </div>
                      <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="DECLG_IDNTFNT_NU">DECLG IDNTFNT NU
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <form:input type="text" path="DECLG_IDNTFNT_NU" id="DECLG_IDNTFNT_NU" name="DECLG_IDNTFNT_NU"  class="form-control col-md-7 col-xs-12"/>

                                <form:errors path="DECLG_IDNTFNT_NU"/>

                        </div>
                      </div>
                      <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="CONTEXT">CONTEXT</label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <form:select itemValue="id" path="CONTEXTE" itemLabel="CONTEXTE_CD" items="${cntxt}" class="select2_single form-control" tabindex="-1" />

                                <form:errors path="CONTEXTE"/>


                        </div>
                      </div>



                      <div class="ln_solid"></div>
                      <div class="form-group">
                        <div class="col-md-6 col-md-offset-3">
                            <c:choose>
                                <c:when test="${edit}">
                                    <button type="button" class="btn btn-primary" onClick="location.href='<c:url value='/appli' />'">Annuler</button>
                                    <button type="submit" class="btn btn-success" onClick="location.href='<c:url value='/edtApp' />'">Modifier</button>
                                </c:when>
                                <c:otherwise>
                                        <button type="button" class="btn btn-primary" onClick="location.href='<c:url value='/appli' />'">Annuler</button>
                                    <input type="submit" class="btn btn-success" value="Ajouter"/>
                                </c:otherwise>
                            </c:choose>
                        </div>
                      </div>
                    </form:form>

messages.properties

NotBlank.pil_p_applctn.APPLCTN_CD=Champ obligatoire !
NotBlank.pil_p_applctn.APPLCTN_ID=Champ obligatoire !
NotNull.pil_p_applctn.APPLCTN_DS=Champ obligatoire !
NotBlank.pil_p_applctn.DECLG_IDNTFNT_NU=Champ obligatoire !
Digits.pil_p_applctn.DECLG_IDNTFNT_NU=Seules les données numériques sont acceptées avec max 20 chiffres !
non.unique.APPLCTN_CD={0} existe déjà !

When i'm insering valid data in the form, the object is persisted in DB, but when i'm trying to insert wrong data the error messages in messages.properties are not shown.. and got this error:

Caused by: java.lang.RuntimeException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'application' available as reques
t attribute
    at io.undertow.servlet.spec.RequestDispatcherImpl.forwardImpl(RequestDispatcherImpl.java:217)
    at io.undertow.servlet.spec.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:106)
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1243)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968)
    ... 70 more
Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'application' available as request attribute
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:117)
    at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422)
    at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
    at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
    at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
    at org.apache.jsp.WEB_002dINF.views.formApp_jsp._jspx_meth_form_005finput_005f0(formApp_jsp.java:2155)
    at org.apache.jsp.WEB_002dINF.views.formApp_jsp._jspService(formApp_jsp.java:554)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:402)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:346)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
    at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:81)
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
    at io.undertow.jsp.JspFileHandler.handleRequest(JspFileHandler.java:32)
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:265)
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchToPath(ServletInitialHandler.java:200)
    at io.undertow.servlet.spec.RequestDispatcherImpl.forwardImpl(RequestDispatcherImpl.java:193)
    ... 78 more

Thanks for any advices ..

Upvotes: 0

Views: 609

Answers (1)

KlajdPaja
KlajdPaja

Reputation: 959

From here is the error comming...

 @RequestMapping(value = { "/addApp" }, method = RequestMethod.POST)
        public String saveApp(@Valid PIL_P_APPLCTN application, BindingResult result, ModelMap model) {

            if (result.hasErrors()) {
                return "formApp";
            }

            if (!appService.isObjectFieldUnique(application.getId(), application.getAPPLCTN_CD())) {
                FieldError app_cdError = new FieldError("application", "APPLCTN_CD", messageSource.getMessage("non.unique.APPLCTN_CD",
                        new String[] { application.getAPPLCTN_CD() }, Locale.getDefault()));
                result.addError(app_cdError);
                return "formApp";
            }

Thus your code shuold be:

@RequestMapping(value = { "/addApp" }, method = RequestMethod.POST)
        public String saveApp(@Valid @ModelAttribute("application") PIL_P_APPLCTN application, BindingResult result, ModelMap model) {
            model.addAttribute("application", application);

            if (result.hasErrors()) {
                return "formApp";
            }

            if (!appService.isObjectFieldUnique(application.getId(), application.getAPPLCTN_CD())) {
                FieldError app_cdError = new FieldError("application", "APPLCTN_CD", messageSource.getMessage("non.unique.APPLCTN_CD",
                        new String[] { application.getAPPLCTN_CD() }, Locale.getDefault()));
                result.addError(app_cdError);
                return "formApp";
            }

formApp.jsp

<form:form method="POST" commandeName="application" class="form-horizontal form-label-left" >
                    //....
 </form:form>

Upvotes: 2

Related Questions