无需会话的多个JSP表单中的Spring模型属性

时间:2018-11-28 12:52:13

标签: spring-mvc model modelattribute

我的要求:我有一个注册页面,我可以在其中获取用户信息并提交表格。提交后,我将验证用户信息并显示确认页面,该页面具有在上一页中输入的相同详细信息,并在确认页面中具有提交按钮。提交确认后,我需要保留用户数据。

面临的困难:我能够在注册表单控制器中成功获取用户信息,并且能够将模型属性转发到确认页面。现在,如果我在标签属性中显示内容,那么在提交确认控制器时,将获得用户对象的新实例,因此我被迫将属性设置为“文本”字段,并启用编辑以在确认控制器中获取模型属性。

下面是我的代码

//Registration Controller
@RequestMapping("/register")

public ModelAndView processRegistration(@Valid @ModelAttribute("regUser")User registrationUser, BindingResult result, 
        @RequestParam(value="name") String name){

    ModelAndView modelAndView = new ModelAndView();


    if(result.hasErrors())
    {
        LOG.debug("Error Occured While Registration : "+result);
        modelAndView.setViewName("RegisterationPage");
        modelAndView.addObject("name",name);
        modelAndView.addObject("regUser",new User());
        return modelAndView;
    }

    LOG.debug("Process Registration : "+registrationUser.getName());

    modelAndView.setViewName("ConfirmRegistration");
    Utils.checkUser(registrationUser);
    modelAndView.addObject("regUser",registrationUser);
    return modelAndView;
}



//Confirmation Controller
@RequestMapping("/confirmRegistration")
public ModelAndView confirmRegistration(  @ModelAttribute("regUser") User registrationUser){

    LOG.debug("Confirm User registration User :"+registrationUser);
    ModelAndView modelAndView = new ModelAndView();

    if(null == registrationUser.getName() || registrationUser.getName().isEmpty()){

        modelAndView.setViewName("ConnectionInterupt");
        modelAndView.addObject("message","Registration Details Incomplete");
        return modelAndView;
    }

    registrationUser = logic.registerUser(registrationUser);

    modelAndView.addObject("title", "Registration Success");
    modelAndView.addObject("message", "Registration is success with User Id :"+registrationUser.getUserId());

    modelAndView.setViewName("Success");
    return modelAndView;

}

以下是我的JSP:

<!-- Registration JSP  -->
 <form:form action="register" method="post" commandName="regUser" class="form-horizontal">

                <div class="row">
                    <div class="form-group col-md-12">
                        <h2>${name}Registration Form</h2>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-12">
                        <label class="col-md-3 control-lable" for="name">User Name</label>
                        <div class="col-md-7">
                            <form:input path="name" />
                            <div class="has-error">
                                <form:errors path="name" class="help-inline" />
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-12">
                        <label class="col-md-3 control-lable" for="designation">Designation</label>
                        <div class="col-md-7">
                            <form:input path="designation" />
                            <div class="has-error">
                                <form:errors path="designation" class="help-inline" />
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-12">
                        <label class="col-md-3 control-lable" for="email">Email</label>
                        <div class="col-md-7">
                            <form:input path="email" />
                            <div class="has-error">
                                <form:errors path="email" class="help-inline" />
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-12">
                        <label class="col-md-3 control-lable" for="dob">Date Of Birth(mm/dd/yyyy)</label>
                        <div class="col-md-7">
                            <form:input path="dob" />
                            <div class="has-error">
                                <form:errors path="dob" class="help-inline" />
                            </div>
                        </div>
                    </div>
                </div>

                <h2>Address</h2>

                <div class="row">
                    <div class="form-group col-md-12">
                        <label class="col-md-3 control-lable" for="address.addressType">Address Type</label>
                        <div class="col-md-7">
                            <form:select path="address.addressType">
                                    <form:options items= "${addressTypes}"/>
                                </form:select>
                            <div class="has-error">
                                <form:errors path="address.addressType" class="help-inline" />
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-12">
                        <label class="col-md-3 control-lable" for="address.street">Street</label>
                        <div class="col-md-7">
                            <form:input path="address.street" />
                            <div class="has-error">
                                <form:errors path="address.street" class="help-inline" />
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-12">
                        <label class="col-md-3 control-lable" for="address.city">City</label>
                        <div class="col-md-7">
                            <form:input path="address.city" />
                            <div class="has-error">
                                <form:errors path="address.city" class="help-inline" />
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-12">
                        <label class="col-md-3 control-lable" for="address.state">State</label>
                        <div class="col-md-7">
                            <form:input path="address.state" />
                            <div class="has-error">
                                <form:errors path="address.state" class="help-inline" />
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-12">
                        <label class="col-md-3 control-lable" for="address.country">Country</label>
                        <div class="col-md-7">
                            <form:input path="address.country" />
                            <div class="has-error">
                                <form:errors path="address.country" class="help-inline" />
                            </div>
                        </div>
                    </div>
                </div>

                <input align="center" type="submit" value="Register" />
    </form:form>


<!-- Confirmation JSP -->
 <form:form action="confirmRegistration" method="post" commandName="regUser" class="form-horizontal">
                <div class="row">
                    <div class="form-group col-md-12">
                        <h2>Confirm Registration</h2>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-12">
                        <label class="col-md-3 control-lable" for="name">User Name</label>
                        <div class="col-md-7">
                            <form:input path="name" value="${regUser.name}" /> <!-- Needs this value attribute as <Label> and also persist value -->
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-12">
                        <label class="col-md-3 control-lable" for="age">Age</label>
                        <div class="col-md-7">
                            <label class="col-md-3 control-lable">${regUser.age}</label>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-12">
                        <label class="col-md-3 control-lable" for="designation">Profession</label>
                        <div class="col-md-7">
                            <form:input path="designation" value="${regUser.designation}" />  <!-- Needs this value attribute as <Label> and also persist value -->
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-12">
                        <label class="col-md-3 control-lable" for="email">Email</label>
                        <div class="col-md-7">
                            <form:input path="email" value="${regUser.email}"/>  <!-- Needs this value attribute as <Label> and also persist value -->
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-12">
                        <label class="col-md-3 control-lable" for="email">Date Of Birth</label>
                        <div class="col-md-7">
                            <form:input path="dob" value="${regUser.dob}" />   <!-- Needs this value attribute as <Label> and also persist value -->
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-12">
                        <h4>Address Details</h4>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-12">
                        <label class="col-md-3 control-lable" for="address.addressType">Address Type</label>
                        <div class="col-md-7">
                            <form:input path="address.addressType" value="${regUser.address.addressType}" />  <!-- Needs this value attribute as <Label> and also persist value -->
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-12">
                        <label class="col-md-3 control-lable" for="address.street">Street</label>  <!-- Needs this value attribute as <Label> and also persist value -->
                        <div class="col-md-7">
                            <form:input path="address.street" value="${regUser.address.street}" />  <!-- Needs this value attribute as <Label> and also persist value -->
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-12">
                        <label class="col-md-3 control-lable" for="address.street">City</label>
                        <div class="col-md-7">
                            <form:input path="address.city" value="${regUser.address.city}" />  <!-- Needs this value attribute as <Label> and also persist value -->
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-12">
                        <label class="col-md-3 control-lable" for="address.state">State</label>
                        <div class="col-md-7">
                            <form:input path="address.state" value="${regUser.address.state}" />  <!-- Needs this value attribute as <Label> and also persist value -->
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-12">
                        <label class="col-md-3 control-lable" for="address.country">Country</label>
                            <div class="col-md-7">
                            <form:input path="address.country" value="${regUser.address.country}" />  <!-- Needs this value attribute as <Label> and also persist value -->
                        </div>
                    </div>
                </div>

                <input align="center" type="submit" value="Confirm" />
    </form:form>

0 个答案:

没有答案