web-dev-qa-db-fra.com

Ni BindingResult ni l'objet simple pour le nom du haricot disponible en tant que requête attr


Bonjour Experts, J'ai ce code de contrôleur qui jette l’erreur mentionnée ci-dessus. Cela fonctionnait bien jusqu'à hier, je n'ai aucune idée de ce que mon collègue a fait avec ce code et aujourd'hui, je vois l'erreur:

Ni BindingResult, ni l'objet cible en clair pour le nom de bean 'sideForm' disponible en tant qu'attribut de demande 

Pouvez-vous s'il vous plaît me suggérer où chercher ce genre d'erreur. Est-ce que je commets une erreur dans la déclaration de la méthode POST ou GET ou si je retourne un problème?

Votre aide est grandement appréciée :)

package com.att.analytics.ui;

import Java.util.Arrays;
import Java.util.Collection;
import Java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.Apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.si.myworld.business.AdminChart;
import com.si.myworld.business.Chart;
import com.si.myworld.dao.AdminChartSummaryData;
import com.si.myworld.dao.BulletinData;
import com.si.myworld.dao.ChartData;

@RequestMapping("/index.html")
@Controller
public class IndexController {

private static final Logger log = Logger.getLogger(IndexController.class);

/**
 * Called to load the page initially (GET request)
 * 
 * @param model
 * @return
 */
@RequestMapping(method = RequestMethod.GET)
public String getCharts(ModelMap model) {

    Chart chart = new Chart();
    chart.setTimeline("Monthly");
    chart.setStartDt("mm/dd/yyyy");
    chart.setEndDt("mm/dd/yyyy");

    AdminChartSummaryData acsd = new AdminChartSummaryData();
    List<AdminChart> list = acsd.getLandingChartDataList();

    if (list.size() == 4) {
        Chart chart1 = new Chart(list.get(0));
        Chart chart2 = new Chart(list.get(1));
        Chart chart3 = new Chart(list.get(2));
        Chart chart4 = new Chart(list.get(3));
        model.put("chart1", chart1);
        model.put("chart2", chart2);
        model.put("chart3", chart3);
        model.put("chart4", chart4);

        log.info("chart 1>>>>" + chart1);


        ChartData cd = new ChartData();

        String chartOneDatasource = cd.fetchChartDatasourceName(chart1
                .getChartSubgroup());
        String chartTwoDatasource = cd.fetchChartDatasourceName(chart2
                .getChartSubgroup());
        String chartThreeDatasource = cd.fetchChartDatasourceName(chart3
                .getChartSubgroup());
        String chartFourDatasource = cd.fetchChartDatasourceName(chart4
                .getChartSubgroup());

        String breadcrumbOne = chart1.getChartGroup() + ">>"
                + chart1.getChartSubgroup();
        String breadcrumbTwo = chart2.getChartGroup() + ">>"
                + chart2.getChartSubgroup();
        String breadcrumbThree = chart3.getChartGroup() + ">>"
                + chart3.getChartSubgroup();
        String breadcrumbFour = chart4.getChartGroup() + ">>"
                + chart4.getChartSubgroup();


        BulletinData bd = new BulletinData();
        String bulletin = bd.getBulletinData();

        model.put("sideForm", chart);



        model.put("chartOneDatasource", chartOneDatasource);
        model.put("chartTwoDatasource", chartTwoDatasource);
        model.put("chartThreeDatasource", chartThreeDatasource);
        model.put("chartFourDatasource", chartFourDatasource);

        model.put("breadcrumbOne", breadcrumbOne);
        model.put("breadcrumbTwo", breadcrumbTwo);
        model.put("breadcrumbThree", breadcrumbThree);
        model.put("breadcrumbFour", breadcrumbFour);

        model.put("bulletin", bulletin);
    }

    return "land";
}


@RequestMapping(method = RequestMethod.POST)
public String loadCharts(HttpServletRequest request, ModelMap model, @ModelAttribute("sideForm") Chart chart) {

    String from_date = request.getParameter("startDt");
    String to_date = request.getParameter("endDt");

    chart.setStartDt(from_date);
    chart.setEndDt(to_date);

    ChartData cd = new ChartData();

    BulletinData bd = new BulletinData();
    String bulletin = bd.getBulletinData();



    AdminChartSummaryData acsd = new AdminChartSummaryData();
    List<AdminChart> list = acsd.getLandingChartDataList();
    if (list.size() == 4) {
        Chart chart1 = new Chart(list.get(0));
        Chart chart2 = new Chart(list.get(1));
        Chart chart3 = new Chart(list.get(2));
        Chart chart4 = new Chart(list.get(3));
        model.put("chart1", chart1);
        model.put("chart2", chart2);
        model.put("chart3", chart3);
        model.put("chart4", chart4);


        String chartOneDatasource = cd.fetchChartDatasourceName(chart1
                .getChartSubgroup());
        String chartTwoDatasource = cd.fetchChartDatasourceName(chart2
                .getChartSubgroup());
        String chartThreeDatasource = cd.fetchChartDatasourceName(chart3
                .getChartSubgroup());
        String chartFourDatasource = cd.fetchChartDatasourceName(chart4
                .getChartSubgroup());

        model.put("chartOneDatasource", chartOneDatasource);
        model.put("chartTwoDatasource", chartTwoDatasource);
        model.put("chartThreeDatasource", chartThreeDatasource);
        model.put("chartFourDatasource", chartFourDatasource);

        String breadcrumbOne = chart1.getChartGroup() + ">>"
                + chart1.getChartSubgroup();
        String breadcrumbTwo = chart2.getChartGroup() + ">>"
                + chart2.getChartSubgroup();
        String breadcrumbThree = chart3.getChartGroup() + ">>"
                + chart3.getChartSubgroup();
        String breadcrumbFour = chart4.getChartGroup() + ">>"
                + chart4.getChartSubgroup();


        model.put("breadcrumbOne", breadcrumbOne);
        model.put("breadcrumbTwo", breadcrumbTwo);
        model.put("breadcrumbThree", breadcrumbThree);
        model.put("breadcrumbFour", breadcrumbFour);
    }
    return "land";
}


@ModelAttribute("timeline")
public Collection<String> populateTimeline() {
    return Arrays.asList("Daily", "Weekly", "Monthly", "Quarterly",
            "Annually", "12_Month_Rolling");
}
}

Cette page obtient les valeurs d'un formulaire ci-dessous:

<form:form commandName="sideForm">
<div style="font-weight:bold; color:#000">Timeline</div>


<div style="margin:0 0 5px 0;"><form:select path="timeline" items="${timeline}" id="tm"/></div>

<div class="tofrom">From:</div>

<form:input path="startDt" id="from_date" size="7" maxlength="10" />

<div class="tofrom">To:</div>

<form:input path="endDt" id="to_date" size="7" maxlength="12" />

<input type="submit" value="Update Chart" />

</form:form>
33
Aman

Assurez-vous que votre formulaire Spring mentionne le modelAttribute="<Model Name".

Exemple:  

@Controller
@RequestMapping("/greeting.html")
public class GreetingController {

 @ModelAttribute("greeting")
 public Greeting getGreetingObject() {
  return new Greeting();
 }

 /**
  * GET
  * 
  * 
  */
 @RequestMapping(method = RequestMethod.GET)
 public String handleRequest() {
  return "greeting";
 }

 /**
  * POST
  * 
  * 
  */
 @RequestMapping(method = RequestMethod.POST)
 public ModelAndView processSubmit(@ModelAttribute("greeting") Greeting greeting, BindingResult result){
  ModelAndView mv = new ModelAndView();
  mv.addObject("greeting", greeting);  
  return mv;
 }
}

Dans votre JSP:

<form:form  modelAttribute="greeting" method="POST" action="greeting.html">
37
Dan Hargis

Veillez à déclarer le bean associé au formulaire dans la méthode GET du contrôleur associé et à l'ajouter également dans le modèle model.addAttribute("uploadItem", uploadItem); qui contient l'annotation @RequestMapping(method = RequestMethod.GET)

Par exemple, UploadItem.Java est associé à myform.jsp et le contrôleur est SecureAreaController.Java.

myform.jsp contient 

<form:form action="/securedArea" commandName="uploadItem" enctype="multipart/form-data"></form:form>

MyFormController.Java

@RequestMapping("/securedArea")
@Controller
public class SecureAreaController {

@RequestMapping(method = RequestMethod.GET)
public String showForm(Model model) {
            UploadItem uploadItem = new UploadItem(); // declareing

            model.addAttribute("uploadItem", uploadItem); // adding in model
    return "securedArea/upload";
}

}

Comme vous pouvez le constater, je déclare UploadItem.Java dans la méthode GET du contrôleur.

25
user473445

Essayez d’ajouter un paramètre BindingResult aux méthodes annotées avec @RequestMapping qui ont des paramètres annotés @ModelAttribute. Après chaque paramètre @ModelAttribute, Spring recherche un BindingResult dans la position suivante du paramètre (l'ordre est important).

Alors essayez de changer:

@RequestMapping(method = RequestMethod.POST)
public String loadCharts(HttpServletRequest request, ModelMap model, @ModelAttribute("sideForm") Chart chart) 
...

À:

@RequestMapping(method = RequestMethod.POST)
public String loadCharts(@ModelAttribute("sideForm") Chart chart, BindingResult bindingResult, HttpServletRequest request, ModelMap model) 
...

6
Rob Beardow

J'ai travaillé sur le même sujet et je suis sûr d'avoir découvert la raison exacte.

Ni BindingResult ni l'objet cible en clair pour le nom du bean 'commande' disponible en tant qu'attribut de demande

Si la valeur de votre propriété successView (nom de la page jsp) est identique à celle de la page d'entrée, la deuxième valeur du constructeur ModelAndView doit correspondre au nom de la commande de la page d'entrée.

Par exemple.

index.jsp

<html>
<body>
    <table>
        <tr><td><a href="Login.html">Login</a></td></tr>
    </table>
</body>
</html>

dispatcher-servlet.xml

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">

    <property name="prefix">
        <value>/WEB-INF/jsp/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>
<bean id="urlMapping"
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="urlMap">
        <map>              
            <entry key="/Login.html">
                <ref bean="userController"/>
            </entry>
        </map>          
    </property>             
</bean>     
 <bean id="userController" class="controller.AddCountryFormController">     
       <property name="commandName"><value>country</value></property>
       <property name="commandClass"><value>controller.Country</value></property>        
       <property name="formView"><value>countryForm</value></property>
       <property name="successView"><value>countryForm</value></property>
   </bean>      

AddCountryFormController.Java

package controller;

import javax.servlet.http.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.mvc.SimpleFormController;


public class AddCountryFormController extends SimpleFormController
{

    public AddCountryFormController(){
        setCommandName("Country.class");
    }

    protected ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response,Object command,BindException errors){

            Country country=(Country)command;

            System.out.println("calling onsubmit method !!!!!");

        return new ModelAndView(getSuccessView(),"country",country);

    }

}

Pays.Java

package controller;

public class Country
{
    private String countryName;

    public void setCountryName(String value){
        countryName=value;
    }

    public String getCountryName(){
        return countryName;
    }

}

countryForm.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<body>
    <form:form commandName="country" method="POST" >
            <table>
                    <tr><td><form:input path="countryName"/></td></tr>
                    <tr><td><input type="submit" value="Save"/></td></tr>
            </table>
    </form:form>
</body>
<html>

Page d'entrée commandName="country"ModelAndView Constructor as return new ModelAndView(getSuccessView(),"country",country); Signifie la page d'entrée commandName==ModeAndView(,"commandName",)

4
ravindra

Nous avons fait face au même problème et changé commandname="" en modelAttribute="" dans la page jsp pour résoudre ce problème.

0
Rohit Singh