0

我想在不使用注释的情况下在控制器中发送表单输入数据。

在这段代码中,我试图从我使用 Spring 的 jsp 文件中获取 ,employeeNo标记empName,但是当我尝试获取值时,它没有获取值并引发异常:deptNo<form:form><form:input>

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

我的控制器

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);
    }

}

我的jsp文件:

<%@ 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>

我的xml文件:

<?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>
4

1 回答 1

0

错误的原因是请求中没有可以绑定字段的对象。因此,当您返回此视图时,您需要将一个 Object 传递给 jsp。

在返回上述 jsp 的方法中,使用表单执行此操作

public ModelAndView yourMethodForView(){
//Assuming Employee object has employeeNo and empName
Employee emp = new Employee();
//your code
return new ModelAndView("yourFormJspName","employee",emp);
}
于 2013-01-02T15:26:29.973 回答