【发布时间】:2018-06-28 11:23:07
【问题描述】:
详情:
我正在测试我的第一个 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,但用户 ID 和密码未设置
【问题讨论】:
-
你好像弄乱了双引号
-
即使我放了“”也没有工作
标签: spring modelandview