2016-09-17 78 views
0

我看起來不同的鏈接和網站,但無法解決資源不可用的問題。我通過以前的計算器答案,但無法解決。我正在使用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; 

    } 
} 

我已經把彈簧調度 - servlet.xml中和web.xml在WEB-INF。

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> 

和Spring調度-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> 

我已經把我的JSP文件下的WEB /頁。運行http://localhost:8888/hi。它產生時,下面的錯誤

enter image description here

但每次:

請求的資源不可用

<%-- 
    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> 

中的IntelliJ我的項目結構爲。

我花了整整一天,但無法弄清楚。

+0

您是否將應用程序部署爲根Web應用程序?否則,它可能在'http:// localhost:8888/SpringHelloWorldDemo12/hi'。 –

+0

我沒有部署爲根網絡應用程序。但我試過http:// localhost:8888/SpringHelloWorldDemo12/hi ..同樣發生了同樣的問題 –

+0

And ...然後,您選擇部署應用程序的上下文路徑是什麼? –

回答

1

最後,我通過增加解決問題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"> 
      <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

我想你應該在你的web.xml應用程序名稱見

​​

可能會像波紋管 http://localhost:8888/mvc-test/hi

嘗試請求路徑改變婁代碼

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

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

嘗試添加另一個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: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> 

,並添加該文件的路徑在web.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