详细信息:
我正在测试我的第一个Spring项目,无法在backing bean中获得表单数据。在这里,我的代码请帮助我执行此操作。
Welcome.jsp是我的登陆页面,我想要输入用户名和密码并显示在userInfo页面中。
控制器:-
package com.accounts.common.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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.accounts.common.backingBean.LoginBackingBean;
/**
* Handles requests for the application welcome page.
*/
@Controller
public class WelcomeController {
/**
* Simply selects the welcome view to render by returning void and relying
* on the default request-to-view-translator.
*/
@RequestMapping(value= "/welcome" ,method = RequestMethod.GET)
**public ModelAndView welcome() {
return new ModelAndView("welcome", "userForm", new LoginBackingBean());
}**
@RequestMapping(value="/userInfo" ,method = RequestMethod.POST)
public String userInfo(@ModelAttribute("userForm")LoginBackingBean login ,
ModelMap model){
model.addAttribute("userId" , login.getUserId());
model.addAttribute("password" , login.getPassword());
return "userInfo";
}
}
这是Welcome JSP的代码:-仅添加我要添加的快照
<body>
<form id=login method="POST" action="/AccountingSW/userInfo" >
<div align="center">
<table>
<tr>
<td>User ID : </td>
<td> <input type="text"> </td>
</tr>
<tr>
<td>Password :</td>
<td><input type="password"> </td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value ="Login">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<label style="font-size: 10;color: red">Already existing Member</label>
</td>
</tr>
<tr><td colspan="2" align="center">OR</td></tr>
<tr><td colspan="2" align="center">
<input type="submit" value ="SignUp"></td></tr>
<tr>
<td colspan="2" align="center">
<label style="font-size: 10;color: red">New Member</label>
</td>
</tr>
</table>
</div>
</form >
</body>
</html>
这是UserInfo JSP的代码:
我只放了jsp包含的快照
<title>Hello ${userForm.userId}</title>
</head>
<body>
<h2>User Info</h2>
<table>
<tr>
<td>UserId</td>
<td>${userForm.userId}</td>
</tr>
<tr>
<td>Password</td>
<td>${userForm.password}</td>
</tr>
</table>
</body>
</html>
这是Backing Bean的代码:-
package com.accounts.common.backingBean;
public class LoginBackingBean {
private String userId;
private String password;
/* Getter and setters generated */
}
我能够执行jsp,但是没有设置userId和Password
答案 0 :(得分:0)
请尝试将其更改为
public String userInfo(@ModelAttribute("userForm")LoginBackingBean login
到
public String userInfo(@ModelAttribute("loginBackingBean")LoginBackingBean login
作为@ModelAttribute的文档
默认模型属性名称是从声明的 属性类型(即方法参数类型或方法返回类型), 根据不合格的类别名称:例如类的“ orderAddress” “ mypackage.OrderAddress”或“ orderAddressList” “列表”。
答案 1 :(得分:0)
在您的welcome.jsp中,输入中缺少属性名称:
<td> <input type="text" name ="userId"> </td>
<td><input type="password" name="password"> </td>