Domanda

I want to send form input data in controller without using annotation.

In this code I'm trying to get the employeeNo, empName, deptNo form the jsp file in which I have used Spring <form:form>, <form:input> tags but as I try to get value, it is not getting the value and it throws an exception:

java.lang.IllegalStateException: Neither BindingResult nor plain target object 
for bean name 'command' available as request attribute 

My controller:

package com.nousinfo.tutorial.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.nousinfo.tutorial.service.impl.EmployeeServiceImpl;
import com.nousinfo.tutorial.service.model.EmployeeBO;

public class SearchEmployeeController extends MultiActionController {
    private EmployeeServiceImpl employeeServiceImpl;

    public void setEmployeeServiceImpl(EmployeeServiceImpl employeeServiceImpl) {
        this.employeeServiceImpl = employeeServiceImpl;
    }

    public ModelAndView searchByEmpNo(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        EmployeeBO empDetails = null;
        try {
            if (employeeServiceImpl.getEmployee(Long.parseLong(request
                    .getParameter("employeeNo"))) != null) {
                empDetails = employeeServiceImpl.getEmployee(Long
                        .parseLong(request.getParameter("employeeNo")));
            }
        } catch (EmptyResultDataAccessException e) {
            return new ModelAndView("successSingleEmp", "empDetails",
                    empDetails);
        }
        return new ModelAndView("successSingleEmp", "empDetails", empDetails);
    }

    public ModelAndView searchByEmpName(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        List<EmployeeBO> listEmployeeBO = employeeServiceImpl
                .findEmployees(request.getParameter("empName"));
        return new ModelAndView("successMultipleEmps", "listEmployeeBO",
                listEmployeeBO);
    }

    public ModelAndView searchByDeptNo(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        List<EmployeeBO> listEmployeeBO = employeeServiceImpl
                .getAllEmployeeByDeptid(request.getParameter("deptNo"));
        return new ModelAndView("successMultipleEmps", "listEmployeeBO",
                listEmployeeBO);
    }

}

My jsp file:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<fmt:setBundle basename="ApplicationResources" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Employee Search Page</title>
</head>
<body>
    <form:form action="search.spring" method="post">
        <table border="0">
            <tr>
                <td><form:label path="employeeNo">Employee_ID</form:label> <form:input
                        path="employeeNo" /></td>
                <td><input type="submit" name="method" value="FindById" /></td>
            </tr>
            <tr>
                <td><form:label path="empName">Employee_Name</form:label> <form:input
                        path="empName" /></td>
                <td><input type="submit" name="method" value="FindByName" /></td>
            </tr>    
            <tr>
                <td><form:label path="deptNo">Employee_Name</form:label> <form:input
                        path="deptNo" /></td>
                <td><input type="submit" name="method" value="FindByDeptNO" /></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><font size=3>For
                        Searching the employees by<b>Employee Name</b><br />you can use %
                        match all the records with the given pattern
                </font><br /> <font size="2"> <i>e.g <b> for search by</b>EmployeeName<br />
                            matches alL the employees whose name starts with character <b>S</b></i></font></td>
            </tr>
        </table>
    </form:form>
</body>
</html>

My xml file:

<?xml version="1.0" encoding="UTF-8"?>

<beans
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">
    <import resource="/services-context.xml" />
    <bean id="searchController"
        class="com.nousinfo.tutorial.controllers.SearchEmployeeController">
        <property name="employeeServiceImpl" ref="employeserviceImpl" />
        <property name="methodNameResolver" ref="methodNameResolver" />
    </bean>    

    <bean id="myurlmapping"
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="search.spring">searchController</prop>
            </props>
        </property>
    </bean>    

    <bean id="methodNameResolver"
        class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
        <property name="paramName" value="method" />
    </bean>

    <!-- configuring BeanNameViewResolver -->
    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"></bean>

    <!-- configuring exception Resolver -->
    <bean
        class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="org.springframework.dao.DAOException">dberrorView</prop>
                <prop key="java.lang.NumberFormatException">notANumber</prop>
            </props>
        </property>
    </bean>
    <!-- configuring views required to render the view for /search.spring -->
    <bean name="successSingleEmp" class="org.springframework.web.servlet.view.JstlView">
        <property name="url" value="/WEB-INF/EmployeeDetail.jsp" />
    </bean>
    <bean name="successMultipleEmps" class="org.springframework.web.servlet.view.JstlView">
        <property name="url" value="/WEB-INF/EmployeeList.jsp" />
    </bean>
    <bean name="dberrorView" class="org.springframework.web.servlet.view.JstlView">
        <property name="url" value="/WEB-INF/DBError.jsp" />
    </bean>
    <bean name="notANumber" class="org.springframework.web.servlet.view.JstlView">
        <property name="url" value="/WEB-INF/NotANumberError.jsp" />
    </bean>
    <!-- CONFIGURING THE MEASSAGE RESOURCES FOR THIS APPLICTION CONTEXT -->
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="/com/nousinfo/resources/ApplicationResources" />
    </bean>
</beans>
È stato utile?

Soluzione

The reason for the error is that there is no object to which the fields can be bound in the request. So you need to pass an Object to the jsp when you are returning this view.

In the method where you return the above mentioned jsp with the form Do this

public ModelAndView yourMethodForView(){
//Assuming Employee object has employeeNo and empName
Employee emp = new Employee();
//your code
return new ModelAndView("yourFormJspName","employee",emp);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top