1

我有 login.jsp 作为我的欢迎页面/主页。像 struts1.2 方法一样,我希望将 login.jsp(用户名和密码)中的数据设置在 pojo(LoginForm)中,并且我需要从控制器中的 pojo 访问用户名和密码。

登录.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<html>
<head>
    <title>Spring 3.2.1 MVC Series</title>
</head>
<body>
   <br><div align='center'> <h2>Welcome to Spring MCV 3.2 Example. </h2><br> </div>
   <br/>
   <form:form method="post" action="login.do" modelAttribute="req">
<label for="UserName :"></label> <form:input path="name"/>
<label for="Password :"/> <form:password path="password"/>
<input name="submit" type="submit"/>
   </form:form>
</body>
</html>

LoginForm pojo 类

public class LoginForm {
private String name;
private String password;
public String getName() {
    return name;
}
public void setName(String name) {      
    this.name = name;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
}

登录控制器

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.spring3.bean.LoginForm;
@Controller

@RequestMapping("/login")
public class LoginController {
@RequestMapping(method = RequestMethod.POST)
public ModelAndView userLogin(@ModelAttribute("req") LoginForm login,BindingResult result)
{
    System.out.println("Inside LoginController...");
    return new ModelAndView("welcome","req",new LoginForm());
}
}

web.xml 和 dispatcher-servlet.xml 文件包含适当的映射。

我收到以下错误。

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

我的 requiremnet 就像在 struts1.2 中一样,我们从 jsp 以 Action 形式获取数据,就像在 spring 3 中一样,因为它是一个迁移项目。

4

1 回答 1

0

要在我的控制器(如 struts1.2 或 2x)的 pojo 中获取表单数据集,您需要使用 commons-beanutils.jar API

此 API 将能够在 bean 中为您提供 HTML 表单数据,只需在构建路径中包含此 jar 以及 commons-logging-1.1.jar(日志记录所需)

将以下 util 类添加到您的项目中

import java.lang.reflect.InvocationTargetException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.BeanUtils;

/**
* @author count
* FormUtil class used BeanUtils class populate method for setting the form values in       Java beans.
*  I made this class a generic class so that we do not need to write same lines again and again in each servlet, 
*  just we need to pass the class of java bean and request in populate method of FormUtil. 
*/
public class FormUtil {

public static <T> T populate(Class<T> clazz, HttpServletRequest request) {
    T object = null;
    try {

        object = (T) clazz.newInstance();
        BeanUtils.populate(object, request.getParameterMap());

    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
    return object;
}
} 

现在你想要你的 POJO 的对象使用下面的代码

 LoginForm loginForm = FormUtil.populate(LoginForm.class, request);
于 2014-03-31T09:47:28.023 回答