1

我是 spring3 的初学者,所以我正在创建一个简单的登录应用程序。在我的 loginController 中,我在以下行中收到“java.lang.ClassCastException:org.springframework.web.bind.support.SimpleSessionStatus cannot be cast to com.forms.LoginForm”

LoginForm login = (LoginForm)command;

参考代码如下:

LoginController.java:-

package com.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.Dao.LoginDaoUtil;
import com.forms.LoginForm;

@SuppressWarnings("deprecation")
@Controller
public class LoginController extends SimpleFormController {

    public LoginController() {
        setCommandClass(LoginForm.class);
    }

    @RequestMapping("/LoginAction")
    protected ModelAndView onSubmit(Object command) {
        try {
            LoginForm login = (LoginForm) command;

            ApplicationContext ctx = new ClassPathXmlApplicationContext(
                    "SpringBlogger-servlet.xml");
            LoginDaoUtil daoUtil = ctx.getBean("LoginDaoUtil",
                    LoginDaoUtil.class);
            boolean isUserValid = daoUtil.isValidUser(login.getUsername(),
                    login.getPassword());

            if (isUserValid)
                return new ModelAndView("success").addObject("name",
                        login.getUsername());
            else
                return new ModelAndView("login", "loginForm", new LoginForm());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return new ModelAndView("login", "loginForm", new LoginForm());

        }

    }
}

login.jsp:-

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>${message}</h1>
<form:form action="LoginAction" method="post" commandName="loginForm">
Username:<form:input path="username"/>
<font color="red"><form:errors path="username"/></font><br/><br/>
Password:<form:password path="password"/>
<font color="red"><form:errors path="password"/></font><br/><br/>
<input type="submit" value="submit">
</form:form>
</body>
</html>

LoginForm.java:-

package com.forms;

public class LoginForm {

    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

}

HomeController.java:-

package com.beans;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.forms.LoginForm;

@Controller
public class HomeController {

    @RequestMapping({"/","/home"})
    public String showHomePage(ModelMap map){
        map.addAttribute("message", "Welcome to Spring Blogger");
        map.addAttribute("loginForm", new LoginForm());
        return "login";
    }
}

Web.xml:-

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringBlogger</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>SpringBlogger</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringBlogger</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

SpringBlogger-servlet.xml:-

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd">


<mvc:annotation-driven/>
<context:component-scan base-package="com.beans" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>

<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost/springblogger"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>


</beans>

请帮忙。

谢谢,马尼什

4

1 回答 1

1

您不能使用 Object 类型来绑定您的表单,而是使用它的类型来装饰@ModelAttribute1

   @RequestMapping("/LoginAction")
   public ModelAndView onSubmit(@ModelAttribute("loginForm") LoginForm command) {

我还看到您正在尝试在控制器内创建一个新的应用程序上下文,这似乎是不必要的,请使用依赖注入/自动装配来代替您的控制器类

于 2013-07-16T22:38:21.257 回答