【问题标题】:Ajax post to Spring MVC controller results in "Requested resource is unavailable" errorAjax 发布到 Spring MVC 控制器导致“请求的资源不可用”错误
【发布时间】:2016-12-08 01:55:13
【问题描述】:

我一直试图让这段代码工作。我刚刚开始使用 Spring MVC(而且我对 web 开发相对较新——我的背景是 GUI 开发/科学),但我认为可能会触发错误,因为将我的 jsp 中的 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 上下文.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/ProgressManagerOnline"/>

Firefox 网页控制台出错:

The requested resource is not available. (404 error)

我的项目.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;
    }    
}

我已经在这里工作了好几天了,并且已经尝试了这个论坛和其他人发布的每一个建议。

非常感谢任何可以提供帮助的人。

【问题讨论】:

  • 您的完整网址是什么?它应该是 /ProgressManagerOnline/projectDashboard
  • 是的......我已经尝试过了 - 不幸的是我仍然收到错误。
  • 我已更改代码以反映您建议的更改

标签: java jquery maven spring-mvc tomcat8


【解决方案1】:

我猜你的应用程序中缺少几个配置。

您需要将 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>

【讨论】:

  • 感谢您的输入 TechBreak - 我已添加您建议的更改,但不幸的是,我仍然遇到相同的 404 错误。
  • @Rufus 我认为您的方法已被调用,但未能找到“确定”的视图。尝试添加日志和调试。还尝试 OK.jsp 以便视图解析器找到它。
  • 太棒了!您建议的更改在 Tomcat 服务器完全重新启动后完美运行 - 非常感谢。
  • 我试过了,但作为一个新人,它显然没有公开展示。真的很高兴你能帮我解决这个相当令人沮丧的问题。
  • @Rufus 别担心。
【解决方案2】:

(代表 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

干杯。

【讨论】:

    猜你喜欢
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    相关资源
    最近更新 更多