当前位置: 首页 > 知识库问答 >
问题:

Springframework:bean名称既不是BindingResult也不是纯目标对象

栾越
2023-03-14
<%--
    Document   : successView
    Created on : May 2, 2010, 2:06:51 PM
    Author     : nbuser
--%>

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

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Enter Your Name</title>
    </head>
    <body>
        <h1>Enter Your Name!</h1>  
        <form:form commandName="car" method="POST">
                Brand:<br>
                <form:input path="brand.name"/><br>
                    <form:input path="brand.country"/><br>
                Price:<br>
                    <form:input path="price"/><br>
                <input type="submit" value="OK">
            </form:form>

    </body>
</html>

carview.jsp

    <%--
    Document   : carView
    Created on : May 2, 2010, 2:06:25 PM
    Author     : nbuser
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Hello</title>
    </head>
    <body>
        <h2>${name}</h2>
        <h2>${country}</h2>
        <h2>${price}</h2>
    </body>
</html>

ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
    <bean name="carService" class="Service.CarService" />
    <!--bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.url}"
    p:username="${jdbc.username}"
    p:password="${jdbc.password}" /-->

    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->

</beans>

web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>
package service;

import controller.Car;

/**
 *
 * @author nbuser
 */
public class CarService {

    public String sayName(Car car) {
        return String.format("Name: %s /n", car.getBrand().getName());
    }

    public String sayCountry(Car car) {
        return String.format("Country: %s /n", car.getBrand().getCountry());
    }

    public String sayPrice(Car car) {
        return String.format("Price: %d", car.getPrice());
    }
}
package controller;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author PTOSH
 */
public class Brand {
    private String name;
    private String country;

    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
package controller;

import java.math.BigDecimal;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author PTOSH
 */
public class Car {
    private Brand brand;
    private BigDecimal price;

    public Brand getBrand() {
        return brand;
    }
    public void setBrand(Brand brand) {
        this.brand = brand;
    }
    public BigDecimal getPrice() {
        return price;
    }
    public void setPrice(BigDecimal price) {
        this.price = price;
    }
}
    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import service.CarService;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

/**
 *
 * @author PTOSH
 */

//@Controller
//@RequestMapping("car")
public class CarController extends SimpleFormController {
//    @Valid
    private CarService carService;

    public CarController() {
        //Initialize controller properties here or
        //in the Web Application Context
        setCommandClass(Car.class);
        setCommandName("car");
        setSuccessView("successView");
        setFormView("carView");
    }
//    @RequestMapping(method = RequestMethod.GET)
//      public String initForm(ModelMap model){
//      //return form view
//      return "car";
//  }
    public void setCarService(CarService carService) {
        this.carService = carService;
    }

    //Use onSubmit instead of doSubmitAction
    //when you need access to the Request, Response, or BindException objects
    @Override
//    @RequestMapping(method = RequestMethod.POST)
    protected ModelAndView onSubmit(Object command) throws Exception {
        Car car = (Car) command;
        ModelAndView mv = new ModelAndView(getSuccessView());
        mv.addObject("name", carService.sayName(car));
        mv.addObject("country", carService.sayCountry(car));
        mv.addObject("price", carService.sayPrice(car));
        return mv;
    }
}

错误

grave:加载应用程序时出现异常grave:上下文/MyCar取消部署失败grave:加载应用程序时出现异常:java.lang.IllegalStateException:ContainerBase.AddChild:Start:org.apache.catalina.LifecycleException:java.lang.IllegalArgumentException:java.lang.ClassNotFoundException:org.SpringFramework.web.context.ContextLoaderListener

共有1个答案

狄彬彬
2023-03-14

1查看Spring参考文档,您正在使用旧的Spring样式(verbose)通过JSP文件访问对象。

  1. 您有以下内容:

更改自

<spring:bind path="Brand">
<spring:bind path="brand">
 类似资料: