在spring MVC控制器中获取null值

时间:2013-06-24 07:07:20

标签: spring model-view-controller

我是春天的新人。我正在开发spring mvc中的登录应用程序。在LoginForm中设置值时,它显示为“null”。请帮我。想摆脱它!! 任何帮助都会得到满足。

我的控制器是:

@Controller
@RequestMapping("loginform.html")
public class LoginController extends MainController {
    private String password;
    private LoginDAO loginDAO;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String showForm(Map model) {
        LoginForm loginForm = new LoginForm();
        model.put("loginForm", loginForm);
        return "loginform";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String processForm(@Valid LoginForm loginForm, BindingResult result,
            Map model) {
        //String userName = "Admin";
        //String password = "root";
        if (result.hasErrors()) {
            return "loginform";

        }

        try {

            loginForm.setUserName(userName);
            loginForm.setPassword(password);
            loginDAO = new LoginDAOImpl();
            LoginForm tempLoginForm = loginDAO.validateUser(loginForm);
            if (tempLoginForm == null) {

                return "loginsuccess";
            }

        } catch (Exception e) {

        }

        model.put("loginForm", loginForm);
        return "loginerror";
    }
}

1 个答案:

答案 0 :(得分:0)

您不能在流程表单方法中设置用户名和密码,您必须从表单对象中读取它。

String userName = login form.getUserName();