我有login.jsp作为我的欢迎页面/主页。像struts1.2方法一样,我希望将login.jsp(用户名和密码)中的数据设置在pojo(LoginForm)中,并且我需要从 Controller 中的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;
}
}
登录 Controller
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
"
我的需求网络是在struts1.2中,我们从jsp中以Action形式获取数据,与spring 3中的操作方式相同,因为它是一个迁移项目。
最佳答案
在我的 Controller (如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);
关于java - 我想使用 spring 3 获取 Controller 中 pojo 中的表单数据集,就像 struts1.2 方法一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15761868/