1

Details:

I am testing my first spring project , I am unable to get form data in backing bean . Here my code please help me execute this.

Welcome.jsp is my landing page from here I want to take userid and password and show in userInfo page.

Controller :--

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";
    }
}

Here is code for Welcome JSP :-- Only snapshot I am adding

<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>

Here is code for UserInfo JSP :

I am putting only snapshot of what the jsp contains

<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>

Here is code for Backing Bean :--

package com.accounts.common.backingBean;
public class LoginBackingBean {
    private String userId;
    private String password;

    /*      Getter and setters generated */

}

I am able to execute the jsp(s) but the userId and Password is not getting set

Addy
  • 11
  • 1

2 Answers2

0

Please try to change from

public String userInfo(@ModelAttribute("userForm")LoginBackingBean login

to

public String userInfo(@ModelAttribute("loginBackingBean")LoginBackingBean login

as @ModelAttribute's document

The default model attribute name is inferred from the declared attribute type (i.e. the method parameter type or method return type), based on the non-qualified class name: e.g. "orderAddress" for class "mypackage.OrderAddress", or "orderAddressList" for "List".

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

khoibv
  • 369
  • 1
  • 2
  • 9
  • @RequestMapping(value= "/welcome" ,method = RequestMethod.GET) public ModelAndView welcome() { return new ModelAndView("welcome", "userForm", new LoginBackingBean()); } – Addy Jun 28 '18 at 11:57
0

In your welcome.jsp you are missing property name on inputs:

<td> <input type="text" name ="userId"> </td>
<td><input type="password" name="password"> </td>