![](/img/trans.png)
[英]I want to set up my controller name same as like the input field data in spring boot?
[英]I want to get the form data set in a pojo in my controller using spring 3, as like struts1.2 approach
我將login.jsp作為我的歡迎頁面/主頁。 像struts1.2方法一樣,我希望將我的login.jsp(用戶名和密碼)中的數據設置在pojo(LoginForm)中,並且需要從Controller中的pojo中訪問用戶名和密碼。
login.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;
}
}
LoginController
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中一樣,由於它是一個遷移項目,所以我們從Spring 3中以同樣的方式從jsp中獲取數據。
要在我的控制器(如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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.