gpt4 book ai didi

java - Spring MVC Controller 没有返回正确的 View

转载 作者:行者123 更新时间:2023-11-29 03:28:39 26 4
gpt4 key购买 nike

当用户请求管理人员/用户页面时,如果 cookie 中有用户名,我会在 cookie 中存储一个用户名 (jeff),我想向用户显示管理人员/用户的详细信息。我使用 ajax 请求 来获取详细信息。

ajax 请求转到 Controller 并获取数据,但是当我将 view 返回给用户时 Objectnull。因此没有数据绑定(bind)到对象。然后 View 是空的。我想将具有官员/用户数据的对象返回给用户。

下面是我的代码和 Firebug 请求的屏幕截图。请告诉我哪里出错了。

来自 View 的 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
});
}

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");

}

带有 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

屏幕截图

用户名是Jeff

这是来自 firebug,它显示 POST jeff.htm 正在返回数据。我认为返回的 View 不正确。

enter image description here

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>

最佳答案

查看您问题中的最后一张图片,我可以看出您正在使用 jQuery,并且看起来您正在收到包含 HTML 的响应。我假设您正在使用 jQuery 发出 ajax 请求(例如使用 $.ajax$.get)。假设您正在使用 jQuery's ajax function .你的 js 代码看起来像这样:

function getOfficerInfo(officerUserName) {
var officerUrl = "/crimeTrack/getOffice/" + officerUserName + ".htm"
$.ajax({
url: officerUrl,
type: "GET"
});
}

这会向服务器发送 GET 请求,服务器会返回响应中显示的 html,您在图像中查看。但是,此代码不处理响应。为此,您需要在传递给 $.ajax 函数的 javascript 对象中添加一个成功字段(或者您可以使用 $.ajax 函数返回一个 promise,但让我们坚持最基本的 :))。

function getOfficerInfo(officerUserName) {
var officerUrl = "/crimeTrack/getOffice/" + officerUserName + ".htm"
$.ajax({
url: officerUrl,
type: "GET",
success: function(data) {
$("#form-container").html(data);
}
});
}

当 ajax 请求成功时,success 函数将被调用,data 变量将保存响应中的数据。在这里,我假设您在页面上有一些元素,其 ID 为 form-container,其内容可以替换为您的 html 响应。

请注意,这里没有错误处理,因此您应该添加一个 error 函数来处理超时、服务器端错误等。

最后。我建议您将数据作为 JSON 传输,并只更新现有表单中的值。 Spring 非常支持使用 @ResponseBody 注释返回 JSON。

关于java - Spring MVC Controller 没有返回正确的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19620693/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com