我试图发送从数据库发送到我的JSP的对象的发送列表。我设法成功地将数据从JSP发送到我的控制器。我的控制器内的方法采用该参数,填充List(我在调试模式下检查它)并且控制器返回该列表。Spring ajax发送从控制器到jsp的对象列表
@RequestMapping(value="/test.html", method=RequestMethod.GET, produces="application/json")
public @ResponseBody List<ModelVechicle> fetchListModelById(@RequestParam Integer number) {
System.out.println(number);
List<ModelVechicle> modelList = vechicleService.fetchModelById(number);
return modelList;
}
当我尝试让我的JSP中列表中,我得到
HTTP Status 406 -
type Status report
message
description The resource identified by this request is only capable of generating
responses with characteristics not acceptable according to the request "accept" headers.
Apache Tomcat/8.0.32
这里是我的JSP与AJAX代码
<script type="text/javascript">
$(document).ready(function(){
$("#brand").change(onSelectChange);
});
function onSelectChange() {
var selected = $("#brand option:selected");
var output = "";
var number = parseInt(selected.val());
$.ajax({
type: "GET",
url: "test.html",
dataType : 'json',
data: ({number: number}),
success: function(response){
$('#result').html("");
var obj = JSON.parse(response);
$('#result').html(obj.modelName);
},
error: function(xhr,e){
alert('Error while request..'+xhr.responseText);
}
});
if(selected.val() != 0){
output = "You selected brand " + selected.text();
}
$("#output").html(number);
}
而且,这里是我的ModelVechicle类,这是我添加到列表中的对象类:
@Entity
@Table(name = "CARDEALERSHIP.MODEL")
public class ModelVechicle implements Serializable {
private static final long serialVersionUID = 7420515051961158192L;
@Id
@Column(name = "ID")
private Integer modelId;
@Column(name = "MODELNAME")
private String modelName;
@ManyToOne
@JoinColumn(name = "BRANDID")
private Brand brand;
public ModelVechicle(Integer modelId, String modelName, Brand brand) {
super();
this.modelId = modelId;
this.modelName = modelName;
this.brand = brand;
}
public ModelVechicle() {}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((brand == null) ? 0 : brand.hashCode());
result = prime * result + ((modelId == null) ? 0 : modelId.hashCode());
result = prime * result + ((modelName == null) ? 0 : modelName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ModelVechicle other = (ModelVechicle) obj;
if (brand == null) {
if (other.brand != null)
return false;
} else if (!brand.equals(other.brand))
return false;
if (modelId == null) {
if (other.modelId != null)
return false;
} else if (!modelId.equals(other.modelId))
return false;
if (modelName == null) {
if (other.modelName != null)
return false;
} else if (!modelName.equals(other.modelName))
return false;
return true;
}
public Integer getModelId() {
return modelId;
}
public void setModelId(Integer modelId) {
this.modelId = modelId;
}
public String getModelName() {
return modelName;
}
public void setModelName(String modelName) {
this.modelName = modelName;
}
public Brand getBrand() {
return brand;
}
public void setBrand(Brand brand) {
this.brand = brand;
}
有人请解释我该怎么做才能动态地将List列表到JSP页面,并正确显示List成员?
编辑:这是我的web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>CarDealership</display-name>
<welcome-file-list>
<welcome-file>addVechicle.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatchers.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/app-config.xml
</param-value>
</context-param>
</web-app>
您使用的是哪个版本的Spring框架? – jlumietu
@jlumietu 4.2.6.RELEASE –