2016-09-17 12 views
0

私は別のリンクとサイトを見ていましたが、リソースが利用できないという問題を解決できませんでした。私は以前のstackoverflow答えを通過するが、解決できませんでした。私はIntellijを使用しています。2016.2.2リソースが利用できません

ここはSpring MVCのHelloWorldの例です。

コントローラクラス:

package com.himal; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.servlet.ModelAndView; 

/** 
* Created by Himal Acharya on 2016-09-17. 
*/ 
@Controller 
public class HelloWorld { 

    @RequestMapping("/hi") 

    public ModelAndView helloWorld(){ 
     ModelAndView modelAndView=new ModelAndView("Hello"); 
     modelAndView.addObject("message","Hello and Hi"); 
     return modelAndView; 

    } 
} 

私は、WEB-INFに、ばねディスパッチャ-servlet.xmlとweb.xmlを入れています。

web.xmlファイル:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
     version="3.1"> 

    <display-name>SpringHelloWorldDemo12</display-name> 
    <servlet> 
     <servlet-name>spring-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>spring-dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</web-app> 

と春-ディスパッチャ-servlet.xmlは次のとおりです。

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> 


    <context:component-scan base-package="com.himal"/> 
    <mvc:annotation-driven/> 
    <bean id="jspViewResolver" 
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix"> 
      <value>/pages/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property></bean> 
</beans> 

私はWEB /ページの下に私のJSPファイルを入れています。

enter image description here

しかし、毎回実行http://localhost:8888/hi .ITは、次のエラーを生成します:

要求されたリソースは利用できません

のIntelliJで
<%-- 
    Created by IntelliJ IDEA. 
    User: Himal Acharya 
    Date: 2016-09-17 
    Time: 3:49 PM 
    To change this template use File | Settings | File Templates. 
--%> 
<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<html> 
<head> 
    <title>Title</title> 
</head> 
<body> 
    <h1>${message}</h1> 
</body> 
</html> 

マイプロジェクト構造はようです。

私は一日を過ごしましたが、わかりませんでした。

+0

アプリケーションをルートWebアプリケーションとしてデプロイしますか?それ以外の場合は、おそらく 'http:// localhost:8888/SpringHelloWorldDemo12/hi'にあります。 –

+0

私はルートWebアプリケーションとして展開していません。しかし、私はhttp:// localhost:8888/SpringHelloWorldDemo12/hiを試してみました。同じ問題が発生しました –

+0

...あなたがアプリケーションを展開するために選択したコンテキストパスは何ですか? –

答えて

1

のようにweb.xmlファイルに、そのファイルのパスを追加しようと、私は追加することで問題を解決しましたweb.xmlのcontext-param

<?xml version="1.0" encoding="UTF-8"?> 
     <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
       version="3.1"> 
      <servlet> 
       <servlet-name>spring-dispatcher</servlet-name> 
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
       <load-on-startup>1</load-on-startup> 
      </servlet> 
      <servlet-mapping> 
       <servlet-name>spring-dispatcher</servlet-name> 
       <url-pattern>/</url-pattern> 
      </servlet-mapping> 
      <context-param> 
       <param-name>contextConfigLocation</param-name> 
       <param-value>WEB-INF/spring-dispatcher-servlet.xml</param-value> 
      </context-param> 
      <listener> 
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
      </listener> 
     </web-app> 
0

は、私はあなたが

​​

が怒鳴るのコードを変更するには以下の http://localhost:8888/mvc-test/hi

試してみるようなあなたの要求パスかもしれアプリケーション名をweb.xmlに表示されるはずだと思う

<servlet-mapping> 
    <servlet-name>spring-dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

<url-pattern>/*</url-pattern> 

が怒鳴る

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> 

<context:component-scan base-package="main.java.com.core.controller"></context:component-scan> 

ようなあなたのアプリで別のXMLファイルを追加し、最後に怒鳴る

<context-param> 
<param-name>contextConfigLocation</param-name> 
<param-value> 
    classpath:main/resources/config/xmlfilename.xml 
</param-value> 
</context-param> 
+0

これは動作しません。同じ問題 –

+0

web.xmlのアプリケーション名タグは何ですか? –

+0

mvc-test

関連する問題