我有一個用戶名(jeff)存儲在cookie當用戶請求官員/用戶頁面,如果有一個userName在cookie我想向用戶顯示官員/用戶的詳細信息。我使用ajax請求以獲取詳細信息。春天MVC控制器沒有返回正確的視圖
的AJAX請求轉到控制器和然而獲取數據時i中視圖返回給用戶的對象是空。因此沒有數據綁定到對象。該視圖然後是空的。我想向用戶返回具有官員/用戶數據的對象。
下是我的代碼和在螢火蟲請求的屏幕截圖。請告訴我我哪裏錯了。
從視圖Ajax請求
<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
});
}
控制器
@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");
}
錯誤日誌與Logger.info消息
我注意到這一行其中指出該模型是空。爲什麼?
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[email protected]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 {[email protected], 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.s[email protected]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.s[email protected]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: [email protected]
758528 [http-bio-8084-exec-9] DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request
屏幕截圖
的用戶名是傑夫
這是螢火蟲,它顯示了POST jeff.htm正在返回的數據。我認爲被返回的視圖不正確。
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>
如何處理ajax請求返回的數據?看起來您的視圖是正確生成的,只需查看螢火蟲中的響應圖像即可。我懷疑問題是與應該更新你看的表單的JavaScript。 –
我沒有返回對象的成功函數。我應該在對象返回時包含成功函數嗎?它將如何綁定? – devdar
我想從控制器綁定對象,並返回視圖,使對象將顯示在視圖 – devdar