2016-12-05 3 views
0

私はこのコードを動作させようとしています。私はちょうど春のMVC(と私は比較的新しいWeb開発者 - 私の背景は、GUI開発者/科学)ですが、私はエラーが発生する可能性があります私のJSPでjqueryコードを春にリンクする問題があると思うコントローラ。私は文字通りこれを成功させることなく数日間働かせようとしてきました。私は、このフォーラムのポスターで提案されたすべての提案を同様の問題で試しました。無駄です。私はあなたの意見を非常に感謝しています。このプロジェクトは、Netbeans、Tomcat 8、Maven、Spring MVC、およびJqueryを使用して開発されています。Spring MVCコントローラへのAjaxポストにより、「要求されたリソースは利用できません」エラー

projectDashboard.jsp(WEB-INF /ビュー内):

<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 /メイン/ Javaでスプリング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:

The requested resource is not available. (404 error) 

マイProject.java::FirefoxのWebコンソールで

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

エラー

@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

あなたの完全なURLは何ですか?それは/ ProgressManagerOnline/projectDashboardである必要があります – ScanQR

+0

ええ...私はそれを試みました - 私はまだ残念ながらエラーを取得します。 – Rufus

+0

あなたの提案した変更を反映するようにコードを変更しました – Rufus

答えて

0

あなたのアプリケーションでは、いくつかの設定が欠落しています。

ので、説明書を見てくださいあなたは、JavaオブジェクトにJSONの自動変換を必要と

@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> 
+0

あなたのご意見ありがとうございますTechBreak - あなたが示唆した変更を追加しましたが、残念ながら私は同じ404エラーを受けています。 – Rufus

+0

@Rufusあなたのメソッドが呼び出されたと思うが、それは "OK"のためのビューを見つけることに失敗した。ログを追加してデバッグしようとしています。また、OK.jspを試して、viewリゾルバがそれを見つけられるようにしてください。 – ScanQR

+0

すごい!あなたが提案した変更は、Tomcatサーバーの完全な再起動後に完全に機能します。 – Rufus

0

(OPに代わって投稿)

TechBreakで提案された解決策が働いた - 私は、私のpomにSpringコンテキストの依存関係がなかった。XMLと私の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 

乾杯。

関連する問題