使用model属性将数据从jsp发送到控制器

时间:2016-07-07 04:57:01

标签: spring jsp

<body>
<jsp:useBean id="loginBean" class="com.ss.sms.bean.LoginBean"></jsp:useBean>
    <div class="row">
        <div class="col-xs-10 col-xs-offset-1 col-sm-8 col-sm-offset-2 col-md-4 col-md-offset-4">
            <div class="login-panel panel panel-default">
                <div class="panel-heading">Log in</div>
                <div class="panel-body">

                    form:form id="loginForm"  method="post" action="login" modelAttribute="login">
                    <fieldset>
                        <div class="form-group">
                            <input class="form-control" placeholder="UserId" name="userId" type="text" autofocus="autofocus" value=${loginBean.userName}>
                        </div>
                        <div class="form-group">
                            <input class="form-control" placeholder="Password" name="password" type="password" value=${loginBean.password}>
                        </div>
                        <div class="checkbox">
                            <label>
                                <input name="remember" type="checkbox" value="Remember Me">Remember Me
                            </label>
                        </div>
                        <input type="submit" class="btn btn-primary">
                        <div style="color: red">${error}</div>
                    </fieldset>
                </form:form>
            </div>
        </div>
    </div><!-- /.col-->
</div><!-- /.row -->    

@RequestMapping(value="/login",method = RequestMethod.POST)
public ModelAndView login(Model md,@ModelAttribute("login")LoginBean loginBean) {
    System.out.println("hello done");
    System.out.println(loginBean.getUserName());
    ModelAndView model = null;
    if (loginBean != null && loginBean.getUserName() != null & loginBean.getPassword() != null) {
        if (loginBean.getUserName().equals("santosh") && loginBean.getPassword().equals("Santosh@123")) {
            model = new ModelAndView("hello");
            return model;
        } else {
            model = new ModelAndView("login");
            return model;
        }
    } else {
        model = new ModelAndView("login");
        return model;
    }
}

1 个答案:

答案 0 :(得分:0)

如果我从您的代码中理解正确,则您在查看页面时遇到问题。

在ModelAndView中,只添加视图而不是模型

所以你需要添加对象

因此,代码看起来像那样

@RequestMapping(value="/login",method = RequestMethod.POST) public ModelAndView login(Model md,@ModelAttribute("login")LoginBean loginBean) {
    System.out.println("hello done");
    System.out.println(loginBean.getUserName());
    ModelAndView model = null;
    if (loginBean != null && loginBean.getUserName() != null & loginBean.getPassword() != null) {
        if (loginBean.getUserName().equals("santosh") && loginBean.getPassword().equals("Santosh@123")) {
           //if the view hello expects an object add it here
            model = new ModelAndView("hello");
            return model;
        } else {
            model = new ModelAndView("login").addObject("login",loginBean);
            return model;
        }
    } else {
        model = new ModelAndView("login").addObject("login",loginBean);
        return model;
    } 
}