我有一个非常简单的问题。它由表示表单数据的bean组成。我有一个长值的字段。在jsp我写这个:<form:hidden path="id"/>
。当我提交表单时,我收到HTTP Status 400错误。
简而言之,提交一个long值的spring mvc无法将其转换为long来构造我的表单对象。
流程很简单:用户点击通过e公司进行编辑。控制器通过服务检索公司,然后呈现视图,最后用户提交更改..但是当提交长值时我得到了错误。
调试信息告诉我:
DefaultHandlerExceptionResolver [DEBUG] Resolving exception from handler [public java.lang.String ar.com.held.controller.CompanyController.edit(java.lang.Long,org.springframework.ui.Model)]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "save"
这是代码:
我的表单bean:
public class CompanyForm {
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@NotBlank
private String name;
@NotBlank
private String addressStreet;
@NotBlank
private String addressCity;
@NotBlank
private String addressState;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddressStreet() {
return addressStreet;
}
public void setAddressStreet(String addressStreet) {
this.addressStreet = addressStreet;
}
public String getAddressCity() {
return addressCity;
}
public void setAddressCity(String addressCity) {
this.addressCity = addressCity;
}
public String getAddressState() {
return addressState;
}
public void setAddressState(String addressState) {
this.addressState = addressState;
}
}
我的控制员:
@RequestMapping(value={"edit/{companyId}"})
public String edit(@PathVariable Long companyId, Model model){
CompanyForm form = new CompanyForm();
if(companyId!=null){
Company company = companyService.find(companyId);
if(company!=null)
modelMapper.map(company, form);
}
model.addAttribute("form", form);
return "company/edit";
}
@RequestMapping(value={"new"})
public String edit(Model model){
return this.edit(null, model);
}
@RequestMapping(value="save", method={RequestMethod.POST})
public String save(@ModelAttribute("form") @Valid CompanyForm companyForm, BindingResult result){
if(result.hasErrors())
return "company/edit";
Company company;
if(companyForm.getId()!=null)
company = companyService.find(companyForm.getId());
else
company = new Company();
modelMapper.map(companyForm, company);
companyService.save(company, getLoggedUser());
return "redirect:list";
}
我的jsp:
<form:form method="post" action="save" modelAttribute="form" cssClass="form-horizontal" >
<fieldset>
<form:hidden path="id"/>
++++ others fields +++
<div class="form-actions">
<input type="submit" value="Guardar" class="btn btn-primary btn-large"/>
</div>
</fieldset>
</form:form>
调试信息
2012-02-17 18:17:45 AntPathRequestMatcher [DEBUG] Checking match of request : '/companies/edit/save'; against '/resources/**'
2012-02-17 18:17:45 FilterChainProxy [DEBUG] /companies/edit/save at position 1 of 10 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
2012-02-17 18:17:45 HttpSessionSecurityContextRepository [DEBUG] Obtained a valid SecurityContext from SPRING_SECURITY_CONTEXT: 'org.springframework.security.core.context.SecurityContextImpl@bcc22d3c: Authentication: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@bcc22d3c: Principal: ar.com.held.auth.UserDetails@1421801; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@166c8: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: 93B19E8B81EBB7F4BFEECF4FFFC46593; Granted Authorities: COMPANY_OWNER'
2012-02-17 18:17:45 FilterChainProxy [DEBUG] /companies/edit/save at position 2 of 10 in additional filter chain; firing Filter: 'LogoutFilter'
2012-02-17 18:17:45 FilterChainProxy [DEBUG] /companies/edit/save at position 3 of 10 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter'
2012-02-17 18:17:45 FilterChainProxy [DEBUG] /companies/edit/save at position 4 of 10 in additional filter chain; firing Filter: 'DefaultLoginPageGeneratingFilter'
2012-02-17 18:17:45 FilterChainProxy [DEBUG] /companies/edit/save at position 5 of 10 in additional filter chain; firing Filter: 'RequestCacheAwareFilter'
2012-02-17 18:17:45 FilterChainProxy [DEBUG] /companies/edit/save at position 6 of 10 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
2012-02-17 18:17:45 FilterChainProxy [DEBUG] /companies/edit/save at position 7 of 10 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter'
2012-02-17 18:17:45 AnonymousAuthenticationFilter [DEBUG] SecurityContextHolder not populated with anonymous token, as it already contained: 'org.springframework.security.authentication.UsernamePasswordAuthenticationToken@bcc22d3c: Principal: ar.com.held.auth.UserDetails@1421801; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@166c8: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: 93B19E8B81EBB7F4BFEECF4FFFC46593; Granted Authorities: COMPANY_OWNER'
2012-02-17 18:17:45 FilterChainProxy [DEBUG] /companies/edit/save at position 8 of 10 in additional filter chain; firing Filter: 'SessionManagementFilter'
2012-02-17 18:17:45 FilterChainProxy [DEBUG] /companies/edit/save at position 9 of 10 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
2012-02-17 18:17:45 FilterChainProxy [DEBUG] /companies/edit/save at position 10 of 10 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
2012-02-17 18:17:45 AntPathRequestMatcher [DEBUG] Checking match of request : '/companies/edit/save'; against '/companies/*'
2012-02-17 18:17:45 FilterSecurityInterceptor [DEBUG] Public object - authentication not attempted
2012-02-17 18:17:45 FilterChainProxy [DEBUG] /companies/edit/save reached end of additional filter chain; proceeding with original chain
2012-02-17 18:17:45 DispatcherServlet [DEBUG] DispatcherServlet with name 'spring' processing POST request for [/Held/companies/edit/save]
2012-02-17 18:17:45 RequestMappingHandlerMapping [DEBUG] Looking up handler method for path /companies/edit/save
2012-02-17 18:17:45 RequestMappingHandlerMapping [DEBUG] Returning handler method [public java.lang.String ar.com.held.controller.CompanyController.edit(java.lang.Long,org.springframework.ui.Model)]
2012-02-17 18:17:45 DefaultListableBeanFactory [DEBUG] Returning cached instance of singleton bean 'companyController'
2012-02-17 18:17:45 StandardEnvironment [DEBUG] Initializing new StandardEnvironment
2012-02-17 18:17:45 StandardEnvironment [DEBUG] Adding [systemProperties] PropertySource with lowest search precedence
2012-02-17 18:17:45 StandardEnvironment [DEBUG] Adding [systemEnvironment] PropertySource with lowest search precedence
2012-02-17 18:17:45 StandardEnvironment [DEBUG] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
2012-02-17 18:17:45 StandardEnvironment [DEBUG] Initializing new StandardEnvironment
2012-02-17 18:17:45 StandardEnvironment [DEBUG] Adding [systemProperties] PropertySource with lowest search precedence
2012-02-17 18:17:45 StandardEnvironment [DEBUG] Adding [systemEnvironment] PropertySource with lowest search precedence
2012-02-17 18:17:45 StandardEnvironment [DEBUG] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
2012-02-17 18:17:45 StandardEnvironment [DEBUG] Initializing new StandardEnvironment
2012-02-17 18:17:45 StandardEnvironment [DEBUG] Adding [systemProperties] PropertySource with lowest search precedence
2012-02-17 18:17:45 StandardEnvironment [DEBUG] Adding [systemEnvironment] PropertySource with lowest search precedence
2012-02-17 18:17:45 StandardEnvironment [DEBUG] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
2012-02-17 18:17:45 StandardEnvironment [DEBUG] Initializing new StandardEnvironment
2012-02-17 18:17:45 StandardEnvironment [DEBUG] Adding [systemProperties] PropertySource with lowest search precedence
2012-02-17 18:17:45 StandardEnvironment [DEBUG] Adding [systemEnvironment] PropertySource with lowest search precedence
2012-02-17 18:17:45 StandardEnvironment [DEBUG] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
2012-02-17 18:17:45 StandardEnvironment [DEBUG] Initializing new StandardEnvironment
2012-02-17 18:17:45 StandardEnvironment [DEBUG] Adding [systemProperties] PropertySource with lowest search precedence
2012-02-17 18:17:45 StandardEnvironment [DEBUG] Adding [systemEnvironment] PropertySource with lowest search precedence
2012-02-17 18:17:45 StandardEnvironment [DEBUG] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
2012-02-17 18:17:45 ExceptionHandlerExceptionResolver [DEBUG] Resolving exception from handler [public java.lang.String ar.com.held.controller.CompanyController.edit(java.lang.Long,org.springframework.ui.Model)]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "save"
2012-02-17 18:17:45 ResponseStatusExceptionResolver [DEBUG] Resolving exception from handler [public java.lang.String ar.com.held.controller.CompanyController.edit(java.lang.Long,org.springframework.ui.Model)]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "save"
2012-02-17 18:17:45 DefaultHandlerExceptionResolver [DEBUG] Resolving exception from handler [public java.lang.String ar.com.held.controller.CompanyController.edit(java.lang.Long,org.springframework.ui.Model)]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "save"
2012-02-17 18:17:45 DispatcherServlet [DEBUG] Null ModelAndView returned to DispatcherServlet with name 'spring': assuming HandlerAdapter completed request handling
2012-02-17 18:17:45 DispatcherServlet [DEBUG] Successfully completed request
2012-02-17 18:17:45 ExceptionTranslationFilter [DEBUG] Chain processed normally
2012-02-17 18:17:45 SecurityContextPersistenceFilter [DEBUG] SecurityContextHolder now cleared, as request processing completed
2012-02-17 18:26:56 AntPathRequestMatcher [DEBUG] Checking match of request : '/companies/edit/save'; against '/resources/**'
2012-02-17 18:26:56 FilterChainProxy [DEBUG] /companies/edit/save at position 1 of 10 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
2012-02-17 18:26:56 HttpSessionSecurityContextRepository [DEBUG] Obtained a valid SecurityContext from SPRING_SECURITY_CONTEXT: 'org.springframework.security.core.context.SecurityContextImpl@bcc22d3c: Authentication: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@bcc22d3c: Principal: ar.com.held.auth.UserDetails@1421801; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@166c8: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: 93B19E8B81EBB7F4BFEECF4FFFC46593; Granted Authorities: COMPANY_OWNER'
2012-02-17 18:26:56 FilterChainProxy [DEBUG] /companies/edit/save at position 2 of 10 in additional filter chain; firing Filter: 'LogoutFilter'
2012-02-17 18:26:56 FilterChainProxy [DEBUG] /companies/edit/save at position 3 of 10 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter'
2012-02-17 18:26:56 FilterChainProxy [DEBUG] /companies/edit/save at position 4 of 10 in additional filter chain; firing Filter: 'DefaultLoginPageGeneratingFilter'
2012-02-17 18:26:56 FilterChainProxy [DEBUG] /companies/edit/save at position 5 of 10 in additional filter chain; firing Filter: 'RequestCacheAwareFilter'
2012-02-17 18:26:56 FilterChainProxy [DEBUG] /companies/edit/save at position 6 of 10 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
2012-02-17 18:26:56 FilterChainProxy [DEBUG] /companies/edit/save at position 7 of 10 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter'
2012-02-17 18:26:56 AnonymousAuthenticationFilter [DEBUG] SecurityContextHolder not populated with anonymous token, as it already contained: 'org.springframework.security.authentication.UsernamePasswordAuthenticationToken@bcc22d3c: Principal: ar.com.held.auth.UserDetails@1421801; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@166c8: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: 93B19E8B81EBB7F4BFEECF4FFFC46593; Granted Authorities: COMPANY_OWNER'
2012-02-17 18:26:56 FilterChainProxy [DEBUG] /companies/edit/save at position 8 of 10 in additional filter chain; firing Filter: 'SessionManagementFilter'
2012-02-17 18:26:56 FilterChainProxy [DEBUG] /companies/edit/save at position 9 of 10 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
2012-02-17 18:26:56 FilterChainProxy [DEBUG] /companies/edit/save at position 10 of 10 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
2012-02-17 18:26:56 AntPathRequestMatcher [DEBUG] Checking match of request : '/companies/edit/save'; against '/companies/*'
2012-02-17 18:26:56 FilterSecurityInterceptor [DEBUG] Public object - authentication not attempted
2012-02-17 18:26:56 FilterChainProxy [DEBUG] /companies/edit/save reached end of additional filter chain; proceeding with original chain
2012-02-17 18:26:56 DispatcherServlet [DEBUG] DispatcherServlet with name 'spring' processing POST request for [/Held/companies/edit/save]
2012-02-17 18:26:56 RequestMappingHandlerMapping [DEBUG] Looking up handler method for path /companies/edit/save
2012-02-17 18:26:56 RequestMappingHandlerMapping [DEBUG] Returning handler method [public java.lang.String ar.com.held.controller.CompanyController.edit(java.lang.Long,org.springframework.ui.Model)]
2012-02-17 18:26:56 DefaultListableBeanFactory [DEBUG] Returning cached instance of singleton bean 'companyController'
2012-02-17 18:26:56 StandardEnvironment [DEBUG] Initializing new StandardEnvironment
2012-02-17 18:26:56 StandardEnvironment [DEBUG] Adding [systemProperties] PropertySource with lowest search precedence
2012-02-17 18:26:56 StandardEnvironment [DEBUG] Adding [systemEnvironment] PropertySource with lowest search precedence
2012-02-17 18:26:56 StandardEnvironment [DEBUG] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
2012-02-17 18:26:56 StandardEnvironment [DEBUG] Initializing new StandardEnvironment
2012-02-17 18:26:56 StandardEnvironment [DEBUG] Adding [systemProperties] PropertySource with lowest search precedence
2012-02-17 18:26:56 StandardEnvironment [DEBUG] Adding [systemEnvironment] PropertySource with lowest search precedence
2012-02-17 18:26:56 StandardEnvironment [DEBUG] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
2012-02-17 18:26:56 StandardEnvironment [DEBUG] Initializing new StandardEnvironment
2012-02-17 18:26:56 StandardEnvironment [DEBUG] Adding [systemProperties] PropertySource with lowest search precedence
2012-02-17 18:26:56 StandardEnvironment [DEBUG] Adding [systemEnvironment] PropertySource with lowest search precedence
2012-02-17 18:26:56 StandardEnvironment [DEBUG] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
2012-02-17 18:26:56 StandardEnvironment [DEBUG] Initializing new StandardEnvironment
2012-02-17 18:26:56 StandardEnvironment [DEBUG] Adding [systemProperties] PropertySource with lowest search precedence
2012-02-17 18:26:56 StandardEnvironment [DEBUG] Adding [systemEnvironment] PropertySource with lowest search precedence
2012-02-17 18:26:56 StandardEnvironment [DEBUG] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
2012-02-17 18:26:56 StandardEnvironment [DEBUG] Initializing new StandardEnvironment
2012-02-17 18:26:56 StandardEnvironment [DEBUG] Adding [systemProperties] PropertySource with lowest search precedence
2012-02-17 18:26:56 StandardEnvironment [DEBUG] Adding [systemEnvironment] PropertySource with lowest search precedence
2012-02-17 18:26:56 StandardEnvironment [DEBUG] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
2012-02-17 18:26:56 ExceptionHandlerExceptionResolver [DEBUG] Resolving exception from handler [public java.lang.String ar.com.held.controller.CompanyController.edit(java.lang.Long,org.springframework.ui.Model)]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "save"
2012-02-17 18:26:56 ResponseStatusExceptionResolver [DEBUG] Resolving exception from handler [public java.lang.String ar.com.held.controller.CompanyController.edit(java.lang.Long,org.springframework.ui.Model)]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "save"
2012-02-17 18:26:56 DefaultHandlerExceptionResolver [DEBUG] Resolving exception from handler [public java.lang.String ar.com.held.controller.CompanyController.edit(java.lang.Long,org.springframework.ui.Model)]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "save"
2012-02-17 18:26:56 DispatcherServlet [DEBUG] Null ModelAndView returned to DispatcherServlet with name 'spring': assuming HandlerAdapter completed request handling
2012-02-17 18:26:56 DispatcherServlet [DEBUG] Successfully completed request
2012-02-17 18:26:56 ExceptionTranslationFilter [DEBUG] Chain processed normally
2012-02-17 18:26:56 SecurityContextPersistenceFilter [DEBUG] SecurityContextHolder now cleared, as request processing completed
答案 0 :(得分:0)
嗯,问题出在这里:
当我编辑一家公司时,我指的是/ companies / edit / {id}
<form:form method="post" action="save" modelAttribute="form" cssClass="form-horizontal" >
所以,当我提交表单时,网址看起来像:/ companies / edit / save和我的控制器期望/公司/保存..
所以,我必须在form标签中指明action属性的绝对路径。喜欢这里:
<form:form method="post" action="${pageContext.request.contextPath}/companies/save" modelAttribute="form" cssClass="form-horizontal" >
和问题解决..这是一个网址问题。