我是Spring的新手。我已经创建了一个名为" BookWorkshop"使用spring-3.1遵循MVC架构。但是一旦我击中了
url:http://localhost:8087/BookWorkshop
我收到的错误如下:
org.springframework.beans.TypeMismatchException:无法转换类型' java.lang.String'的属性值要求的类型' java.lang.Class' for property' commandClass' 。
我附上我的代码::
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>
表单控制器类::: 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>
任何人都可以给出任何解决方案吗?????????????
答案 0 :(得分:1)
试试这个:
<property name="commandClass">
<value>
com.edifixio.model.UserBook
</value>
</property>
答案 1 :(得分:0)
您可以从here 下载基于完整工作的spring mvc表单示例。然后,您可以以任何方式修改该示例。