Ajax post 到 Spring MVC 控制器导致 "Requested resource is unavailable" 错误
Ajax post to Spring MVC controller results in "Requested resource is unavailable" error
我一直在努力让这段代码起作用。我刚开始使用 Spring MVC(我对网络开发还比较陌生——我的背景是 GUI dev/science),但我认为可能会触发错误,因为链接 [=我的 jsp 中的 54=] 代码到 Spring 控制器。几天来,我一直在努力使它正常工作,但都没有成功,而且我已经尝试了该论坛上发帖人提出的所有建议,但都遇到了类似的问题——但无济于事。因此,我非常感谢您的意见。该项目正在使用 Netbeans、Tomcat 8、Maven、Spring MVC 和 Jquery.
进行开发
projectDashboard.jsp(在WEB-INF/views):
<div class="col-lg-8 col-md-8 col-sm-8">
<div id="projectForm">
<div class="form-group">
<input id="name" name="name" type="text" class="form-control" placeholder="Project name (50 characters or less)"/>
<textarea id="description" name="description" class="form-control" placeholder="Project Description (200 characters or less)"></textarea>
</div>
<div class="form-group">
<input class="btn btn-primary pull-right" id="createProjectButton" value="Create" type="button"/>
</div>
</div>
</div>
JQuery:
<script>
$(document).ready(function(){
$("#createProjectButton").on("click", function(){
var projectJson = {
"name":$("#name").val(),
"description":$("#description").val()
};
$.ajax({
type: "POST",
url: "/ProgressManagerOnline/projectDashboard",
data: JSON.stringify(projectJson),
contentType: "application/json; charset=utf-8",
dataType: "json"
});
});
});
</script>
ProjectDashboard.java(src/main/java 中的Spring MVC 控制器class):
@RequestMapping(value = "/projectDashboard", method = RequestMethod.POST)
public String saveProject(@RequestBody Project project) throws Exception {
return "OK";
}
appconfig中的相关代码-mvc.xml:
<mvc:annotation-driven/>
<mvc:view-controller path="/" view-name="login"/> //home web page - login.jsp
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
Maven pom.xml 包括:
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.5</version>
</dependency>
Tomcat context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/ProgressManagerOnline"/>
Firefox Web 控制台中的错误:
The requested resource is not available. (404 error)
我的Project.java:
@Entity
@Table(name = "projects")
public class Project implements Serializable {
private Long id;
private String name;
private String description;
private java.util.Date dateCreated;
public Project(){};
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id){
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Column(name = "dateCreated", columnDefinition="DATETIME")
@Temporal(TemporalType.TIMESTAMP)
public Date getDateCreated() {
return dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
}
我已经在这里好几天了,并尝试了在这个论坛和其他论坛上发布的每一个建议。
非常感谢任何能提供帮助的人。
我猜你的应用程序中缺少一些配置。
您需要将 json 自动转换为 java 对象,
@RequestBody Project project
因此您需要添加 json 转换器,因此请查看文档:http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.html
在您的配置上下文 xml 中添加以下内容,并在您的 pom.xml、
中添加所需的依赖项
<!-- Configure bean to convert JSON to POJO and vice versa -->
<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
<!-- Configure to plugin JSON as request and response in method handler -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonMessageConverter"/>
</list>
</property>
</bean>
(代表OP发表).
TechBreak 建议的解决方案有效 - 我在 pom.xml 中缺少 Spring 上下文依赖项,在 xml 中缺少额外配置:
<!-- Configure bean to convert JSON to POJO and vice versa -->
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
<!-- Configure to plugin JSON as request and response in method handler -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonMessageConverter"/>
</list>
</property>
</bean>
服务器重启后
mvn clean install
干杯。
我一直在努力让这段代码起作用。我刚开始使用 Spring MVC(我对网络开发还比较陌生——我的背景是 GUI dev/science),但我认为可能会触发错误,因为链接 [=我的 jsp 中的 54=] 代码到 Spring 控制器。几天来,我一直在努力使它正常工作,但都没有成功,而且我已经尝试了该论坛上发帖人提出的所有建议,但都遇到了类似的问题——但无济于事。因此,我非常感谢您的意见。该项目正在使用 Netbeans、Tomcat 8、Maven、Spring MVC 和 Jquery.
进行开发projectDashboard.jsp(在WEB-INF/views):
<div class="col-lg-8 col-md-8 col-sm-8">
<div id="projectForm">
<div class="form-group">
<input id="name" name="name" type="text" class="form-control" placeholder="Project name (50 characters or less)"/>
<textarea id="description" name="description" class="form-control" placeholder="Project Description (200 characters or less)"></textarea>
</div>
<div class="form-group">
<input class="btn btn-primary pull-right" id="createProjectButton" value="Create" type="button"/>
</div>
</div>
</div>
JQuery:
<script>
$(document).ready(function(){
$("#createProjectButton").on("click", function(){
var projectJson = {
"name":$("#name").val(),
"description":$("#description").val()
};
$.ajax({
type: "POST",
url: "/ProgressManagerOnline/projectDashboard",
data: JSON.stringify(projectJson),
contentType: "application/json; charset=utf-8",
dataType: "json"
});
});
});
</script>
ProjectDashboard.java(src/main/java 中的Spring MVC 控制器class):
@RequestMapping(value = "/projectDashboard", method = RequestMethod.POST)
public String saveProject(@RequestBody Project project) throws Exception {
return "OK";
}
appconfig中的相关代码-mvc.xml:
<mvc:annotation-driven/>
<mvc:view-controller path="/" view-name="login"/> //home web page - login.jsp
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
Maven pom.xml 包括:
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.5</version>
</dependency>
Tomcat context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/ProgressManagerOnline"/>
Firefox Web 控制台中的错误:
The requested resource is not available. (404 error)
我的Project.java:
@Entity
@Table(name = "projects")
public class Project implements Serializable {
private Long id;
private String name;
private String description;
private java.util.Date dateCreated;
public Project(){};
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id){
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Column(name = "dateCreated", columnDefinition="DATETIME")
@Temporal(TemporalType.TIMESTAMP)
public Date getDateCreated() {
return dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
}
我已经在这里好几天了,并尝试了在这个论坛和其他论坛上发布的每一个建议。
非常感谢任何能提供帮助的人。
我猜你的应用程序中缺少一些配置。
您需要将 json 自动转换为 java 对象,
@RequestBody Project project
因此您需要添加 json 转换器,因此请查看文档:http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.html
在您的配置上下文 xml 中添加以下内容,并在您的 pom.xml、
中添加所需的依赖项<!-- Configure bean to convert JSON to POJO and vice versa -->
<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
<!-- Configure to plugin JSON as request and response in method handler -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonMessageConverter"/>
</list>
</property>
</bean>
(代表OP发表).
TechBreak 建议的解决方案有效 - 我在 pom.xml 中缺少 Spring 上下文依赖项,在 xml 中缺少额外配置:
<!-- Configure bean to convert JSON to POJO and vice versa -->
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
<!-- Configure to plugin JSON as request and response in method handler -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonMessageConverter"/>
</list>
</property>
</bean>
服务器重启后
mvn clean install
干杯。