I have a userName(jeff) stored in a cookie when the user request the officer/user page if there is a userName in the cookie i want show the user the officer/user details. I use an ajax request to get the details.
The ajax request goes to the Controller and gets the data however when i return the view to the user the Object is null. Therefore no data is bind to the object. The view is then empty. I would like to return to the user the object which has the officer/user data.
Under is my code and a screen shot of the request in firebug. Please tell me where i am going wrong.
Ajax request from view
<script type="text/javascript">
function getCookie(name) {
var regexp = new RegExp("(?:^" + name + "|;\s*"+ name + ")=(.*?)(?:;|$)", "g");
var result = regexp.exec(document.cookie);
return (result === null) ? null : result[1];
}
var userName = getCookie("userName");
console.log(getCookie("userName"));
if(userName != null & userName != 'Guest'){
alert('Redirecting to get officer');
$.ajax({
type:'POST',
url:'getOfficer/'+ userName + '.htm',
contentType: "application/json",
async: false,
cache: false,
data:userName
});
}
Controller
@RequestMapping(value="getOfficer/{userName}.htm", method = RequestMethod.POST)
public ModelAndView getOfficer(@PathVariable String userName,@ModelAttribute Officers officer,
BindingResult result,ModelMap m,Model model,HttpServletRequest request,
HttpServletResponse response) {
logger.info("In get Officer by userName");
try{
model.addAttribute("officers",officerManager.getOfficer(userName));
}catch(Exception e){
logger.error("Exception In Officer Controller getOfficer/{userName} " + e.getMessage());
request.setAttribute("error",e.getMessage());
}
logger.info("about to return new officer_registration");
logger.info("Officer Badge Number is "+officer.getBadgeNo());
logger.info("Officer First and last name is "+officer.getfName() + " - " + officer.getlName());
return new ModelAndView("officer_registration");
}
Error Log with Logger.info messages
I noticed this line which states the model is null. Why?
758511 [http-bio-8084-exec-9] DEBUG org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod - Method [postOfficer] returned [ModelAndView: reference to view with name 'officer_registration'; model is null
758470 [http-bio-8084-exec-9] INFO com.crimetrack.web.OfficerController - In get Officer by userName
758470 [http-bio-8084-exec-9] INFO com.crimetrack.jdbc.JdbcOfficersDAO - Getting Officer in getOfficer(String userName)
758506 [http-bio-8084-exec-9] DEBUG org.springframework.jdbc.core.JdbcTemplate - Executing prepared SQL query
758506 [http-bio-8084-exec-9] DEBUG org.springframework.jdbc.core.JdbcTemplate - Executing prepared SQL statement [SELECT * FROM crimetrack.tblofficers WHERE userName = ?]
758506 [http-bio-8084-exec-9] DEBUG org.springframework.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
758508 [http-bio-8084-exec-9] DEBUG org.springframework.jdbc.core.StatementCreatorUtils - Setting SQL statement parameter value: column index 1, parameter value [jeff], value class [java.lang.String], SQL type unknown
758510 [http-bio-8084-exec-9] DEBUG org.springframework.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource
758511 [http-bio-8084-exec-9] INFO com.crimetrack.web.OfficerController - about to return new officer_registration
758511 [http-bio-8084-exec-9] INFO com.crimetrack.web.OfficerController - Officer Badge Number is null
758511 [http-bio-8084-exec-9] INFO com.crimetrack.web.OfficerController - Officer First and last name is null - null
758511 [http-bio-8084-exec-9] DEBUG org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod - Method [postOfficer] returned [ModelAndView: reference to view with name 'officer_registration'; model is null]
758511 [http-bio-8084-exec-9] DEBUG org.springframework.web.method.support.InvocableHandlerMethod - Invoking [initBinder] method with arguments [org.springframework.web.servlet.mvc.method.annotation.ExtendedServletRequestDataBinder@b836456]
758512 [http-bio-8084-exec-9] DEBUG org.springframework.web.method.support.InvocableHandlerMethod - Method [initBinder] returned [null]
758512 [http-bio-8084-exec-9] DEBUG org.springframework.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'officer_registration'; URL [/WEB-INF/jsp/officer_registration.jsp]] in DispatcherServlet with name 'crimetrack'
758512 [http-bio-8084-exec-9] DEBUG org.springframework.web.servlet.view.JstlView - Rendering view with name 'officer_registration' with model {officers=com.crimetrack.business.Officers@5f305001, org.springframework.validation.BindingResult.officers=org.springframework.validation.BeanPropertyBindingResult: 0 errors} and static attributes {}
758512 [http-bio-8084-exec-9] DEBUG org.springframework.web.servlet.view.JstlView - Added model object 'officers' of type [com.crimetrack.business.Officers] to request in view with name 'officer_registration'
758512 [http-bio-8084-exec-9] DEBUG org.springframework.web.servlet.view.JstlView - Added model object 'userName' of type [java.lang.String] to request in view with name 'officer_registration'
758512 [http-bio-8084-exec-9] DEBUG org.springframework.web.servlet.view.JstlView - Added model object 'org.springframework.validation.BindingResult.officers' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'officer_registration'
758513 [http-bio-8084-exec-9] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - No bean named 'requestDataValueProcessor' found in org.springframework.beans.factory.support.DefaultListableBeanFactory@726b5b3c: defining beans [citizenManager,validateCitizenManager,citizenDao,citizenTypeManager,citizenTypeDao,markerManager,markerDao,crimeHotSpotManager,crimeHotSpotDao,crimeManager,crimeRegistrationValidation,crimeDao,colorManager,colorDao,monitoringTypeManager,monitoringTypeDao,monitoringManager,monitoringDao,ethnicityManager,ethnicityDao,crimeLevelManager,crimeLevelDao,crimeTypeManager,crimeTypeDao,statusManager,statusDao,crimeCategoryManager,crimeCategoryDao,maritalStatusManager,maritalStatusDao,occupationManager,occupationDao,officerManager,countryManager,countryDao,authenticationManager,loginDao,divisionManager,divisionDao,positionManager,positionDao,genderManager,genderDao,officerRegistrationValidation,validateUserManager,officerDao,dataSource,propertyConfigurer,transactionManager]; root of factory hierarchy
758513 [http-bio-8084-exec-9] DEBUG org.springframework.web.servlet.view.JstlView - Forwarding to resource [/WEB-INF/jsp/officer_registration.jsp] in InternalResourceView 'officer_registration'
758514 [http-bio-8084-exec-9] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - No bean named 'requestDataValueProcessor' found in org.springframework.beans.factory.support.DefaultListableBeanFactory@726b5b3c: defining beans [citizenManager,validateCitizenManager,citizenDao,citizenTypeManager,citizenTypeDao,markerManager,markerDao,crimeHotSpotManager,crimeHotSpotDao,crimeManager,crimeRegistrationValidation,crimeDao,colorManager,colorDao,monitoringTypeManager,monitoringTypeDao,monitoringManager,monitoringDao,ethnicityManager,ethnicityDao,crimeLevelManager,crimeLevelDao,crimeTypeManager,crimeTypeDao,statusManager,statusDao,crimeCategoryManager,crimeCategoryDao,maritalStatusManager,maritalStatusDao,occupationManager,occupationDao,officerManager,countryManager,countryDao,authenticationManager,loginDao,divisionManager,divisionDao,positionManager,positionDao,genderManager,genderDao,officerRegistrationValidation,validateUserManager,officerDao,dataSource,propertyConfigurer,transactionManager]; root of factory hierarchy
758528 [http-bio-8084-exec-9] DEBUG org.springframework.web.servlet.DispatcherServlet - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@51dd475f
758528 [http-bio-8084-exec-9] DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request
Screen Shot
The userName is Jeff
This is from firebug and it shows the POST jeff.htm is returning data. I think the view being returned is incorrect.
Officer_Registration.jsp
</head>
<body>
<form:form id="officerRegistration" name="officerRegistration" method="post" modelAttribute="officers" action="officer_registration.htm">
<ol>
<li>
<label>Badge No</label>
<form:input path="badgeNo" id="badgeNo" title="Enter a Valid Badge Number"
readonly="${badgeNoStatus}" class="formData" />
<form:errors path="badgeNo" class="errors" />
<label id="badgeNoErr"></label>
</li>
<li>
<form:label for="userName" path="userName">User Name</form:label>
<form:input path="userName" id="userName" title="Choose A Unique UserName"
readonly="${userNameStatus}" class="formData" />
<form:errors path="userName" class="errors" />
<label id="userNameErr"></label>
</li>
<li>
<label>Password</label>
<form:password path="password" id="password" class="formData" />
<form:errors path="password" class="errors" />
</li>
<li>
<label>Re-Enter Password</label>
<form:password path="password2" id="password2"
class="formData" />
<form:errors path="password2" class="errors" />
</li>
<li>
<label>e-Mail Address</label>
<form:input path="emailAdd" id="emailAdd" title="Enter eMail Address"
class="formData" />
<form:errors path="emailAdd" class="errors" />
</li>
<li>
<label>First Name</label>
<form:input path="fName" id="fName" title="Your First Name"
class="formData" />
<form:errors path="fName" class="errors" />
</li>
<li>
<label>Last Name</label>
<form:input path="lName" id="lName" title="Your Last Name"
class="formData" />
<form:errors path="lName" class="errors" />
</li>
<li>
ModelAndView, use another constructor to add your dataModelAndViewContainerwill aggregate the attributes in the returnedModelAndViewand those in theModelMapandModelarguments passed to the method.