![](/img/trans.png)
[英]Display submitted form data in Java Spring MVC bootstrap modal
[英]How to get a form's data after the user submitted it with Spring MVC?
我收到一條錯誤消息:org.springframework.web.util.NestedServletException:請求處理失敗; 嵌套異常是java.lang.ClassCastException:java.lang.Object無法強制轉換為com.crimetrack.business.Login
Login.java
public class Login {
private String userName;
private String password;
private boolean loggedin;
public Login(){};
/**
* @return the loggedin
*/
public boolean isLoggedin() {
return loggedin;
}
/**
* @param loggedin the loggedin to set
*/
public void setLoggedin(boolean loggedin) {
this.loggedin = loggedin;
}
/**
* @param userName
* @param password
*/
public Login(String userName, String password) {
this.userName = userName;
this.password = password;
}
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
}
@Controller
public class AuthenticationController {
private final Logger logger = Logger.getLogger(getClass());
private AuthenticationManager authenticationManager;
private Login login = new Login();
String message = "Congrulations You Have Sucessfully Login";
String errorMsg = "Login Unsucessful";
@RequestMapping(value="login.htm")
public ModelAndView onSubmit(Object command) throws ServletException {
String userName = ((Login)command).getUserName();
String password = ((Login)command).getPassword();
login.setUserName(userName);
login.setPassword(password);
logger.info("Login was set");
logger.info("the username was set to " + login.getUserName());
logger.info("the password was set to " + login.getPassword());
if (authenticationManager.Authenticate(login) == true){
return new ModelAndView("main","welcomeMessage", message);
}
//return new ModelAndView("main","welcomeMessage", message);
return new ModelAndView("login","errorMsg", "Error!!!");
}
}
嘗試此解決方案:
JSP
<form:form action="yourUrl" modelAttribute="login" method="POST">
<% ... %>
</form:form>
調節器
// your method that prints the form
public ModelAndView onGet(@ModelAttribute Login login) {
// return ...
}
@RequestMapping(value="login.htm")
public ModelAndView onSubmit(@ModelAttribute Login login) {
String userName = login.getUserName();
String password = login.getPassword();
// ...
}
說明
注釋@ModelAttribute
與model.addAttribute(String name, Object value)
完全相同。 例如,@ @ModelAttribute Login login
與model.addAttribute("login", new Login());
。
也就是說,使用onGet
方法,您將這樣的對象傳遞給您的視圖。 由於屬性modelAttribute="login"
,標簽<form:form>
將查看模型的屬性列表,以找到名稱為login
的屬性。 如果找不到,則拋出異常。
然后,這是神奇的部分:使用標簽<form:input path="userName" />
,Spring MVC將自動設置bean的屬性userName
,該屬性位於modelAttribute="login"
屬性中,即在您的情況下, login
。 如果您<form:input path="wtf" />
了類似<form:input path="wtf" />
,則會拋出異常,因為bean Login
沒有這樣的屬性。
所以,最后,在你的onSubmit
方法上(再次感謝注釋@ModelAttribute
),你可以訪問以前由Spring MVC自動提取的login
bean。
注意
我個人(幾乎)從不使用ModelAndView
實例,但按以下步驟操作:
// the methods can have the name you want
// not only onGet, onPost, etc. as in servlets
@RequestMapping("url1.htm")
public String loadAnyJsp(@ModelAttribute Login login) {
return "path/to/my/views/login";
}
@RequestMapping("url2.htm")
public String redirectToAnotherController(@ModelAttribute Login login) {
return "redirect:url1.htm";
}
JSP的路徑在web.xml文件中指定,例如:
...
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" p:favorPathExtension="true" p:favorParameter="true" p:ignoreAcceptHeader="true" p:defaultContentType="text/html">
<description>Depending on extension, return html with no decoration (.html), json (.json) or xml (.xml), remaining pages are decoracted</description>
<property name="mediaTypes">
<map>
<entry key="xml" value="application/xml" />
<entry key="json" value="application/json" />
<entry key="html" value="text/html" />
<entry key="action" value="text/html" />
</map>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.xml.MarshallingView" p:marshaller-ref="xstreamMarshaller" />
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</list>
</property>
<property name="viewResolvers">
<list>
<bean id="nameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver">
<description>Maps a logical view name to a View instance configured as a Spring bean</description>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/views/" p:suffix=".jsp" />
</list>
</property>
</bean>
...
您應該閱讀文檔以獲取更多信息(參見16.5解析視圖)。
在'onSubmit'方法中,您將command
為Login
。
顯然它不是。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.