1

我是 Spring 的新手。我按照 MVC 架构使用 spring-3.1 制作了一个名为“BookWorkshop”的应用程序。但是一旦我 url:http://localhost:8087/BookWorkshop 遇到错误,如下所示:

org.springframework.beans.TypeMismatchException:无法将类型“java.lang.String”的属性值转换为属性“commandClass”所需的类型“java.lang.Class”

我附上我的代码::

Spring servlet 类:bookdispatcher-servlet.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:context="http://www.springframework.org/schema/context"
   xmlns:p="http://www.springframework.org/schema/p"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xs">

  <bean name="/welcome_book.html" class="com.edifixio.controller.UserBookController"/>

  <bean name="/new_book.html" class="com.edifixio.controller.StoreBookController">
      <property name="commandClass" value="com.edifixio.model.UserBook"/>
      <property name="formView" value="addnewbook"/>
      <property name="successView" value="/welcome_book.html"/>
 </bean>

 <bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver"
       p:viewClass="org.springframework.web.servlet.view.JstlView"         
       p:prefix="/WEB-INF/pages/"
       p:suffix=".jsp"/>  
</beans>

表单控制器 class:::StoreBookController.java 如下

public class StoreBookController extends SimpleFormController {

   @Override
   protected Object formBackingObject(HttpServletRequest request) throws Exception {
     UserBook book = new UserBook(0,"Enter Book Name", null, 0);
     return book;
   }

    @Override
    protected Map referenceData(HttpServletRequest request) throws Exception {
      Map<Object, Object> dataMap = new HashMap<Object, Object>();
      PublisherManager manager = new PublisherManager();
      dataMap.put("publishierList", manager.getPublishierList());
      return dataMap;
   }

   @Override
  protected void initBinder(HttpServletRequest request, ServletRequestDataBinder  dataBinder) throws Exception {
    dataBinder.setDisallowedFields(new String[]{"publishier"});
    UserBook userBook = (UserBook) dataBinder.getTarget();
    PublisherManager manager = new PublisherManager();
    Long publishierid = null;
    try {
        publishierid = Long.parseLong(request.getParameter("publishier"));
    } catch (Exception e) {
    }
    if (publishierid != null) {
        userBook.setPublisher(manager.getPublishierById(publishierid));
    }
  }

   @Override
   public ModelAndView onSubmit(Object command) throws ServletException {
     BookManager bookManager = new BookManager();
     bookManager.createBook((UserBook) command);
     return new ModelAndView(new RedirectView(getSuccessView()));
    }
 }

然后我的formView页面命名为::: addnewbook.jsp如下---

      <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      <%@taglib prefix="sp" uri="http://www.springframework.org/tags/form"%>
      <%@page contentType="text/html" pageEncoding="UTF-8"%>
       <!DOCTYPE html>
   <html>
     <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       <title>JSP Page</title>
     </head>
   <body>
      <h1>ADD BOOK</h1>
      <sp:form method="post">
           Publisher:<sp:select path="publisher"> 
               <sp:options items="${publisherList}" itemLabel="publishername" itemValue="publisherid"/>
          </sp:select>
           <br/><br/>
          Book Name:<sp:input path="bookname"/>
           <br/><br/>
          Price:<sp:input path="price"/>
           <br/><br/>
          <input type="submit" value="SAVE"/>
       </sp:form>
   </body>
</html>

最后successView控制器类命名为:UserBookController.java如下

public class UserBookController implements Controller {

  @Override
  public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse   response) throws Exception {
     BookManager manager = new BookManager();
     ModelAndView modelAndView = new ModelAndView("booklist");
     modelAndView.addObject("allbook", manager.createBookCollection());
     return modelAndView;
    }
}

successView页面命名为:booklist.jsp如下:::

  <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  <%@page contentType="text/html" pageEncoding="UTF-8"%>
  <!DOCTYPE html>
 <html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       <title>JSP Page</title>
   </head>
  <body>
      <h1>BOOK  WORKSHOP  APPLICATION</h1>
           <table style="border:2;color:graytext" >
           <th style="color: background;alignment-adjust: baseline">Book Name</th>
           <th style="color: background">Price</th>
           <th style="color: background">Publisher</th>
           <c:forEach items="${allbook}" var="b">
              <tr>
                  <td style="color:burlywood">${b.bookname}</td> 
                  <td style="color:burlywood">${b.price}</td>
                  <td style="color:burlywood">${b.publisher.publishername}</td>
              </tr>
           </c:forEach>
      </table>
    </body>
  </html>

最后web.xml文件在这里::::::::

  <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee    /web-app_3_0.xsd">

    <welcome-file-list>
       <welcome-file>/new_book.html</welcome-file>
   </welcome-file-list>
  <servlet>
      <servlet-name>bookdispatcher</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>bookdispatcher</servlet-name>
      <url-pattern>*.html</url-pattern>
  </servlet-mapping>
   <session-config>
      <session-timeout>
        30
      </session-timeout>
   </session-config>
   </web-app>

谁能解决这个问题????????????

4

2 回答 2

1

尝试这个:

<property name="commandClass">  
            <value>  
                com.edifixio.model.UserBook 
            </value>  
</property> 
于 2013-09-25T07:32:36.243 回答
0

您可以从这里下载一个完全工作的基于 spring mvc 表单的示例。然后,您可以以任何您想要的方式修改该示例。

于 2013-09-25T13:28:29.783 回答