Vivek Gondliya
Vivek Gondliya

Reputation: 308

Getting Error in Spring Mvc

I am getting following error....

java.lang.IllegalStateException: **Neither BindingResult nor plain target object for bean name 'course' available as request attribute**
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
    at 

Model Class:

@Entity
@Table(name="course" ,schema = "practise5")
public class Course implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;



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

     @Column(name="Name")
        private String Name;

    @ManyToMany(mappedBy="courseSet")
    private Set<Person> personSet;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }



    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

    public Set<Person> getPersonSet() {
        return personSet;
    }

    public void setPersonSet(Set<Person> personSet) {
        this.personSet = personSet;
    }

}

Controller:

@RequestMapping(value="/addCourse",method= RequestMethod.POST)
public @ResponseBody String addCourse(@ModelAttribute("course") Course course, Model model)
{

    courseServise.addcourse(course);

    return "redirect:addEmployee";
}

Jsp:

<form:form commandName="course" action="addCourse" method="POST">
<form:input path="CourseName" id="course" required="required"/>
<input type="submit" value="Submit">
</form:form>

............................................................................................................................................................

<context:annotation-config />
 <context:component-scan base-package="com.spring" />

<mvc:annotation-driven />
<mvc:resources location="/WEB-INF/" mapping="/**" />


<bean id="jspViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/view/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="resources/messages" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />

<tx:annotation-driven />
<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
        <list>
            <value>com.spring.model</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql">true</prop>
            <!-- <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.use_sql_comments">true</prop> -->
        </props>
    </property>
</bean>

Upvotes: 1

Views: 415

Answers (3)

pappu_kutty
pappu_kutty

Reputation: 2488

as said in this post, error might occur while loading the jsp itself, and more over binding might fail for below reasons

The binding results can be examined via the BindingResult interface, extending the Errors interface: see the getBindingResult() method. Missing fields and property access exceptions will be converted to FieldErrors, collected in the Errors instance, using the following error codes:

> Missing field error: "required" 
> Type mismatch error: "typeMismatch"
> Method invocation error: "methodInvocation"

so check any type mismatches or field you might have missed, as i had faced this issue personally

Upvotes: 0

Vivek Gondliya
Vivek Gondliya

Reputation: 308

I just add the following code in my controller...

Second thing is that you have to first redirect your index.jsp file to some other file...

@RequestMapping(method = RequestMethod.GET)
public String addEmployee(@ModelAttribute("a") A a, Model model,HttpServletRequest request) 
{

    return "addEmployee";
}

Upvotes: 1

Mistalis
Mistalis

Reputation: 18309

You don't match the correct URL in your JSP file. Change this line on your JSP file :

<form:form commandName="course" action="addCourse" method="POST"> 

For:

<form:form commandName="course" action="/addCourse" method="POST">

Else, you can change your mapping on your controller:

@RequestMapping(value="/addCourse",method= RequestMethod.POST) 

For:

@RequestMapping(value="addCourse",method= RequestMethod.POST)

Upvotes: 0

Related Questions