当用户请求官员/用户页面时,如果我想向用户显示官员/用户详细信息的cookie中有用户名,我有一个用户名(杰夫)存储在 cookie 中。我使用ajax 请求来获取详细信息。
ajax请求转到控制器并获取数据,但是当我将视图返回给用户时,对象为null。因此,没有数据绑定到对象。然后视图为空。我想将具有官员/用户数据的对象返回给用户。
下面是我的代码和萤火虫中请求的屏幕截图。请告诉我哪里出错了。
来自视图的 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.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
截屏
用户名是杰夫
这是来自 firebug,它显示 POST jeff.htm 正在返回数据。我认为返回的视图不正确。
Office_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>