资源不可用

时间:2016-09-17 11:19:30

标签: xml jsp spring-mvc intellij-idea

我查看了不同的链接和网站,但无法解决资源不可用的问题。我通过以前的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;

    }
}

我已将spring-dispatcher-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-dispatcher-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 / pages下。

<%--
  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的项目结构如下:

enter image description here

但每次运行时都会http://localhost:8888/hi。它会产生以下错误:

请求的资源不可用。

我花了一整天但却想不通。

2 个答案:

答案 0 :(得分:1)

最后,我通过在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>

答案 1 :(得分:0)

我认为您应该在web.xml中看到应用程序名称

<display-name>mvc-test</display-name>

可能是你的请求路径,如波纹管 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>