Java 请求的资源在spring MVC中不可用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20742749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:00:10  来源:igfitidea点击:

Requested resource is not available in spring MVC

javaspringjspspring-mvc

提问by Subho

I am new to spring MVC,i am trying to deploy a hello world application in it.But I am always getting a requested resource not available error on the jsp page.I am using tomcat 7. Here I am pasting my code anyone please help..

我是 spring MVC 的新手,我正在尝试在其中部署一个 hello world 应用程序。 ..

web.xml

网页.xml

<?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee       
      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">

<display-name>HelloWorldSpring</display-name>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

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

spring-servlet.xml

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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">


<context:component-scan
    base-package="net.viralpatel.spring3.controller" />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>
</beans>

Controller of the application

应用程序控制器

package net.viralpatel.spring3.controller;

import javax.servlet.http.HttpServletRequest;

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

@Controller
public class HelloWorldController {

    @RequestMapping("/hello")
    public ModelAndView helloWorld() {
        String message = "Hello World, Spring 3.0!";
        System.out.println(message);
        return new ModelAndView("hello", "message", message);
    }

}

hello.jsp

你好.jsp

<html>
<head>
<title>Spring 3.0 MVC </title>
</head>
<body>
${message}
</body>
</html>

index.jsp

索引.jsp

<html>
<head>

</head>
<body>
<a href="hello">Say Hello</a>
</body>
</html>

this is my application and I am also adding screenshot of my project structure..enter image description here

这是我的应用程序,我还添加了我的项目结构的屏幕截图..在此处输入图片说明

采纳答案by Rohan

The main problem was with <url-pattern>*.html</url-pattern>.
I did following changes in your code and i am able to run the same code on my machine :

主要问题在于 <url-pattern>*.html</url-pattern>.
我对您的代码进行了以下更改,并且能够在我的机器上运行相同的代码:

1) changed <url-pattern>*.html</url-pattern>to <url-pattern>/</url-pattern>
2) copied jstl-1.2.jarin libfolder.

1)改变<url-pattern>*.html</url-pattern><url-pattern>/</url-pattern>
2)复制jstl-1.2.jarlib文件夹中。

回答by Ashish Jagtap

Just change your controller as follows

只需按如下方式更改您的控制器

@Controller
    @RequestMapping("/hello")
    public class HelloWorldController {

    @RequestMapping(method = RequestMethod.GET)
    public String helloWorld(ModelMap model, HttpServletRequest request) {
        String message = "Hello World, Spring 3.0!";
        System.out.println(message);
        model.addAttribute("message", message);
        return "hello";
    }
    }

hope this will solve your problem

希望这能解决你的问题

回答by Fenil

Once check your folder structure.. all spring jars should be copied in lib folder that is in Web-INF and import from there. That should solve your issue

检查您的文件夹结构后.. 所有 spring jar 都应复制到 Web-INF 中的 lib 文件夹中并从那里导入。那应该可以解决您的问题

回答by Witold Kaczurba

I had a similar problem and I believe it was Maven-related. What I did is I changed jstl dependency from jstl:jstl:1.2 to javax.servlet:jstl:1.2 . I do not believe that this was the reason but after Maven updated its dependencies - it started working. Then it made no difference which one I used. I think it had to do with libs being not in the right place...

我有一个类似的问题,我相信它与 Maven 相关。我所做的是将 jstl 依赖项从 jstl:jstl:1.2 更改为 javax.servlet:jstl:1.2 。我不相信这是原因,但在 Maven 更新其依赖项之后 - 它开始工作。然后我用哪一个没有区别。我认为这与 libs 不在正确的位置有关...

enter image description here

在此处输入图片说明

回答by Lakshmi Murali

Change the following in the web.xml

在 web.xml 中更改以下内容

<servlet>

      <servlet-name>HelloWeb</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
        </init-param>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>HelloWeb</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>

回答by Ajay

Well i too faced this problem while following thistutorial and code above seems to be from this tutorial also,so Adding spring-webmvc-x-y-z.RELEASE.jar, and removing "asm" and "web.servlet" jars worked for me. Hope this would help someone learning spring.

好吧,我在学习教程时也遇到了这个问题,上面的代码似乎也来自本教程,因此添加 spring-webmvc-xyz.RELEASE.jar,并删除“asm”和“web.servlet”jar 对我有用。希望这对学习spring的人有所帮助。

回答by pushpendra yadav

I felt the same issue. I added the jstl 1.2.jar and it worked for me.

我觉得同样的问题。我添加了 jstl 1.2.jar 并且它对我有用。