我正在尝试更新Company对象,您可以在下面的Company.js中看到。当我试图从company.js调用put方法时,它会给我400状态错误,并且执行不会进入CompanyController.java中的put方法。公司对象也在$scope中可用。执行company.js中的最终URL时:http://localhost:8080/Jobkreisel/protected/company/50但它甚至没有进入CompanyController.java中的更新方法,只是移动到$http.put(url,$scope.company,config)方法中的错误块。
company.js
$scope.updateCompany = function (updateCompanyForm) {
if (!updateCompanyForm.$valid) {
$scope.displayValidationError = true;
return;
}
$scope.lastAction = 'update';
var url = '/Jobkreisel/protected/company/' + $scope.company.companyID;
var config = {};
alert("Company scope "+$scope.company.companyID);
alert("Company config "+config);
$http.put(url, $scope.company, config)
.success(function (data) {
alert('In update success');
})
.error(function(data, status, headers, config) {
console.debug(data);
alert('data:' + data);
alert('status: ' + status);
alert('update error');
});
};
CompanyController.java
@Controller
@RequestMapping(value = "/protected/company")
public class CompanyController extends UserBaseController {
@Autowired
private CompanyService companyService;
@RequestMapping(value = "/{companyID}", method = RequestMethod.PUT, produces = "application/json")
public ResponseEntity<?> update(@PathVariable("companyID") int companyId,
@RequestBody Company company,
Locale locale) {
if (companyId != company.getCompanyID()) {
return new ResponseEntity<String>("Bad Request", HttpStatus.BAD_REQUEST);
}
companyService.save(company);
return null;
}
}
请告诉我为什么它不执行成功块。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd ">
<!-- Spring servlet that will handle the requests-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Spring basic configurations -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/spring.xml
</param-value>
</context-param>
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Enconding helper filter -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<servlet-name>dispatcher</servlet-name>
</filter-mapping>
<!-- Encoding utility -->
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<page-encoding>UTF-8</page-encoding>
<trim-directive-whitespaces>true</trim-directive-whitespaces>
</jsp-property-group>
</jsp-config>
</web-app>
网络日志:
远程地址:127.0.0.1:8080请求网址:http://localhost:8080/Jobkreisel/protected/company/50要求方法:PUT状态代码:400错误的请求-请求标头查看源Accept:application/json,text/plain,/Accept-Encoding:gzip,deflate,sdch接受语言:en-US,en;q=0.8连接:保持活动内容长度:175内容类型:application/json;charset=UTF-8Cookie:JSESSIONID=1w425u610rioe主机:localhost:8080原产地:http://localhost:8080参考人:http://localhost:8080/Jobkreisel/protected/company用户代理:Mozilla/5.0(Windows NT 6.1;WOW64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/40.0.2214.115 Safari/537.36请求有效载荷视图源
{companyID:"50",名称:"Agrident GmbH",网址:"http://www.agrident.com/",twitter:",…}ausbildungvideourl:"companyID:"50"员工:"脸书:"名称:"Agrident GmbH"studiovideourl:"twitter:"视频:"网站:"http://www.agrident.com/"响应标头视图源内容长度:0 Pragma:无缓存服务器:Jetty(6.1.21)
web.xml
是不完整的:实际上,您需要/Jobkreisel/*
到控制器的servlet映射。
只需按如下方式替换春季调度器的<url-pattern>
标签:
<!-- Spring servlet that will handle the requests-->
<servlet>
<servlet-name>dispatcher</servlet-name>
...
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/Jobkreisel/*</url-pattern>
</servlet-mapping>
如果调度员的映射正确,你也可以更改从你的angular应用程序调用的url:
var url = '/protected/company/' + $scope.company.companyID;