首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ModelAndView弹簧

ModelAndView弹簧
EN

Stack Overflow用户
提问于 2018-06-28 19:23:08
回答 2查看 58关注 0票数 1

详细信息:

我正在测试我的第一个spring项目,我无法在backing中获得表单数据。在这里,我的代码,请帮助我执行这个。

Welcome.jsp是我的登录页,从这里我想采取用户名和密码,并显示在userInfo页面。

控制器:--

代码语言:javascript
复制
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的代码:--只有我添加的快照

代码语言:javascript
复制
<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所包含内容的快照。

代码语言:javascript
复制
<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>

下面是支持Bean的代码:--

代码语言:javascript
复制
package com.accounts.common.backingBean;
public class LoginBackingBean {
    private String userId;
    private String password;

    /*      Getter and setters generated */

}

我可以执行jsp,但是没有设置userId和密码。

EN

回答 2

Stack Overflow用户

发布于 2018-06-28 19:45:44

请尝试从

代码语言:javascript
复制
public String userInfo(@ModelAttribute("userForm")LoginBackingBean login

代码语言:javascript
复制
public String userInfo(@ModelAttribute("loginBackingBean")LoginBackingBean login

作为@ModelAttribute的文档

默认模型属性名称是基于非限定类名从声明的属性类型(即方法参数类型或方法返回类型)中推断出来的:例如,"orderAddress“表示类"mypackage.OrderAddress","orderAddressList”表示"List“。

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/ModelAttribute.html#name--

票数 0
EN

Stack Overflow用户

发布于 2018-06-28 19:46:23

在您的welcome.jsp中,输入上缺少属性名称:

代码语言:javascript
复制
<td> <input type="text" name ="userId"> </td>
<td><input type="password" name="password"> </td>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51081677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档