我一直试图让这段代码运行起来。我刚刚开始使用Spring MVC(而且我对web开发相对较新 - 我的背景是GUI开发/科学)但我认为错误可能会被触发,因为将jspery中的jquery代码链接到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控制器类):
@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;
}
}
我已经在这几天了,并尝试过在这个论坛和其他人发布的所有建议。
非常感谢能够提供帮助的任何人。
答案 0 :(得分:0)
(代表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
干杯。