Мое требование: у меня есть страница регистрации, где я беру информацию о пользователе и отправляю форму.При отправке я проверяю информацию о пользователе и отображаю страницу подтверждения с теми же данными, которые были введены на предыдущей странице, и имею кнопку отправки на странице подтверждения.При отправке подтверждения мне нужно сохранить данные пользователя.
Столкнулись с трудностями: я успешно могу получить информацию о пользователе в контроллере регистрационной формы и перенаправить атрибут модели на страницу подтверждения.Теперь, если я отображаю содержимое в атрибуте label, то при отправке подтверждения контроллер получает новый экземпляр пользовательского объекта, поэтому я вынужден сделать атрибут в виде текстового поля с возможностью редактирования, чтобы получить атрибут модели в контроллере подтверждения.
Ниже мой код
//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>